Example #1
0
 public BinaryTreeNode(int key)
 {
     this.key = key;
     right    = null;
     left     = null;
 }
Example #2
0
 public BT()
 {
     root = null;
 }
Example #3
0
 public BinaryTreeNode(int x)
 {
     this.data  = x;
     this.left  = null;
     this.right = null;
 }
 public BinaryTreeVik(int data)
 {
     this.root = new BinaryTreeNode(data);
 }
        public bool DeleteFromBST(int num)
        {
            try {
                if (this.IsEmpty())
                {
                    Console.WriteLine("Tree is empty, nothing to be removed");
                    return(false);
                }
                BinaryTreeNode current  = this.root;
                BinaryTreeNode previous = this.root;
                while (current != null)
                {
                    if (num == current.data)
                    {
                        Console.WriteLine("Giiven number {0} was found", num);
                        break;
                    }
                    previous = current;
                    if (num < current.data)
                    {
                        current = current.left;
                    }
                    else
                    {
                        current = current.right;
                    }
                }
                // Below  if condition handles the special case when node to be delted is root node
                if (current == this.root)
                {
                    if (current.left == null || current.right == null)
                    {
                        if (current.left == null)
                        {
                            this.root = current.right;
                        }
                        else
                        {
                            this.root = current.left;
                        }
                    }
                    else
                    {
                        BinaryTreeNode newRoot     = this.root.left;
                        BinaryTreeNode newRootPrev = this.root.left;
                        while (newRoot.right != null)
                        {
                            newRootPrev = newRoot;
                            newRoot     = newRoot.right;
                        }
                        newRootPrev.right = null;
                        newRoot.left      = this.root.left;
                        newRoot.right     = this.root.right;
                        this.root         = newRoot;
                    }
                }
                // Below code will be executed to handle the case when node to be deleted is not root node
                else
                {
                    if (current.left == null || current.right == null)
                    {
                        if (current.left == null)
                        {
                            previous.right = current.right;
                        }
                        else
                        {
                            previous.left = current.left;
                        }
                    }
                    else
                    {
                        BinaryTreeNode newCurrent = current.left;

                        BinaryTreeNode newPrevious = current.left;
                        while (newCurrent.right != null)
                        {
                            newPrevious = newCurrent;
                            newCurrent  = newCurrent.right;
                        }
                        newCurrent.right = current.right;
                        current.left     = null;
                        current.right    = null;
                        if (previous.left == current)
                        {
                            previous.left = newCurrent;
                        }
                        else
                        {
                            previous.right = newCurrent;
                        }
                    }
                }
                return(true);
            }
            catch (NullReferenceException nre)
            {
                Console.WriteLine(nre.Message);
                Console.WriteLine(nre.StackTrace);
                return(false);
            }
        }
 public BinaryTreeVik()
 {
     this.root = null;
 }