#include #include #include "BTree.h" #ifndef TRUE #define TRUE 1 #endif #ifndef FALSE #define FALSE 0 #endif static const int MAX_DATA_LEN = 200; int main(void) { int menu_choice; int key; char data[MAX_DATA_LEN]; char *outdata, *newdata; BTree bt(3); int valid; do { cout << "Menu:\n" << " 1. Insert item\n" << " 2. Find item\n" << " 3. Remove item\n" << " 4. Print tree\n" << " 5. Quit program\n\n"; valid = FALSE; while (!valid) { cout << "Please make a selection [1-5]: "; cin >> menu_choice; if (cin.eof()) { menu_choice = 5; valid = TRUE; } if (!cin) { cout <<"Invalid input.\n"; cin.clear(); cin.ignore(200, '\n'); } else valid = TRUE; } cin.ignore(200, '\n'); switch (menu_choice) { case 1: valid = FALSE; while (!valid) { cout << "Please enter a key: "; cin >> key; if (!cin) { cout <<"Invalid input.\n"; cin.clear(); cin.ignore(200, '\n'); } else valid = TRUE; } cin.ignore(200, '\n'); cout << "Please enter data: "; cin.getline(data, MAX_DATA_LEN, '\n'); newdata = new char[strlen(data) + 1]; strcpy(newdata, data); if (bt.find(key, outdata)) cout << "( " << key << " , " << outdata << " ) already in tree.\n"; else bt.insert(key, newdata); break; case 2: valid = FALSE; while (!valid) { cout << "Please enter a key: "; cin >> key; if (!cin) { cout <<"Invalid input.\n"; cin.clear(); cin.ignore(200, '\n'); } else valid = TRUE; } cin.ignore(200, '\n'); if (bt.find(key, outdata)) cout << "( " << key << " , " << outdata << " )\n"; else cout << "Key not found.\n"; break; case 3: valid = FALSE; while (!valid) { cout << "Please enter a key: "; cin >> key; if (!cin) { cout <<"Invalid input.\n"; cin.clear(); cin.ignore(200, '\n'); } else valid = TRUE; } cin.ignore(200, '\n'); if (bt.find(key, outdata)) { bt.remove(key); cout << "Deleted ( " << key << " , " << outdata << " )\n"; delete [] outdata; } else cout << "Key not found.\n"; break; break; case 4: bt.print(cout); break; case 5: break; default: cout << "Invalid option.\n"; } cin.clear(); cout << endl; } while (menu_choice != 5); return 0; }