// T(n) = O(n); S(n) = O(h) (height) private IEnumerator <T> GetInOrderEnumerator() { Stack <AvlNode <T> > stack = new Stack <AvlNode <T> >(); AvlNode <T> current = Root; while (current.IsNotNull() || !stack.IsEmpty) { while (current.IsNotNull()) { stack.Push(current); current = current.Left; } if (!stack.IsEmpty) { current = stack.Pop(); yield return(current.Item); current = current.Right; } } }
private AvlNode <T> Find(AvlNode <T> node, T item) { AvlNode <T> current = node; while (current.IsNotNull()) { if (current.Item.CompareTo(item) == 0) { return(current); // Match } else if (current.Item.CompareTo(item) > 0) { current = current.Left; } else { current = current.Right; } } return(null); // Not found. }
public AvlTree(T rootItem) { Root = new AvlNode <T>(rootItem); }
private static int GetNodeHeight(AvlNode <T> node) { return(node.IsNull() ? -1 : node.Height); }
private bool IsRoot(AvlNode <T> node) { return(Root.Item.CompareTo(node.Item) == 0); }
private static AvlNode <T> DoubleWithRightChild(AvlNode <T> k1) { k1.Right = RotateWithLeftChild(k1.Right); return(RotateWithRightChild(k1)); }
private static AvlNode <T> DoubleWithLeftChild(AvlNode <T> k3) { k3.Left = RotateWithRightChild(k3.Left); return(RotateWithLeftChild(k3)); }