예제 #1
0
 /// <summary>
 /// Removes all items from the tree
 /// </summary>
 public void Clear()
 {
     _head  = null;
     _count = 0;
 }
예제 #2
0
        /// <summary>
        /// Removes the first occurence of the specified value from the tree
        /// </summary>
        /// <param name="value">The value to remove</param>
        /// <returns>True if hte value was removed, false otherwise</returns>
        public bool Remove(T value)
        {
            BinaryTreeNode <T> current, parent;

            //try to find the node to remove
            current = FindWithParent(value, out parent);

            //couldn't find the node to remove
            if (current == null)
            {
                return(false);
            }

            _count--;

            //Case 1: If the current has no right child, then the current's left replaces the current
            if (current.Right == null)
            {
                if (parent == null)
                {
                    //removing the root node, now promote left child to root node
                    _head = current.Left;
                }
                else
                {
                    //determine which parent link we're updating
                    int result = parent.CompareTo(current.Value);
                    if (result > 0)
                    {
                        //if parent value is greater than current value
                        //make the current left child a left child of the parent
                        parent.Left = current.Left;
                    }
                    else if (result < 0)
                    {
                        //if parent value is less than current value
                        //make the current left child a right child of parent
                        parent.Right = current.Left;
                    }
                }
            }

            //Case 2: If the current's right child has no left child, then current's right child
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;
                //removing the root node, now promote right child to root node
                if (parent == null)
                {
                    _head = current.Right;
                }
                else
                {
                    //determine which parent link we're updating
                    int result = parent.CompareTo(current.Value);
                    if (result > 0)
                    {
                        //if parent value is greater than current value
                        //make the current right child a left child of the parent
                        parent.Left = current.Right;
                    }
                    else if (result < 0)
                    {
                        //if parent value is less than current value
                        //make the current right child a right child of parent
                        parent.Right = current.Right;
                    }
                }
            }
            else
            {
                //Case 3: If the current's right child has a left child, replace current with current's right child's left-most child

                //find the right's left-most child
                BinaryTreeNode <T> leftmost       = current.Right.Left;
                BinaryTreeNode <T> leftmostParent = current.Right;

                //while there's still a child to the left, keep going left
                while (leftmost.Left != null)
                {
                    leftmostParent = leftmost;
                    leftmost       = leftmost.Left;
                }

                //the parent's left subtree become's the leftmost's right subtree
                leftmostParent.Left = leftmost.Right;

                //assign leftmost's left and right child to the current's left and right children
                leftmost.Left  = current.Left;
                leftmost.Right = current.Right;

                if (parent == null)
                {
                    _head = leftmost;
                }
                else
                {
                    //determine which parent link we're updating
                    int result = parent.CompareTo(current.Value);
                    if (result > 0)
                    {
                        //if parent value is greater than current value
                        //make the leftmost the parent's left child
                        parent.Left = leftmost;
                    }
                    else if (result < 0)
                    {
                        //if parent value is less than current value
                        //make the leftmost the parent's right child
                        parent.Right = leftmost;
                    }
                }
            }

            return(true);
        }