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); }
public Node RightRotate() { Node q = Left; Left = q.Right; q.Right = this; this.FixHeight(); q.FixHeight(); return(q); }
public Node LeftRotate() { Node p = Right; Right = p.Left; p.Left = this; this.FixHeight(); p.FixHeight(); return(p); }
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); }
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); }