#ifndef AA_TREE_H_ #define AA_TREE_H_ #include "dsexceptions.h" #include // For NULL // AATree class // // CONSTRUCTION: with ITEM_NOT_FOUND object used to signal failed finds // // ******************PUBLIC OPERATIONS********************* // void insert( x ) --> Insert x // void remove( x ) --> Remove x // Comparable find( x ) --> Return item that matches x // Comparable findMin( ) --> Return smallest item // Comparable findMax( ) --> Return largest item // boolean isEmpty( ) --> Return true if empty; else false // void makeEmpty( ) --> Remove all items // void printTree( ) --> Print tree in sorted order // Node and forward declaration because g++ does // not understand nested classes. template class AATree; template class AANode { Comparable element; AANode *left; AANode *right; int level; AANode( ) : left( NULL ), right( NULL ), level( 1 ) { } AANode( const Comparable & e, AANode *lt, AANode *rt, int lv = 1 ) : element( e ), left( lt ), right( rt ), level( lv ) { } friend class AATree; }; template class AATree { public: explicit AATree( const Comparable & notFound ); AATree( const AATree & rhs ); ~AATree( ); const Comparable & findMin( ) const; const Comparable & findMax( ) const; const Comparable & find( const Comparable & x ) const; bool isEmpty( ) const; void printTree( ) const; void makeEmpty( ); void insert( const Comparable & x ); void remove( const Comparable & x ); const AATree & operator=( const AATree & rhs ); private: AANode *root; const Comparable ITEM_NOT_FOUND; AANode *nullNode; // Recursive routines void insert( const Comparable & x, AANode * & t ); void remove( const Comparable & x, AANode * & t ); void makeEmpty( AANode * & t ); void printTree( AANode *t ) const; // Rotations void skew( AANode * & t ) const; void split( AANode * & t ) const; void rotateWithLeftChild( AANode * & t ) const; void rotateWithRightChild( AANode * & t ) const; AANode * clone( AANode * t ) const; }; #include "AATree.cpp" #endif