예제 #1
0
 private void AddTo(AVLTreeNode <T> node, T value)
 {
     // Case 1: Value is less than the current node value
     if (value.CompareTo(node.Value) < 0)
     {
         if (node.Left == null)
         {
             node.Left = new AVLTreeNode <T>(value, node, this);
         }
         else
         {
             AddTo(node.Left, value);
         }
     }
     // Case 2: Value is equal to or greater than the current value
     else
     {
         // If there is no right, add it to the right
         if (node.Right == null)
         {
             node.Right = new AVLTreeNode <T>(value, node, this);
         }
         else
         {
             // else add it to the right node
             AddTo(node.Right, value);
         }
     }
     node.Balance();
 }
예제 #2
0
        /// <summary>
        /// Removes the first occurance of the specified value from the tree.
        /// </summary>
        /// <param name="value">The value to remove</param>
        /// <returns>True if the value was removed, false otherwise</returns>
        public bool Remove(T value)
        {
            AVLTreeNode <T> current;

            current = Find(value);

            if (current == null)
            {
                return(false);
            }

            AVLTreeNode <T> treeToBalance = current.Parent;

            _count--;

            // Case 1: If current has no right child, then current's left replaces current

            if (current.Right == null)
            {
                if (current.Parent == null)
                {
                    _head = current.Left;
                    if (_head != null)
                    {
                        _head.Parent = null;
                    }
                }
                else
                {
                    int result = current.Parent.CompareTo(current.Value);
                    if (result > 0)
                    {
                        // if parent value is greater than current value
                        // make the current left child a left child of parent
                        current.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
                        current.Parent.Right = current.Left;
                    }
                }
            }
            // Case 2: If current's right child has no left child, then current's right child
            //         replaces current
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;
                if (current.Parent == null)
                {
                    _head = current.Right;
                    if (_head != null)
                    {
                        _head.Parent = null;
                    }
                }
                else
                {
                    int result = current.Parent.CompareTo(current.Value);
                    if (result > 0)
                    {
                        // if parent value is greater than current value
                        // make the current right child a left child of parent
                        current.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
                        current.Parent.Right = current.Right;
                    }
                }
            }
            // Case 3: If current's right child has a left child, replace current with current's
            //         right child's left-most child
            else
            {
                // find the right's left-most child
                AVLTreeNode <T> leftmost = current.Right.Left;

                while (leftmost.Left != null)
                {
                    leftmost = leftmost.Left;
                }

                // the parent's left subtree becomes the leftmost's right subtree
                leftmost.Parent.Left = leftmost.Right;

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

                leftmost.Right = current.Right;

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

            if (treeToBalance != null)
            {
                treeToBalance.Balance();
            }
            else
            {
                if (_head != null)
                {
                    _head.Balance();
                }
            }

            return(true);
        }