#ifndef BINARY_SEARCH_TREE_H_ #define BINARY_SEARCH_TREE_H_ #include "dsexceptions.h" #include // For NULL // Binary node and forward declaration because g++ does // not understand nested classes. template class BinarySearchTree; template class BinaryNode { Comparable element; BinaryNode *left; BinaryNode *right; BinaryNode( const Comparable & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree; }; // BinarySearchTree 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 template class BinarySearchTree { public: explicit BinarySearchTree( const Comparable & notFound ); BinarySearchTree( const BinarySearchTree & rhs ); ~BinarySearchTree( ); 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 BinarySearchTree & operator=( const BinarySearchTree & rhs ); private: BinaryNode *root; const Comparable ITEM_NOT_FOUND; const Comparable & elementAt( BinaryNode *t ) const; void insert( const Comparable & x, BinaryNode * & t ) const; void remove( const Comparable & x, BinaryNode * & t ) const; BinaryNode * findMin( BinaryNode *t ) const; BinaryNode * findMax( BinaryNode *t ) const; BinaryNode * find( const Comparable & x, BinaryNode *t ) const; void makeEmpty( BinaryNode * & t ) const; void printTree( BinaryNode *t ) const; BinaryNode * clone( BinaryNode *t ) const; }; #include "BinarySearchTree.cpp" #endif