#include <iostream> using namespace std; enum result_code { Underflow, Overflow, Success, Duplicate, NotPresent }; template<class T> class dynamic_set { public: virtual result_code insert( const T & x ) = 0; virtual result_code remove( T & x ) = 0; virtual result_code search( T & x ) const = 0; virtual bool empty() const = 0; //virtual bool full() const = 0; }; template<class T> struct binary_tree_node { T data; binary_tree_node<T> * left; binary_tree_node<T> * right; binary_tree_node( const T & data, binary_tree_node * left = NULL, binary_tree_node * right = NULL ) { this->data = data; this->left = left; this->right = right; } }; template<class T> class binary_search_tree : public dynamic_set<T> { public: binary_search_tree() { root = NULL; } ~binary_search_tree(); public: result_code search( T & x ) const; result_code insert( const T & x ); result_code remove( T & x ); void clear(); void inorder_traverse() const; bool empty() const; private: void clear( binary_tree_node<T> *& root ); void inorder_recur( binary_tree_node<T> * root ) const; result_code insert_recur( binary_tree_node<T> * root, const T & x ); result_code search_recur( binary_tree_node<T> * root, T & x ) const; result_code search( binary_tree_node<T> * root, T & x ) const; private: binary_tree_node<T> * root; }; template<class T> void binary_search_tree<T>::clear( binary_tree_node<T> *& root ) { if( root != NULL ) { clear( root->left ); clear( root->right ); delete root; root = NULL; } } template<class T> void binary_search_tree<T>::clear() { clear(root); } template<class T> void binary_search_tree<T>::inorder_recur( binary_tree_node<T> * root ) const { if( root != NULL ) { inorder_recur( root->left ); cout << root->data << " "; inorder_recur( root->right ); } } template<class T> void binary_search_tree<T>::inorder_traverse() const { inorder_recur( root ); } template<class T> binary_search_tree<T>::~binary_search_tree() { clear(); } template<class T> result_code binary_search_tree<T>::search( T & x ) const { return search( root, x ); } template<class T> result_code binary_search_tree<T>::insert_recur( binary_tree_node<T> * root, const T & x ) { result_code res; if( root == NULL ) { root = new binary_tree_node<T>(x); res = Success; } else { if( root->data == x ) res = Duplicate; else if( root->data < x ) res = insert_recur( root->left, x ); else res = insert_recur( root->right, x ); } return res; } template<class T> result_code binary_search_tree<T>::insert( const T & x ) { result_code res; if( root == NULL ) { root = new binary_tree_node<T>( x, NULL, NULL ); res = Success; } else { binary_tree_node<T> * p = root; binary_tree_node<T> * q = NULL; while( p != NULL ) { q = p; if( p->data == x ) { res = Duplicate; break; } else if( p->data < x ) p = p->right; else p = p->left; } if( p == NULL ) { p = new binary_tree_node<T>( x ); if( x < q->data ) q->left = p; else q->right = p; res = Success; } } return res; } template<class T> result_code binary_search_tree<T>::remove( T & x ) { result_code res; binary_tree_node<T> * p = root; binary_tree_node<T> * p_father = NULL; binary_tree_node<T> * p_child = NULL; binary_tree_node<T> * s = NULL; binary_tree_node<T> * s_father = NULL; // locate x while( p != NULL && p->data != x ) { p_father = p; p = ( x < p->data ) ? p->left : p->right; } if( p == NULL ) { res = NotPresent; } else { x = p->data; // convert problem if( p->left != NULL && p->right != NULL ) { s = p->right; s_father = p; while( s->left != NULL ) { s_father = s; s = s->left; } p->data = s->data; p = s; p_father = s_father; } // solve the more general problem p_child = ( p->left != NULL ) ? p->left : p->right; if( p == root ) { root = p_child; } else { if( p_father->left == p ) p_father->left = p_child; else p_father->right = p_child; } res = Success; delete p; } return res; } template<class T> result_code binary_search_tree<T>::search( binary_tree_node<T> * root, T & x ) const { binary_tree_node<T> * p = root; while( p != NULL ) { if( x < p->data ) p = p->left; else if( x > p->data ) p = p->right; else { x = p->data; break; } } return p == NULL ? NotPresent : Success; } template<class T> result_code binary_search_tree<T>::search_recur( binary_tree_node<T> * root, T & x ) const { result_code res; if( root == NULL ) { res = NotPresent; } else { if( x < root->data ) { res = search( root->left, x ); } else if( x > root->data ) { res = search( root->right, x ); } else { x = root->data; res = Success; } } return res; } template<class T> bool binary_search_tree<T>::empty() const { return root == NULL; } // int main(int argc, char *argv[]) { int data[] = { 28, 21, 25, 36, 33, 43 }; binary_search_tree<int> tree; for( int i = 0; i < 6; ++i ) tree.insert( data[i] ); tree.inorder_traverse(); cout << endl; cout << "Remove " << 25 << endl; int x = 28; tree.remove(x); tree.inorder_traverse(); cout << endl; tree.clear(); if( tree.empty() ) cout << "Empty" << endl; return 0; }