private static AvlNode <T> Balance(AvlNode <T> node) { FixHeight(node); if (GetBalanceFactor(node) == 2) { if (GetBalanceFactor(node.Right) < 0) { RotateRight(node.Right); } RotateLeft(node); } else if (GetBalanceFactor(node) == -2) { if (GetBalanceFactor(node.Left) < 0) { RotateLeft(node.Left); } RotateRight(node); } return(node); }
public override void Insert(T item) { Root = Insert(Root, item); Count++; }
private static int GetHeight(AvlNode <T> node) => node?.Height ?? 0;
private static int GetBalanceFactor(AvlNode <T> node) => GetHeight(node?.Right) - GetHeight((node?.Left));