Exemple #1
0
 /// <summary>
 /// Clears the tree.
 /// </summary>
 public void Clear()
 {
     this.root = null;
 }
Exemple #2
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;
            }
        }
Exemple #3
0
 public AVLTree()
 {
     this.root = null;
 }