#include #include #include #include "bst.h" const int CMDLENGTH = 100; int main( void ) { char cmd[ CMDLENGTH ]; int item; BinarySearchTree bst; cout << "bst> " << flush; while( cin >> cmd ) { if( !strcmp( cmd, "quit" ) ) { exit( 0 ); } else if( !strcmp( cmd, "help" ) ) { cout << "Commands:" << endl << " quit: quit the program" << endl << " help: print this help message" << endl << " print: print out the current search tree" << endl << " inorder: print out tree using inorder traversal" << endl << " preorder: print out tree using preorder traversal" << endl << " postorder: print out tree using postorder traversal" << endl << "insert ...: insert the number n into the tree" << endl << " remove : remove the number n from the tree" << endl << " find : is the value n in the tree?" << endl << " reset: remove all elements from the tree" << endl; } else if( !strcmp( cmd, "print" ) ) { bst.printNice( cout ); cout << endl; } else if( !strcmp( cmd, "inorder" ) ) { Printer p( cout ); bst.inorder( p ); cout << endl; } else if( !strcmp( cmd, "preorder" ) ) { Printer p( cout ); bst.preorder( p ); cout << endl; } else if( !strcmp( cmd, "postorder" ) ) { Printer p( cout ); bst.postorder( p ); cout << endl; } else if( !strcmp( cmd, "insert" ) ) { cin >> item; bst.insert( item ); char ch; while( cin.get( ch ) ) { if( ch == '\n' ) { break; } else if( isdigit( ch ) ) { cin.putback( ch ); cin >> item; bst.insert( item ); } } } else if( !strcmp( cmd, "remove" ) ) { cin >> item; bst.remove( item ); } else if( !strcmp( cmd, "find" ) ) { cin >> item; if( bst.isInTree( item ) ) { cout << "true" << endl; } else { cout << "false" << endl; } } else if( !strcmp( cmd, "reset" ) ) { bst = BinarySearchTree(); } else { cout << "Unrecognized command: " << cmd << endl; cout << "Type 'help' for a list of available commands." << endl; } cout << "bst> " << flush; } return 0; }