Esempio n. 1
0
 private static AvlNode <T> Insert(AvlNode <T> node, T key)
 {
     if (node == null)
     {
         return(new AvlNode <T>(key));
     }
     if (key.CompareTo(node.Key) < 0)
     {
         node.Left = Insert(node.Left, key);
     }
     else
     {
         node.Right = Insert(node.Right, key);
     }
     return(Balance(node));
 }
Esempio n. 2
0
        private static AvlNode <T> Balance(AvlNode <T> node)
        {
            FixHeight(node);
            if (CalculateBalanceFactor(node) == 2)
            {
                if (CalculateBalanceFactor(node.Right) < 0)
                {
                    node.Right = RotateRight(node.Right);
                }
                return(RotateLeft(node));
            }

            if (CalculateBalanceFactor(node) == -2)
            {
                if (CalculateBalanceFactor(node.Left) > 0)
                {
                    node.Left = RotateLeft(node.Left);
                }
                return(RotateRight(node));
            }

            return(node);
        }
Esempio n. 3
0
 public override void Insert(T key)
 {
     _root = Insert(_root, key);
     Count++;
 }
Esempio n. 4
0
 private static int CalculateBalanceFactor(AvlNode <T> node)
 {
     return((int)(GetHeight(node.Right) - GetHeight(node.Left)));
 }
Esempio n. 5
0
 private static uint GetHeight(AvlNode <T> node)
 {
     return(node == null ? 0 : node.Height);
 }