| | | | | |

Threaded Iterator Support

BST class:

  public:
    Iterator Begin() const
    {
      Iterator i;
      i.Init(root_);
      return i;
    }

    Iterator End() const
    {
      Iterator i;
      return i;
    }

Iterator class:

  private: // usable by friends but not clients
    void Init(Node* n)
    {
      node_ = n;
      while (node_ != nullptr && node_->HasLeftChild())
        node_ = node_->lchild_;
      while (node_ != nullptr && node_->IsDead())
        Increment();
    }

    void Increment ()
    {
      if (node_ == nullptr) return;
      if (node_->IsRightThreaded())
      {
        node_ = node_->rchild_;
        return;
      }
      node_ = node_->rchild_;
      while (node_ != nullptr && node_->HasLeftChild())
        node_ = node_->lchild_;
    }

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