Пример #1
0
        /// <summary>
        /// Adjusts the balance factors for affected tree nodes.
        /// </summary>
        private void InsertBalanceTree(AVLNode <TKey, TValue> node, int addBalance)
        {
            while (node != null)
            {
                //Add the new balance value to the current node balance.
                node.Balance += addBalance;

                /*
                 * If the balance was -1 or +1, the tree is still balanced so
                 * we don't have to balanced it further
                 */
                if (node.Balance == 0)
                {
                    break;
                }
                //If the height(left-subtree) - height(right-subtree) == 2
                else if (node.Balance == 2)
                {
                    if (node.LeftChild.Balance == 1)
                    {
                        RotateLeftLeft(node);
                    }
                    else
                    {
                        RotateLeftRight(node);
                    }
                    break;
                }

                //If the height(left-subtree) - height(right-subtree) == -2
                else if (node.Balance == -2)
                {
                    if (node.RightChild.Balance == -1)
                    {
                        RotateRightRight(node);
                    }
                    else
                    {
                        RotateRightLeft(node);
                    }
                    break;
                }

                if (node.Parent != null)
                {
                    /*
                     * If the current node is the left child of the parent node
                     * we need to increase the height of the parent node.
                     * */
                    if (node.Parent.LeftChild == node)
                    {
                        addBalance = 1;
                    }

                    /*
                     * If it is the right child,
                     * we decrease the height of the parent node
                     * */
                    else
                    {
                        addBalance = -1;
                    }
                }
                node = node.Parent;
            }
        }
Пример #2
0
 /// <summary>
 /// Czyszczenie drzewa
 /// </summary>
 public void Clear()
 {
     this.root = null;
 }
Пример #3
0
 public AVLNode(AVLNode <TKey, TValue> node)
     : this(node.Key, node.Value, node.Parent)
 {
     this.LeftChild  = node.LeftChild;
     this.RightChild = node.RightChild;
 }
Пример #4
0
 public AVLTree()
 {
     this.root = null;
 }
Пример #5
0
 public AVLNode(TKey key, TValue value, AVLNode <TKey, TValue> parent)
     : this(key, value)
 {
     this.Parent = parent;
 }