#ifndef _KTREE_H_ #define _KTREE_H_ #include "dsexceptions.h" #include // For NULL template class KTree; template class TreeItr; // Incomplete declaration. template class KTreeNode { private: Object element; int size; KTreeNode *nextSibling; KTreeNode *firstChild; KTreeNode( const Object & theElement, int sz, KTreeNode *ns, KTreeNode *fc ) : element( theElement ), size (sz), nextSibling( ns ), firstChild( fc ) { } friend class KTree; friend class TreeItr; }; // KTree class // // CONSTRUCTION: with ITEM_NOT_FOUND object used to signal failed finds // // ******************PUBLIC OPERATIONS********************* // void insert( x, sz, itr ) --> Insert x // void remove( x ) --> Remove x // TreeItr find( x ) --> Return location of item that matches x // TreeItr findParent( x ) --> Return location of parent item of x // boolean isEmpty( ) --> Return true if empty; else false // void makeEmpty( ) --> Remove all items // void printTree( ) --> Print tree in sorted order template class KTree { public: explicit KTree(); KTree( const KTree & rhs ); ~KTree( ); TreeItr zeroth( ) const; TreeItr find( const Object & x ) const; TreeItr find( const Object & x, const TreeItr &itr ) const; TreeItr findParent( const Object & x ) const; bool isEmpty( ) const; void printTree( ) const; void makeEmpty( ); void insert( const Object & x, int sz, const TreeItr &itr ); void remove( const Object & x ); const KTree & operator=( const KTree & rhs ); void percolate(); void rinse(); private: KTreeNode *root; TreeItr rootitr; const Object ITEM_NOT_FOUND; const Object & elementAt( KTreeNode *t ) const; void insert( const Object & x, int sz, KTreeNode * & t ) ; int remove( const Object & x, KTreeNode * & t ) const; KTreeNode * find( const Object & x, KTreeNode *t ) const; KTreeNode * findParent( const Object & x, KTreeNode *t ) const; void makeEmpty( KTreeNode * & t ) const; void printTree(KTreeNode *t, char * ) const; KTreeNode * clone( KTreeNode *t ) const; int percolate(KTreeNode *); void rinse(KTreeNode *); }; // TreeItr class; maintains "current position" // // CONSTRUCTION: Package friendly only, with a KTreeNode // // ******************PUBLIC OPERATIONS********************* // bool isPastEnd( ) --> True if past end position in tree // Object retrieve --> Return item in current position template class TreeItr { public: TreeItr( ) : current( NULL ) { } bool isPastEnd( ) const { return current == NULL; } void over() {if (isPastEnd()) throw BadIterator(); current = current->nextSibling; } void down() {if (isPastEnd()) throw BadIterator(); current = current->firstChild;} const Object & retrieve( ) const { if( isPastEnd( ) ) throw BadIterator( ); return current->element; } int retrieveSize( ) const { if( isPastEnd( ) ) throw BadIterator( ); return current->size; } private: KTreeNode *current; // Current position explicit TreeItr( KTreeNode *theNode ) : current( theNode ) { } friend class KTree; // Grant access to constructor }; #endif