Exemplo n.º 1
0
 public AVLTreeNode(TNode value, AVLTreeNode <TNode> parent, AVLTree <TNode> tree)
 {
     this.Value  = value;
     this.Parent = parent;
     this.tree   = tree;
 }
Exemplo n.º 2
0
        public bool Remove(T value)
        {
            AVLTreeNode <T> current = Find(value);

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

            AVLTreeNode <T> treeToBallance = 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 if (result < 0)
                    {
                        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 if (result < 0)
                    {
                        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 = leftMost.Parent.CompareTo(leftMost.Value);
                    if (result > 0)
                    {
                        current.Parent.Left = leftMost;
                    }
                    else if (result < 0)
                    {
                        current.Parent.Right = leftMost;
                    }
                }
            }

            if (treeToBallance != null)
            {
                treeToBallance.Balance();
            }
            else
            {
                if (Head != null)
                {
                    Head.Balance();
                }
            }

            return(true);
        }
Exemplo n.º 3
0
 private int BalanceFactor(AVLTreeNode <TKey> x)
 {
     return(Height(x.Left) - Height(x.Right));
 }