forked from zahinekbal/codeWith-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
415 lines (414 loc) · 7.75 KB
/
BST.cpp
File metadata and controls
415 lines (414 loc) · 7.75 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include<iostream>
using namespace std;
class BSTnode
{
public:
int info;
BSTnode * left;
BSTnode* right;
BSTnode()
{
info = 0;
left = right = NULL;
}
};
class BST
{
public:
BSTnode* root;
int lcount;
int ncount;
void insert(BSTnode*, BSTnode*);
void preorder(BSTnode*);
void postorder(BSTnode*);
void inorder(BSTnode*);
void display(BSTnode*, int);
bool search( BSTnode* );
void count(BSTnode* );
void mirror(BSTnode*);
int maxH(BSTnode*);
void printLevelOrder(BSTnode *);
void printGivenOrder(BSTnode *, int);
void deleteByMerging(BSTnode*&);
void findAndDeleteByMerging(int);
void deleteByCopying(BSTnode*&);
void findAndDeleteByCopying(int);
void search_merge( BSTnode*);
BST()
{
root = NULL;
lcount = 0;
ncount = 0;
}
};
void BST::search_merge( BSTnode* n)
{
BSTnode* p = root;
while (p != NULL)
{
if (p->info == n->info)
{
cout << "\nKey Found";
return;
}
else if (p->info < n->info)
p = p->right;
else
p = p->left;
}
cout << "\n Key not found. Hence inserting .";
insert(root, n);
}
void BST::deleteByMerging(BSTnode*& node)
{
BSTnode *tmp = node;
if (node != 0)
{
if (!node->right)
node = node->left;
else if (node->left == 0)
node = node->right;
else
{
tmp = node->left;
while (tmp->right != 0)
tmp = tmp->right;
tmp->right = node->right;
tmp = node;
node = node->left;
}
delete tmp;
}
}
void BST::findAndDeleteByMerging(int el)
{
BSTnode *node = root, *prev = 0;
while (node != 0)
{
if (node->info == el)
break;
prev = node;
if (el < node->info)
node = node->left;
else node = node->right;
}
if (node != 0 && node->info == el)
if (node == root)
deleteByMerging(root);
else if (prev->left == node)
deleteByMerging(prev->left);
else deleteByMerging(prev->right);
else if (root != 0)
cout << "\nElement" << el << "is not in the tree\n";
else cout << "\nThe tree is empty\n";
}
void BST::findAndDeleteByCopying(int el)
{
BSTnode *temp = root, *prev = 0;
while (temp != 0)
{
if (temp->info == el)
break;
prev = temp;
if (temp->info < el)
temp = temp->right;
else
temp = temp->left;
}
if (temp != 0 && temp->info == el)
if (temp == root)
deleteByCopying(root);
else if (prev->left == temp)
deleteByCopying(prev->left);
else
deleteByCopying(prev->right);
else if (root != 0)
cout << "\ninfo" << el << "is not in the tree" << endl;
else
cout << "\nthe tree is empty" << endl;
}
void BST::deleteByCopying(BSTnode*& node)
{
BSTnode *previous, *tmp = node;
if (node->right == 0)
node = node->left;
else if (node->left == 0)
node = node->right;
else
{
tmp = node->left;
previous = node;
while (tmp->right != 0)
{
previous = tmp;
tmp = tmp->right;
}
node->info = tmp->info;
if (previous == node)
previous->left = tmp->left;
else
previous->right = tmp->left;
}
delete tmp;
}
void BST:: printLevelOrder(BSTnode* n)
{
int h = maxH(n);
int i;
for (int i = 1;i <= h;i++)
printGivenOrder(n, i);
}
void BST::printGivenOrder(BSTnode *n, int level)
{
if (n == NULL)
return;
if (level == 1)
cout << n->info << " ";
else if (level > 1)
{
printGivenOrder(n->left, level - 1);
printGivenOrder(n->right, level - 1);
}
}
void BST:: insert(BSTnode* tree, BSTnode* newnode)
{
if (root == NULL)
{
root = newnode;
return;
}
if (tree->info > newnode->info)
{
if (tree->left != NULL)
insert(tree->left, newnode);
else
tree->left = newnode;
return;
}
else
{
if (tree->right != NULL)
insert(tree->right, newnode);
else
tree->right = newnode;
return;
}
}
void BST::preorder(BSTnode* n)
{
if (root == NULL)
{
cout << "Tree is empty. Cannot Display...";
}
if (n != NULL)
{
cout << n->info << " ";
preorder(n->left);
preorder(n->right);
}
}
void BST:: postorder(BSTnode* n)
{
if (root == NULL)
{
cout << "Tree is empty. Cannot Display...";
}
if (n != NULL)
{
postorder(n->left);
postorder(n->right);
cout << n->info << " ";
}
}
void BST::inorder(BSTnode* n)
{
if (root == NULL)
{
cout << "Tree is empty. Cannot Display...";
}
if (n != NULL)
{
inorder(n->left);
cout << n->info << " ";
inorder(n->right);
}
}
void BST:: display(BSTnode* ptr, int level)
{
int i;
if (ptr != NULL)
{
display(ptr -> right, level + 1);
cout << endl;
if (ptr == root)
cout << "Root->: ";
else
{
for (i = 0;i < level;i++)
cout << " ";
}
cout << ptr->info;
display(ptr->left, level + 1);
}
}
bool BST:: search( BSTnode* n)
{
BSTnode* ptr = root;
while (ptr != NULL)
{
if (ptr->info == n->info)
return 1;
else if (n->info < ptr->info)
ptr = ptr->left;
else
ptr = ptr->right;
}
return 0;
}
void BST:: count(BSTnode* n)
{
if (root == NULL)
{
}
if (n != NULL)
{
count(n->left);
if (n->left == NULL && n->right == NULL)
lcount++;
else
ncount++;
count(n->right);
}
}
void BST:: mirror(BSTnode* m)
{
if (m == NULL)
return;
else
{
BSTnode* temp;
mirror(m->left);
mirror(m->right);
temp = m->left;
m->left = m->right;
m->right = temp;
}
}
int BST::maxH(BSTnode* n)
{
if (n == NULL)
return 0;
else
{
int lheight = maxH(n->left);
int rheight = maxH(n->right);
if (lheight > rheight)
return lheight + 1;
else
return rheight + 1;
}
}
int main()
{
BST bst;
int choice, h;
BSTnode* temp;
int t;
bool f;
char ch;
do
{
cout << "\nMenu:";
cout << "\n 1.Insert";
cout << "\n 2.Preorder";
cout << "\n 3.Postorder";
cout << "\n 4.Inorder";
cout << "\n 5.Display";
cout << "\n 6.Count";
cout << "\n 7.Search.";
cout << "\n 8.Search Merge.";
cout << "\n 9.Mirror";
cout << "\n10.Height";
cout << "\n11.BF traversal";
cout << "\n12.Delete by Merging.";
cout << "\n13.Delete by Copying.";
cout << "\nEnter your choice:";
cin >> choice;
switch (choice)
{
case 1:
temp = new BSTnode;
cout << "\nEnter the info part for the node:";
cin >> temp->info;
bst.insert(bst.root, temp);
break;
case 2:
cout << "\nPre order traversal";
bst.preorder(bst.root);
break;
case 3:
cout << "\nPost order traversal";
bst.postorder(bst.root);
break;
case 4:
cout << "\nIn order traversal";
bst.inorder(bst.root);
break;
case 5:
cout << "\nDisplaying elements";
bst.display(bst.root, 1);
break;
case 6:
bst.count(bst.root);
cout << "\nLeaf count :" << bst.lcount;
cout << "\nNon leaf count: " << bst.ncount;
break;
case 7:
temp = new BSTnode;
cout << "\nEnter the value to be searched :";
cin >> temp->info;
f = bst.search( temp);
if (f == 1)
cout << "Search successful";
else
cout << "Search unsuccessful";
break;
case 8:
cout << "\nEnter the value to be searched :";
cin >> temp->info;
bst.search_merge(temp);
break;
case 9:
cout << "\nBefore mirror";
bst.display(bst.root, 1);
cout << "\nAfter mirror";
bst.mirror(bst.root);
bst.display(bst.root, 1);
break;
case 10:
h = bst.maxH(bst.root);
cout << "Height of the tree is " << h;
break;
case 11:
cout << "Breadth First Traversal";
bst.printLevelOrder(bst.root);
break;
case 12:
cout << "\nEnter the value to be deleted :";
cin >> t;
bst.findAndDeleteByMerging( t);
break;
case 13:
cout << "\nEnter the value to be deleted :";
cin >> t;
bst.findAndDeleteByCopying( t);
break;
default:
cout << "\nWrong Input";
}
//cout << "\nDo you want to continue?(y/n)";
//cin >> ch;
}
while (1);
return 0;
}