/*--- BSTtester.cpp ------------------------------------------------- Your Name yname@online.tamu-commerce.edu Assignment #5 - Binary Search Trees June 28, 2005 Compiler used Program for testing class template BST. ---------------------------------------------------------------------*/ #include using namespace std; #include "BST.h" int main() { const char MENU[] = "MENU CHOICES\n" "0. Display the menu\n" "1. Check if BST is empty\n" "2. Insert some elements into the BST\n" "3. Search for an element\n" "4. Delete some elements from the BST\n" "5. Graphical representation of BST (sideways)\n" "6. Inorder traversal\n" "7. Preorder traversal\n" "8. Postorder traversal\n" "9. Check destructor\n" "10. Quit the program\n"; // Testing Constructor and empty() BST intBST; // test the class constructor cout << "Constructing empty BST\n"; cout << "BST " << (intBST.empty() ? "is" : "is not") << " empty\n"; // Test Other Operations cout << MENU << endl; int choice; do { cout << "\nEnter a menu choice (0 for menu, 5 to graph, 10 to stop): "; cin >> choice; switch(choice) { case 0: // Display menu cout << MENU << endl; break; case 1: // Checking empty cout << "BST " << (intBST.empty() ? "is" : "is not") << " empty\n"; break; case 2: // Insert elements cout << "\nNow insert a bunch of integers into the BST." "\nTry items not in the BST and some that are in it:\n"; int number; for (;;) { cout << "Item to insert (-999 to stop inserting): "; cin >> number; if (number == -999) break; intBST.insert(number); cout << "Here's the BST:\n"; intBST.graph(cout); } break; case 3: // Searching) cout << "\n\nNow testing the search() operation." "\nTry both items in the BST and some not in it:\n"; for (;;) { cout << "Item to find (-999 to stop searching): "; cin >> number; if (number == -999) break; cout << (intBST.search(number) ? "Found" : "Not found") << endl; } break; case 4: // Deleting elements cout << "\nNow testing the remove() operation." "\nTry both items in the BST and some not in it:\n"; for (;;) { cout << "Item to delete (-999 to stop deleting): "; cin >> number; if (number == -999) break; intBST.remove(number); cout << "Here's the BST:\n"; intBST.graph(cout); } break; case 5: // Graphical representation cout << "Here's the BST (sidewise):\n"; intBST.graph(cout); break; /* ---- inorder test ---- case 6: // Inorder traversal cout << "\nInorder Traversal of BST: \n"; intBST.inorder(cout); cout << endl; break; ---- END inorder test ---- */ /* ---- preorder test ---- case 7: // Test preorder traversal cout << "\nPreorder Traversal of BST: \n"; intBST.preorder(cout); break; ---- END preorder test ----*/ /* ---- postorder test ---- case 8: // Test postorder traversal cout << "\nPostorder Traversal of BST: \n"; intBST.postorder(cout); break; ---- END postorder test ----*/ /* ---- destructor test ---- case 9: // Testing the Destructor cout << "\nNow testing the destructor. Remember to add an\n" "output statement to your destructor to indicate \n" "when it is called.\n"; { BST doomedBST; doomedBST.insert(6); doomedBST.insert(9); doomedBST.insert(5); doomedBST.insert(1); doomedBST.insert(3); doomedBST.insert(7); cout << "\nHere's a BST:\n"; doomedBST.graph(cout); cout << "\n\nLifetime of this BST is over -- now destroy it.\n"; } break; ---- END destructor test ----*/ default: cerr << "BAD CHOICE -- TRY AGAIN\n"; break; case 10: // quit menu break; } // switch } while (choice != 10); } // end main()