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