Exemple #1
0
            private static Node <T> RotateLeft(Node <T> n)
            {
                Node <T> result = n.Right;

                n.Right     = result.Left;
                result.Left = n;
                n.FixHeight();
                result.FixHeight();
                return(result);
            }
Exemple #2
0
            public Node RightRotate()
            {
                Node q = Left;

                Left    = q.Right;
                q.Right = this;
                this.FixHeight();
                q.FixHeight();
                return(q);
            }
Exemple #3
0
            public Node LeftRotate()
            {
                Node p = Right;

                Right  = p.Left;
                p.Left = this;
                this.FixHeight();
                p.FixHeight();
                return(p);
            }
Exemple #4
0
 public static Node <T> Balance(Node <T> n)
 {
     n.FixHeight();
     if (n.BalanceFactor() == -2)
     {
         if (n.Right.BalanceFactor() > 0)
         {
             n.Right = RotateRight(n.Right);
         }
         return(RotateLeft(n));
     }
     if (n.BalanceFactor() == 2)
     {
         if (n.Left.BalanceFactor() < 0)
         {
             n.Left = RotateLeft(n.Left);
         }
         return(RotateRight(n));
     }
     return(n);
 }
Exemple #5
0
 private static Node DoBalance(Node node)
 {
     node.FixHeight();
     if (node.BalanceFactor() == 2)
     {
         if (node.Right.BalanceFactor() < 0)
         {
             node.Right = node.Right.RightRotate();
         }
         return(node.LeftRotate());
     }
     if (node.BalanceFactor() == -2)
     {
         if (node.Left.BalanceFactor() > 0)
         {
             node.Left = node.Left.LeftRotate();
         }
         return(node.RightRotate());
     }
     return(node);
 }