Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
 private static int CalculateBalanceFactor(AvlNode <T> node)
 {
     return((int)(GetHeight(node.Right) - GetHeight(node.Left)));
 }
Ejemplo n.º 3
0
 public void Insert(T key)
 {
     _root = Insert(_root, key);
     Count++;
 }
Ejemplo n.º 4
0
 private static uint GetHeight(AvlNode <T> node)
 {
     return(node == null ? 0 : node.Height);
 }