#ifndef ITERATE_H_ #define ITERATE_H_ #include "BinaryTree.h" #include "StackLi.h" #include "QueueLi.h" #include "Except.h" typedef IteratorOutOfBoundsException BadIterator; //////////// ITERATOR BASE CLASS // TreeIterator class interface; maintains "current position". // // CONSTRUCTION: with a tree to which the iterator is bound. // // ******************PUBLIC OPERATIONS********************** // First two are not virtual, last two are pure virtual // bool isValid( ) --> True if at valid position in tree // Object retrieve( ) --> Return item in current position // void first( ) --> Set current position to first // void advance( ) --> Advance // ******************ERRORS********************************* // BadIterator is thrown for illegal access or advance. template class TreeIterator { public: TreeIterator( const BinaryTree & theTree ) : root( theTree.root ), current( NULL ) { } virtual ~TreeIterator( ) { } virtual void first( ) = 0; bool isValid( ) const { return current != NULL; } const Object & retrieve( ) const; virtual void advance( ) = 0; protected: const BinaryNode *root; const BinaryNode *current; }; //////////// PREORDER // PreOrder class interface; maintains "current position". // // CONSTRUCTION: with a tree to which the iterator is bound. // // ******************PUBLIC OPERATIONS********************** // bool isValid( ) --> True if at valid position in tree // Object retrieve( ) --> Return item in current position // void first( ) --> Set current position to first // void advance( ) --> Advance // ******************ERRORS********************************* // BadIterator is thrown for illegal access or advance. template class PreOrder: public TreeIterator { public: PreOrder( const BinaryTree & theTree ); ~PreOrder( ) { } void first( ); void advance( ); protected: Stack< const BinaryNode * > s; }; ////////// POSTORDER // PostOrder class interface; maintains "current position". // // CONSTRUCTION: with a tree to which the iterator is bound. // // ******************PUBLIC OPERATIONS********************** // bool isValid( ) --> True if at valid position in tree // Object retrieve( ) --> Return item in current position // void first( ) --> Set current position to first // void advance( ) --> Advance // ******************ERRORS********************************* // BadIterator is thrown for illegal access or advance. template struct StNode { const BinaryNode *node; int timesPopped; StNode( const BinaryNode *n = 0 ) : node( n ), timesPopped( 0 ) { } }; template class PostOrder : public TreeIterator { public: PostOrder( const BinaryTree & theTree ); ~PostOrder( ) { } void first( ); void advance( ); protected: Stack< StNode > s; }; ////////// INORDER // InOrder class interface; maintains "current position". // // CONSTRUCTION: with a tree to which the iterator is bound. // // ******************PUBLIC OPERATIONS********************** // bool isValid( ) --> True if at valid position in tree // Object retrieve( ) --> Return item in current position // void first( ) --> Set current position to first // void advance( ) --> Advance // ******************ERRORS********************************* // BadIterator is thrown for illegal access or advance. template class InOrder : public PostOrder { // Accept PostOrder construction and default destruction. public: InOrder( const BinaryTree & theTree ) : PostOrder( theTree ) { } void advance( ); }; ////////// LEVEL ORDER // LevelOrder class interface; maintains "current position". // // CONSTRUCTION: with a tree to which the iterator is bound. // // ******************PUBLIC OPERATIONS********************** // bool isValid( ) --> True if at valid position in tree // Object retrieve( ) --> Return item in current position // void first( ) --> Set current position to first // void advance( ) --> Advance // ******************ERRORS********************************* // BadIterator is thrown for illegal access or advance. template class LevelOrder : public TreeIterator { public: LevelOrder( const BinaryTree & theTree ); ~LevelOrder( ) { } void first( ); void advance( ); private: Queue< const BinaryNode * > q; }; #include "Iterate.cpp" #endif