예제 #1
0
        // 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;
                }
            }
        }
예제 #2
0
        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.
        }
예제 #3
0
 public AvlTree(T rootItem)
 {
     Root = new AvlNode <T>(rootItem);
 }
예제 #4
0
 private static int GetNodeHeight(AvlNode <T> node)
 {
     return(node.IsNull() ? -1 : node.Height);
 }
예제 #5
0
 private bool IsRoot(AvlNode <T> node)
 {
     return(Root.Item.CompareTo(node.Item) == 0);
 }
예제 #6
0
 private static AvlNode <T> DoubleWithRightChild(AvlNode <T> k1)
 {
     k1.Right = RotateWithLeftChild(k1.Right);
     return(RotateWithRightChild(k1));
 }
예제 #7
0
 private static AvlNode <T> DoubleWithLeftChild(AvlNode <T> k3)
 {
     k3.Left = RotateWithRightChild(k3.Left);
     return(RotateWithLeftChild(k3));
 }