// testbst.cpp // Tests intbst.cpp for CS 24 lab #include "intbst.h" #include using namespace std; int getTest(); // creates two trees (one of which is empty), // and does some simple tests of tree methods int main() { IntBST bst1, bst2; // insert data to bst1 bst1.insert(64); bst1.insert(128); bst1.insert(8); bst1.insert(512); bst1.insert(256); bst1.insert(32); bst1.insert(16); bst1.insert(4); // let user choose one or all tests bool all = true; int testnum = getTest(); if (testnum) all = false; // print and test methods for bst1 cout << "BST: " << endl << " pre-order: "; bst1.printPreOrder(); cout << endl; if (all || testnum == 1) { cout << " in-order: "; bst1.printInOrder(); cout << endl; } if (all || testnum == 2) { cout << " post-order: "; bst1.printPostOrder(); cout << endl; } if (all || testnum == 3) cout << " sum: " << bst1.sum() << endl; if (all || testnum == 4) cout << " count: " << bst1.count() << endl; if (all || testnum == 5) { cout << " contains 16? " << (bst1.contains(16) ? "Y" : "N") << endl; cout << " contains 128? " << (bst1.contains(128) ? "Y" : "N") << endl; cout << " contains 17? " << (bst1.contains(17) ? "Y" : "N") << endl; cout << " contains 512? " << (bst1.contains(512) ? "Y" : "N") << endl; } // test methods for empty bst2 cout << "Empty BST: " << endl << " pre-order: "; bst2.printPreOrder(); cout << endl; if (all || testnum == 1) { cout << " in-order: "; bst2.printInOrder(); cout << endl; } if (all || testnum == 2) { cout << " post-order: "; bst2.printPostOrder(); cout << endl; } if (all || testnum == 3) cout << " sum: " << bst2.sum() << endl; if (all || testnum == 4) cout << " count: " << bst2.count() << endl; if (all || testnum == 5) cout << " contains 16? " << (bst2.contains(16) ? "Y" : "N") << endl; // add tests for optional methods that you attempt to implement // but don't do that until after submitting the basic version return 0; } int getTest() { cout << "Choice of tests:\n" << " 0. all tests\n" << " 1. just printInOrder\n" << " 2. just printPostOrder\n" << " 3. just sum\n" << " 4. just count\n" << " 5. just contains\n"; do { int choice; cout << "Enter choice:\n"; cin >> choice; if (choice >=0 && choice < 6) return choice; cout << "0, 1, 2, 3, 4 or 5 only!\n"; } while (true); }