-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
304 lines (256 loc) · 6.48 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <iostream>
#include <queue>
#define COUNT 10
using namespace std;
struct Node
{
int key;
struct Node *left , *right;
};
struct Node *newnode(int item){
Node *temp = new Node;
temp->key = item;
temp->left = NULL;
temp->right = NULL;
return temp;
}
void printTree(struct Node * root , int space){
if (root==NULL){
return ;
}
printTree(root->right , space);
space += COUNT;
cout<<endl;
for (int i = COUNT ; i < space ; i++)
cout<<" ";
cout<<endl<<root->key;
printTree(root->left , space);
}
void print2D(struct Node * root){
printTree(root , 0);
}
struct Node *insert(struct Node * node , int key){
if (node==NULL){
return newnode(key);
}
else if (key < node->key){
node->left = insert(node->left, key);
}
else if (key > node->key){
node->right = insert(node->right, key);
}
return node;
}
struct Node *search(struct Node * root , int key){
if (root == NULL || root->key == key ){
return root;
}
else if (root->key > key){
root = root->left;
return search(root , key);
}
else if (root->key < key){
root = root->right;
return search(root , key);
}
return root;
}
int minValueLeaf(struct Node * root){
struct Node * current = root;
while (current->left != NULL){
current = current->left;
}
return (current->key);
}
struct Node * minValue(struct Node * root){
struct Node * current = root;
while (current->left != NULL){
current = current->left;
}
return current;
}
int maxValueLeaf(struct Node * root){
struct Node * current = root;
while (current->right != NULL){
current = current->right;
}
return (current->key);
}
struct Node *Delete(struct Node * root , int key){
if (root == NULL){
return root;
}
else if (key > root->key){
root->right = Delete(root->right , key);
}
else if (key < root->key){
root->left = Delete(root->left , key);
}
else {
if (root->right == NULL){
struct Node *obj = root->left;
delete(root);
return obj;
}
else if (root->left == NULL){
struct Node *obj = root->right;
delete(root);
return obj;
}
struct Node *obj = minValue(root->left);
root->key = obj->key;
root->left = Delete(root->left , key);
}
return root;
}
int maxHeight(struct Node * node){
if (node == NULL){
return 0;
} else {
int lheight;
int rheight;
lheight = maxHeight(node->left);
rheight = maxHeight(node->right);
if (lheight > rheight){
return lheight+1;
} else {
return rheight+1;
}
}
}
int getCount(struct Node * root){
if (!root){
return 0;
}
queue<Node *> Q;
int count = 0;
Q.push(root);
while (!Q.empty()){
struct Node * temp = Q.front();
Q.pop();
if (temp->left && temp->right){
count++;
}
if (temp->left != NULL){
Q.push(temp->left);
}
if (temp->right != NULL){
Q.push(temp->right);
}
}
return count;
}
void loop (struct Node * root);
void menu(struct Node * root){
int choice = 0;
int searchElement;
int deleteElement;
cout<<" 1 : search element in bst"<<endl;
cout<<" 2 : delete element in bst"<<endl;
cout<<" 3 : print the tree"<<endl;
cout<<" 4 : get the smallest value in the tree"<<endl;
cout<<" 5 : get the largest value in the tree"<<endl;
cout<<" 6 : get the height of the tree"<<endl;
cout<<" 7 : get the element's count in the tree"<<endl;
cout<<"enter your choice"<<endl;
cin>>choice;
cout<<endl;
switch (choice){
case 1:
cout<<"enter element which you want to search"<<endl;
cin>>searchElement;
cout<<endl;
if (search(root , searchElement)){
cout<<"this element is in the tree";
} else {
cout<<"this element is not in the tree";
}
cout<<endl;
loop(root);
break;
case 2:
cout<<"enter element which you want to delete"<<endl;
cin>>deleteElement;
cout<<endl;
Delete(root , deleteElement);
print2D(root);
cout<<endl;
loop(root);
break;
case 3:
cout<<"binary search tree"<<endl;
print2D(root);
cout<<endl;
loop(root);
break;
case 4:
cout<<"the smallest element in the tree is"<<endl;
cout<<minValueLeaf(root);
cout<<endl;
loop(root);
break;
case 5:
cout<<"the largest element in the tree is"<<endl;
cout<<maxValueLeaf(root);
cout<<endl;
loop(root);
break;
case 6:
cout<<"the height of the tree " <<endl;
cout<<maxHeight(root);
cout<<endl;
loop(root);
break;
case 7:
cout<<"element's count of the tree"<<endl;
cout<<getCount(root);
cout<<endl;
loop(root);
break;
default:
cout<<"sorry your choice is not here try another choice"<<endl;
menu(root);
break;
}
}
int main() {
struct Node *root;
root = new Node;
int elements[100];
int Size = 0;
int searchElement;
int deleteElement;
cout<<"enter elements number of the tree"<<endl;
cin>>Size;
cout<<endl;
cout<<"enter tree's elements"<<endl;
for (int i = 0; i < Size; i++) {
cin>>elements[i];
insert(root , elements[i]);
}
menu(root);
}
void loop(struct Node * root)
{
char choice ;
cout<<" f : Continue to the functions"<<endl;
cout<<" t : create new tree"<<endl;
cout<<" n : Exit"<<endl;
cout<<"enter your choice"<<endl;
cin>>choice;
cout<<endl;
while (true){
if (choice == 'f'){
menu(root);
}
else if (choice == 't'){
main();
}
else if (choice == 'n'){
return;
} else {
cout<<"sorry your choice is not here try another choice"<<endl;
loop(root);
}
}
}