Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AVLTree{T}"/> class.
 ///   Basic constructor.
 /// </summary>
 public AVLTree()
 {
     this._root = null;
 }
Exemple #2
0
 /// <summary>
 /// Clears nodes of the tree.
 /// </summary>
 public override void Clear()
 {
     this._root = null;
 }
Exemple #3
0
        /// <summary>
        /// Removes element from the tree.
        /// </summary>
        /// <param name="data">
        /// Element to be removed.
        /// </param>
        /// <returns>
        /// The remove.
        /// </returns>
        public override bool Remove(T data)
        {
            if (this._root == null)
            {
                return(false);
            }

            AVLTreeNode <T> current = this._root, parent = null;
            int             result = this._comparer.Compare(current.Values[0], data);

            while (result != 0)
            {
                if (result > 0)
                {
                    parent  = current;
                    current = current.Left;
                }
                else
                {
                    parent  = current;
                    current = current.Right;
                }

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

                result = this._comparer.Compare(current.Values[0], data);
            }

            if (current.Right == null)
            {
                if (parent == null)
                {
                    if (current.Left != null)
                    {
                        current.Left.Parent = parent;
                    }

                    this._root = current.Left;
                }
                else
                {
                    result = this._comparer.Compare(parent.Values[0], current.Values[0]);
                    if (result > 0)
                    {
                        if (current.Left != null)
                        {
                            current.Left.Parent = parent;
                        }

                        parent.Left = current.Left;
                    }
                    else
                    {
                        if (current.Right != null)
                        {
                            current.Right.Parent = parent;
                        }

                        parent.Right = current.Right;
                    }
                }
            }
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;
                if (current.Left != null)
                {
                    current.Left.Parent = current.Right;
                }

                if (parent == null)
                {
                    if (current.Right != null)
                    {
                        current.Right.Parent = parent;
                    }

                    this._root = current.Right;
                }
                else
                {
                    result = this._comparer.Compare(parent.Values[0], current.Values[0]);
                    if (result > 0)
                    {
                        if (current.Right != null)
                        {
                            current.Right.Parent = parent;
                        }

                        parent.Left = current.Right;
                    }
                    else
                    {
                        if (current.Right != null)
                        {
                            current.Right.Parent = parent;
                        }

                        parent.Right = current.Right;
                    }
                }
            }
            else
            {
                AVLTreeNode <T> leftMost = current.Right, lmParent = current;
                while (leftMost.Left != null)
                {
                    lmParent = leftMost;
                    leftMost = lmParent.Left;
                }

                lmParent.Left  = leftMost.Right;
                leftMost.Left  = current.Left;
                leftMost.Right = current.Right;
                if (lmParent.Left != null)
                {
                    lmParent.Left.Parent = leftMost;
                }

                if (leftMost.Right != null)
                {
                    leftMost.Right.Parent = lmParent;
                }

                if (current.Left != null)
                {
                    current.Left.Parent = leftMost;
                }

                if (current.Right != null)
                {
                    current.Right.Parent = leftMost;
                }

                if (parent == null)
                {
                    leftMost.Parent = parent;
                    this._root      = leftMost;
                }
                else
                {
                    result = this._comparer.Compare(parent.Values[0], current.Values[0]);
                    if (result > 0)
                    {
                        leftMost.Parent = parent;
                        parent.Left     = leftMost;
                    }
                    else
                    {
                        leftMost.Parent = parent;
                        parent.Right    = leftMost;
                    }
                }
            }

            current.Parent = current.Left = current.Right = null;
            if (this._root != null)
            {
                while (this._root.Parent != null)
                {
                    this._root = (AVLTreeNode <T>) this._root.Parent;
                }

                this._root.UpdateHeight();
                this._root.Balance();
                while (this._root.Parent != null)
                {
                    this._root = (AVLTreeNode <T>) this._root.Parent;
                }
            }

            return(true);
        }
Exemple #4
0
        /// <summary>
        /// Balances node.
        /// </summary>
        /// <returns>
        /// The balance.
        /// </returns>
        public bool Balance()
        {
            if (this.Left != null)
            {
                this.Left.Balance();
            }

            if (this.Right != null)
            {
                this.Right.Balance();
            }

            this.UpdateHeight();
            int leftHeight  = 0;
            int rightHeight = 0;

            if (this.Left != null)
            {
                leftHeight = this.Left.Height;
            }

            if (this.Right != null)
            {
                rightHeight = this.Right.Height;
            }

            if (leftHeight - rightHeight > 1)
            {
                // right rotation
                AVLTreeNode <T> z           = this;
                AVLTreeNode <T> y           = this.Left;
                AVLTreeNode <T> b           = this.Left.Right;
                bool            zIsleftNode = z.Parent != null && ((AVLTreeNode <T>)z.Parent).Left == this;
                z.Left = b;
                if (b != null)
                {
                    b.Parent = z;
                }

                y.Right = z;
                if (y != null)
                {
                    y.Parent = z.Parent;
                    if (y.Parent != null)
                    {
                        if (zIsleftNode)
                        {
                            ((AVLTreeNode <T>)y.Parent).Left = y;
                        }
                        else
                        {
                            ((AVLTreeNode <T>)y.Parent).Right = y;
                        }
                    }
                }

                z.Parent = y;

                if (b != null)
                {
                    b.UpdateHeight();
                }
                if (z != null)
                {
                    z.UpdateHeight();
                }
                if (y != null)
                {
                    y.UpdateHeight();
                }

                return(true);
            }

            if (rightHeight - leftHeight > 1)
            {
                // left rotation
                AVLTreeNode <T> z           = this;
                AVLTreeNode <T> y           = this.Right;
                AVLTreeNode <T> b           = this.Right.Left;
                bool            zIsleftNode = z.Parent != null && ((AVLTreeNode <T>)z.Parent).Left == this;
                z.Right = b;
                if (b != null)
                {
                    b.Parent = z;
                }

                y.Left = z;
                if (y != null)
                {
                    y.Parent = z.Parent;
                    if (y.Parent != null)
                    {
                        if (zIsleftNode)
                        {
                            ((AVLTreeNode <T>)y.Parent).Left = y;
                        }
                        else
                        {
                            ((AVLTreeNode <T>)y.Parent).Right = y;
                        }
                    }
                }

                z.Parent = y;

                if (b != null)
                {
                    b.UpdateHeight();
                }
                if (z != null)
                {
                    z.UpdateHeight();
                }
                if (y != null)
                {
                    y.UpdateHeight();
                }

                return(true);
            }

            return(false);
        }