#ifndef AVL_TREE_H_ #define AVL_TREE_H_ // Node and forward declaration because g++ does // not understand nested classes. template class AvlTree; template class AvlNode { Comparable element; AvlNode *left; AvlNode *right; int height; AvlNode( const Comparable & theElement, AvlNode *lt, AvlNode *rt, int h = 0 ) : element( theElement ), left( lt ), right( rt ), height( h ) { } friend class AvlTree; }; #include "dsexceptions.h" #include // For NULL // AvlTree class // // CONSTRUCTION: with ITEM_NOT_FOUND object used to signal failed finds // // ******************PUBLIC OPERATIONS********************* // void insert( x ) --> Insert x // void remove( x ) --> Remove x (unimplemented) // 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 template class AvlTree { public: explicit AvlTree( const Comparable & notFound ); AvlTree( const AvlTree & rhs ); ~AvlTree( ); 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 AvlTree & operator=( const AvlTree & rhs ); private: AvlNode *root; const Comparable ITEM_NOT_FOUND; const Comparable & elementAt( AvlNode *t ) const; void insert( const Comparable & x, AvlNode * & t ) const; AvlNode * findMin( AvlNode *t ) const; AvlNode * findMax( AvlNode *t ) const; AvlNode * find( const Comparable & x, AvlNode *t ) const; void makeEmpty( AvlNode * & t ) const; void printTree( AvlNode *t ) const; AvlNode * clone( AvlNode *t ) const; // Avl manipulations int height( AvlNode *t ) const; int max( int lhs, int rhs ) const; void rotateWithLeftChild( AvlNode * & k2 ) const; void rotateWithRightChild( AvlNode * & k1 ) const; void doubleWithLeftChild( AvlNode * & k3 ) const; void doubleWithRightChild( AvlNode * & k1 ) const; }; #include "AvlTree.cpp" #endif