-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
83 lines (74 loc) · 2.04 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <limits>
#include "BSTree.h"
using namespace std;
void printOrders(BSTree *tree) {
cout << "Preorder = ";
tree->preOrder();
cout << endl;
cout << "Inorder = ";
tree->inOrder();
cout << endl;
cout << "Postorder = ";
tree->postOrder();
cout << endl;
}
int menu() {
int choice = 0;
cout << endl << "Enter menu choice: ";
cout << endl;
cout
<< "1. Insert" << endl
<< "2. Remove" << endl
<< "3. Print" << endl
<< "4. Search" << endl
<< "5. Smallest" << endl
<< "6. Largest" << endl
<< "7. Height" << endl
<< "8. Quit" << endl;
cin >> choice;
// fix buffer just in case non-numeric choice entered
// also gets rid of newline character
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return choice;
}
int main( ) {
BSTree tree;
int choice = menu();
string entry;
while (choice != 8) {
if (choice == 1) {
cout << "Enter string to insert: " << endl;
getline(cin, entry);
tree.insert(entry);
} else if (choice == 2) {
cout << "Enter string to remove: " << endl;
getline(cin, entry);
tree.remove(entry);
} else if (choice == 3) {
printOrders(&tree);
} else if (choice == 4) {
cout << "Enter string to search for: " << endl;
getline(cin, entry);
if(!tree.search(entry))
cout << "Not Found" << endl;
else
cout << "Found" << endl;
} else if (choice == 5) {
cout << "Smallest: ";
cout << tree.smallest() << endl;
} else if (choice == 6) {
cout << "Largest: ";
cout << tree.largest() << endl;
} else if (choice == 7) {
cout << "Enter string: " << endl;
getline(cin, entry);
cout << "Height of subtree rooted at " << entry << ": ";
cout << tree.height(entry) << endl;
}
//fix buffer just in case non-numeric choice entered
choice = menu();
}
return 0;
}