| | | | | |

Binary Tree Iterator Algorithms: Inorder

Initialize(bt)
{
  n = bt.root;                       // always enter tree at root
  while (n->HasLeftChild()) ++n;     // "left slide"
  while (n->IsDead()) Increment();   // skip dead nodes
}

Increment()
{
  if (n->HasRightChild())
  {
    n++;                             // go to right child
    while (n->HasLeftChild()) ++n;   // "left slide"
  }
  else                               // find unvisited ancestor
  {
    bool wasRightChild;
    do
    {
      wasRightChild = n->IsRightChild();
      --n;
    }
    while (wasRightChild);
  }
}

| | Top of Page | 14. BST Iterators - 4 of 41