Ejemplo n.º 1
0
        public bool Remove(T value)
        {
            AVLTreeNode <T> current;

            current = Find(value);

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

            AVLTreeNode <T> treeToBalance = current.Parent;

            Count--;

            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)
                    {
                        current.Parent.Left = current.Left;
                    }
                    else
                    {
                        current.Parent.Right = current.Left;
                    }
                }
            }
            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)
                    {
                        current.Parent.Left = current.Right;
                    }
                    else
                    {
                        current.Parent.Right = current.Right;
                    }
                }
            }
            else
            {
                AVLTreeNode <T> leftMost = current.Right.Left;
                while (leftMost.Left != null)
                {
                    leftMost = leftMost.Left;
                }

                leftMost.Parent.Left = leftMost.Right;

                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)
                    {
                        current.Parent.Left = leftMost;
                    }
                    else
                    {
                        current.Parent.Right = leftMost;
                    }
                }
            }
            return(true);
        }
 public AVLTreeNode(TNode value, AVLTreeNode <TNode> parent, AVLTree <TNode> tree)
 {
     Parent    = parent;
     Value     = value;
     this.tree = tree;
 }