| | | | | |

Inorder Iterator Using Stack

Works for 2-D alternative (no parent pointers)

  template < class C >
  class InorderBTIterator // patterns: ConstIterator, BidirectionalIterator
  {
  public: // terminology support
    typedef typename C::ValueType    ValueType;
    typedef typename C::Node         Node;

    // proper type "big 4" ...

    // used in algorithms, not as important for clients
    bool  Valid   () const { return !stk_.Empty(); } // Iterator can be de-referenced

    // various operators
    bool                   operator== (const InorderBTIterator& i2) const { return stk_ == i2.stk_; }
    bool                   operator!= (const InorderBTIterator& i2) const { return !(*this == i2); }
    const ValueType&       operator*  () const { return stk_.Top()->value_; }
    InorderBTIterator<C>&  operator++ ();    // prefix
    InorderBTIterator<C>   operator++ (int); // postfix
    InorderBTIterator<C>&  operator-- ();    // prefix
    InorderBTIterator<C>   operator-- (int); // postfix

    // developers helper
    void Dump( std::ostream& os = std::cout , char ofc = '\0' ) const;

  private:
    friend C;
    fsu::Stack < Node* , fsu::Vector < Node* > > stk_; // keep stack implementation choice compatible w ChechRBLLT
    void Init      (Node* n); // live nodes only
    void sInit     (Node* n); // structural (all nodes) version - pair with Increment for structural traversal
    void rInit     (Node* n); // live nodes only
    void Increment (); // structural version of ++
    void Decrement (); // structural version of --
  };

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