Example #1
0
        public void remove2(int num)
        {
            if (!Contains(num))
            {
                Console.WriteLine("value is not in the tree");
                return;
            }
            BNode pointer = root;
            BNode parent  = root;    // need a parent pointer so that root and pointer can be linked

            while (pointer != null)
            {
                if (num < pointer.value)
                {
                    parent  = pointer;
                    pointer = pointer.left;
                }
                else if (num > pointer.value)
                {
                    parent  = pointer;
                    pointer = pointer.right;
                }
                else
                {
                    break;
                }
            }

            if (pointer.left == null && pointer.right == null)
            {
                // no children
                if (pointer == root)
                {
                    root = null;
                }
                else if (parent.left.value == pointer.value)
                {
                    parent.left = null;
                }
                else if (parent.right.value == pointer.value)
                {
                    parent.right = null;
                }
                pointer = null;
            }
            else if (pointer.left != null && pointer.right == null)
            {
                // one child left
                if (pointer == root)
                {
                    root = pointer.left;
                }
                else if (parent.left.value == pointer.value)
                {
                    parent.left = pointer.left;
                }
                else if (parent.right.value == pointer.value)
                {
                    parent.right = pointer.left;
                }
                pointer = null;
            }
            else if (pointer.left == null && pointer.right != null)
            {
                // one child right
                if (pointer == root)
                {
                    root = pointer.right;
                }
                else if (parent.left.value == pointer.value)
                {
                    parent.left = pointer.right;
                }
                else if (parent.right.value == pointer.value)
                {
                    parent.right = pointer.right;
                }
                pointer = null;
            }
            else
            {
                //  two children
                // swap value with rightmost node of left subtree
                BNode RightMostOfLeft = iop(pointer.left);
                if (pointer == root)
                {
                    root.value = RightMostOfLeft.value;
                }
                else if (parent.left.value == pointer.value)
                {
                    parent.left.value = RightMostOfLeft.value;
                }
                else if (parent.right.value == pointer.value)
                {
                    parent.right.value = RightMostOfLeft.value;
                }
                // traverse to the parent node of the rightmost node
                parent  = pointer;
                pointer = pointer.left;
                while (pointer.right != null && pointer.right.right != null)
                {
                    pointer = pointer.right;
                }
                // remove rightmost node
                if (pointer.right != null)
                {
                    pointer.right = pointer.right.left;
                }
                else
                {
                    parent.left = pointer.left;
                }
            }
        }
Example #2
0
 public BNode(int v)
 {
     value = v;
     left  = null;
     right = null;
 }
Example #3
0
 public BST()
 {
     root = null;
 }