-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
151 lines (123 loc) · 2.53 KB
/
Copy pathmain.cpp
File metadata and controls
151 lines (123 loc) · 2.53 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include"StacklessBST.h"
#include <string>
//returns 1 if str contains only numbers
bool isNumber(string const& str) {
for (auto it = str.begin(); it != str.end(); ++it) {
if (*it<'0' || *it>'9')
return 0;
}
return 1;
}
//returns an integer from input
int InputNum() {
string temp;
cin >> temp;
while (!isNumber(temp) || temp == "" ) {
cout << "Enter a valid number: ";
cin.ignore();
getline(cin, temp);
}
return stoi(temp);
}
void showMenu() {
cout << "1. print trees"<<endl;
cout << "2. print tree between values"<<endl;
cout << "3. add element to the tree"<<endl;
cout << "4. remove element"<<endl;
cout << "5. search element"<<endl;
cout << "6. check if the tree is balance"<<endl;
cout << "7. reverse order of tree"<<endl;
cout << "8. copy tree 1 to tree 2"<<endl;
cout << "9. Quit" << endl;
}
void programLoop(SortedStacklessBST<int>* b1,SortedStacklessBST<int>* b2) {
//temporary variables
int temp;
int temp2;
bool exit = 0;
while (!exit) {
system("cls");
showMenu();
cout << endl << "tree 1: ";
b1->print();
cout << endl;
cout << "Enter menu option: ";
int choice = InputNum();
switch (choice)
{
case 1:
//print
cout << endl << "tree 1: ";
b1->print();
cout << endl << "tree 2: ";
b2->print();
break;
case 2:
//overloaded print
cout << "Enter lower bound: ";
temp = InputNum();
cout << "Enter upper bound: ";
temp2 = InputNum();
try {
b1->print(temp2, temp);
}
catch (invalid_argument & e) {
cout << e.what();
}
break;
case 3:
//add element
cout << "enter integer to add to the tree: ";
temp = InputNum();
b1->insertStackless(temp);
break;
case 4:
//remove element
cout << "Enter element to remove: ";
temp = InputNum();
if (b1->remove(temp))
cout << "removed";
else
cout << "not found";
break;
case 5:
//search
cout << "Enter element to search: ";
temp = InputNum();
if (b1->search(temp))
cout << "Found";
else
cout << "Not found";
break;
case 6:
//balance
if (b1->isBalanced())
cout << "balanced";
else
cout << "not balanced";
break;
case 7:
//revserse Order
b1->reverseOrder();
break;
case 8:
//assignment
*b2 = *b1;
break;
case 9:
//exit
exit = 1;
break;
default:
cout << "Invalid choice";
break;
}
cout << endl;
system("pause");
}
}
int main() {
SortedStacklessBST<int>* b1 = new SortedStacklessBST<int>;
SortedStacklessBST<int>* b2 = new SortedStacklessBST<int>;
programLoop(b1, b2);
}