-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBINARY SEARCH TREE IMPLEMENTATION.cpp
351 lines (325 loc) · 5.78 KB
/
BINARY SEARCH TREE IMPLEMENTATION.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Binary Search Tree - Implemenation in C++
#include<iostream>
#include <stdlib.h>
#include<queue>
using namespace std;
struct node
{
int data;
node* left;
node* right;
};
struct node* root=NULL;
void insert()
{
struct node *temp, *parent;
temp=(struct node*)malloc(sizeof(struct node));
cout<<"Enter node data : \n";
cin>>temp->data;
temp->left=temp->right=NULL;
parent=root;
if(root == NULL)
{
root=temp;
}
else
{
struct node *curr;
curr=root;
while(curr)
{
parent=curr;
if(temp->data>curr->data)
{
curr=curr->right;
}
else
{
curr=curr->left;
}
}
if(temp->data>parent->data)
{
parent->right=temp;
}
else
{
parent->left=temp;
}
}
}
void search()
{
if(root == NULL)
{
cout<<"Nothing to be searched..."<<endl;
}
else
{
int n;
cout<<"Enter number be searched...\n";
cin>>n;
int c=0;
struct node *t;
t=root;
while(t!=NULL)
{
if(n<t->data)
{
t=t->left;
}
else if(n>t->data)
{
t=t->right;
}
else
{
c++;
break;
}
}
if(c==0)
{
cout<<"Not Found...\n";
}
else
{
cout<<"Found...\n";
}
}
}
void findmax(struct node*root)
{
struct node *t;
t=root;
if(root==NULL)
{
cout<<"No elements present in the tree...\n";
}
else
{
while(t->right!=NULL)
{
t=t->right;
}
cout<<"Maximum element is : "<<t->data<<endl;
}
}
void findmin(struct node* root)
{
struct node *t;
t=root;
if(root==NULL)
{
cout<<"No elements present in the tree...\n";
}
else
{
while(t->left!=NULL)
{
t=t->left;
}
cout<<"Minimum element is : "<<t->data<<endl;
}
}
int findheight(struct node *root)
{
int lht,rht;
if(root==NULL)
{
return -1;
}
else
{
lht=findheight(root->left);
rht=findheight(root->right);
int max=(lht>rht)?lht:rht;
return max+1;
}
}
void LevelOrder(struct node *root)
{
if(root == NULL)
return;
else
{
queue<node*> Q;
Q.push(root);
//while there is at least one discovered node
while(!Q.empty())
{
node* current = Q.front();
Q.pop(); // removing the element at front
cout<<current->data<<endl;
if(current->left != NULL)
Q.push(current->left);
if(current->right != NULL)
Q.push(current->right);
}
}
}
void preorder(struct node *root)
{
if(root == NULL)
return;
else
{
cout<<root->data<<endl;
preorder(root->left);
preorder(root->right);
}
}
void inorder(struct node *root)
{
if(root == NULL)
return;
else
{
inorder(root->left);
cout<<root->data<<endl;
inorder(root->right);
}
}
void postorder(struct node *root)
{
if(root == NULL)
return;
else
{
postorder(root->left);
postorder(root->right);
cout<<root->data<<endl;
}
}
void traverse(struct node *root)
{
if(root==NULL)
{
cout<<"No elements present in the tree...\n";
}
int choice;
cout<<"---------------------------\n";
cout<<"Enter your choice : "<<endl;
cout<<"1. Level Order Traversal\n";
cout<<"2. Pre-Order Traversal\n";
cout<<"3. In-Order Traversal\n";
cout<<"4. Post-Order Traversal\n";
cout<<"Enter your choice : \n";
cin>>choice;
switch(choice)
{
case 1: LevelOrder(root);
break;
case 2: preorder(root);
break;
case 3: inorder(root);
break;
case 4: postorder(root);
break;
default :
system("cls");
cout<<"---------------------------\n";
cout<<"Invalid input\n";
}
}
int totalnodes(struct node *root)
{
if(root==NULL)
{
return 0;
}
else
{
return(totalnodes(root->left)+totalnodes(root->right)+1);
}
}
node* FindMin(node* root)
{
while(root->left != NULL)
root = root->left;
return root;
}
struct node* deletion(struct node *root, int data) {
if(root == NULL)
return root;
else if(data < root->data)
root->left = deletion(root->left,data);
else if (data > root->data)
root->right = deletion(root->right,data);
// Wohoo... I found you, Get ready to be deleted
else {
// Case 1: No child
if(root->left == NULL && root->right == NULL) {
delete root;
root = NULL;
}
//Case 2: One child
else if(root->left == NULL) {
struct node *temp = root;
root = root->right;
delete temp;
}
else if(root->right == NULL) {
struct node *temp = root;
root = root->left;
delete temp;
}
// case 3: 2 children
else {
struct node *temp = FindMin(root->right);
root->data = temp->data;
root->right = deletion(root->right,temp->data);
}
}
return root;
}
int main()
{
int ch,max,min,ht,total;
while(1)
{
cout<<"---------------------------\n";
cout<<"1. Insert an element\n";
cout<<"2. Search\n";
cout<<"3. Find Maximum Element\n";
cout<<"4. Find Minimum Element\n";
cout<<"5. Height of tree\n";
cout<<"6. Total nodes in the tree\n";
cout<<"7. Traverse\n";
cout<<"8. Delete\n";
cout<<"9. Quit\n";
cout<<"Enter your choice : \n";
cin>>ch;
switch(ch)
{
case 1: insert();
break;
case 2: search();
break;
case 3: findmax(root);
break;
case 4: findmin(root);
break;
case 5: ht=findheight(root);
cout<<"Height of tree is : "<<ht<<endl;
break;
case 6: total=totalnodes(root);
cout<<"Total Nodes : "<<total<<endl;
break;
case 7: traverse(root);
break;
case 8: cout<<"Enter data to be deleted... : \n";
int d;
cin>>d;
root=deletion(root,d);
cout<<"Deleted...\n";
break;
case 9: exit(1);
default :
system("cls");
cout<<"---------------------------\n";
cout<<"Invalid input\n";
}
}
return 0;
}