-
Notifications
You must be signed in to change notification settings - Fork 40
/
binarySearchTree.cpp
463 lines (445 loc) · 11.6 KB
/
binarySearchTree.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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <cmath>
using namespace std;
typedef struct binarySearchTree
{
int data;
struct binarySearchTree *left, *right;
} binarySearchTree;
binarySearchTree *createTree(int key)
{
binarySearchTree *temp = (binarySearchTree *)malloc(sizeof(binarySearchTree));
temp->data = key;
temp->left = NULL;
temp->right = NULL;
return temp;
}
binarySearchTree *insertNode(binarySearchTree *root, int data)
{
if (root == NULL)
{
return createTree(data);
}
if (root->data > data)
{
root->left = insertNode(root->left, data);
}
else if (root->data < data)
{
root->right = insertNode(root->right, data);
}
return root;
}
void preOrder(binarySearchTree *root)
{
if (root != NULL)
{
cout << root->data << " ";
preOrder(root->left);
preOrder(root->right);
}
}
void postOrder(binarySearchTree *root)
{
if (root != NULL)
{
postOrder(root->left);
postOrder(root->right);
cout << root->data << " ";
}
}
void inOrder(binarySearchTree *root)
{
if (root != NULL)
{
inOrder(root->left);
cout << root->data << " ";
inOrder(root->right);
}
}
void levelOrder(binarySearchTree *root)
{
queue<binarySearchTree *> s;
s.push(root);
while (!s.empty())
{
binarySearchTree *temp;
temp = s.front();
s.pop();
cout << temp->data << " ";
if (temp->left != NULL)
{
s.push(temp->left);
}
if (temp->right != NULL)
{
s.push(temp->right);
}
}
cout << endl;
}
void levelOrderSpiral(binarySearchTree *root, vector<int> v)
{
queue<binarySearchTree *> s;
s.push(root);
while (!s.empty())
{
binarySearchTree *temp;
temp = s.front();
// v.push_back(temp->data);
s.pop();
// cout<<temp->data<<" ";
if (temp->left != NULL)
{
s.push(temp->left);
}
if (temp->right != NULL)
{
s.push(temp->right);
}
}
// for(int i=0;i<v.size();i++){
// cout<<v[i]<<" ";
// }
// cout<<endl;
int k = 0, l = 1;
bool even = true;
for (int i = 0; i < v.size();)
{
k = 0;
if (i == 0)
{
cout << v[i] << " ";
k++;
}
else
{
int j = 0;
// cout<<endl<<i<<" "<<l<<endl;
if (!even)
{
for (j = min((int)(pow(2, l) + i - 1), (int)v.size() - 1); j >= i; j--)
{
cout << v[j] << " ";
k++;
}
}
else if (even)
{
for (j = i; j <= pow(2, l) + i - 1 && j < v.size(); j++)
{
cout << v[j] << " ";
k++;
}
}
l++;
even = !even;
}
i += k;
}
}
//using hashing basics, implementing vertical order
void verticalOrder(binarySearchTree *root, int horiontalDistance, map<int, vector<int>> &m)
{
if (root == NULL)
return;
m[horiontalDistance].push_back(root->data);
verticalOrder(root->left, horiontalDistance - 1, m);
//left side nodes will come first or left of root node, so horizontal distance should be negative
verticalOrder(root->right, horiontalDistance + 1, m);
//righ side nodes will come first so horizontal distance should be greator than 0
}
void printVerticalOrder(binarySearchTree *root)
{
map<int, vector<int>> m;
int horizontalDistance = 0;
verticalOrder(root, horizontalDistance, m);
cout << "Vertical order of this binary tree is" << endl
<< "\t";
map<int, vector<int>>::iterator i;
for (i = m.begin(); i != m.end();)
{
if (i == m.begin())
{
cout << "LEFT MOST NODE----> ";
}
for (int j = 0; j < i->second.size(); j++)
{
cout << i->second[j] << " ";
}
i++;
if (i == m.end())
{
cout << " <----- RIGHT MOST NODE";
}
cout << endl
<< "\t";
}
}
int height(binarySearchTree *root)
{
if (root == NULL)
return 0;
return max(height(root->right), height(root->left)) + 1;
}
int internalNode(binarySearchTree *root)
{
if ((root->left == NULL && root->right == NULL) || root == NULL)
{
return 0;
}
return internalNode(root->right) + internalNode(root->left) + 1;
}
int externalNode(binarySearchTree *root)
{
if (root == NULL)
{
return 0;
}
if (root->left == NULL && root->right == NULL)
{
return 1;
}
return externalNode(root->right) + externalNode(root->left);
}
bool searchNode(binarySearchTree *root, int key)
{
if (root == NULL)
{
return false;
}
if (root->data > key)
{
return searchNode(root->left, key);
}
else if (root->data < key)
{
return searchNode(root->right, key);
}
return true;
}
binarySearchTree *deleteNode(binarySearchTree *root, int key)
{
if (root == NULL)
{
return root;
}
if (root->data > key)
{
root->left = deleteNode(root->left, key);
}
else if (root->data < key)
{
root->right = deleteNode(root->right, key);
}
else
{
binarySearchTree *temp;
if (root->left == NULL && root->right == NULL)
{
temp = root->right;
free(root);
return temp;
}
else if (root->left == NULL && root->right != NULL)
{
temp = root->right;
// root=root->right;
free(root);
return temp;
}
else if (root->left != NULL && root->right == NULL)
{
temp = root->left;
// root=root->left;
free(root);
return temp;
}
else if (root->left != NULL && root->right != NULL)
{
temp = root->right;
while (temp->left->left != NULL)
{
temp = temp->left;
}
binarySearchTree *temp2 = temp->right;
swap(temp->left->data, root->data);
temp->left = temp->left->right;
}
}
return root;
}
int predecessor(binarySearchTree *root, int key)
{
if (root != NULL)
{
if (root->data >= key)
{
if (root->left == NULL)
{
return -1;
}
return predecessor(root->left, key);
}
else if (root->data < key)
{
if (root->right == NULL)
{
return root->data;
}
if (root->right->data >= key && root->right->left == NULL)
{
return root->data;
}
return predecessor(root->right, key);
}
}
}
int successor(binarySearchTree *root, int key)
{
if (root != NULL)
{
if (root->data <= key)
{
if (root->right == NULL)
{
return -1;
}
return successor(root->right, key);
}
else if (root->data > key)
{
if (root->left == NULL)
{
return root->data;
}
if ((root->left->data <= key && root->left->right == NULL) || (root->left->data < key && root->right->data > key))
{
return root->data;
}
return successor(root->left, key);
}
}
}
int main()
{
cout << "\t\t\t.................We will here implementing Binary Search Tree................. ";
binarySearchTree *root = NULL;
int x, data, y;
vector<int> v;
vector<int>::iterator it;
do
{
cout << "\nPRESS\n\t1 To insert element\n\t2 To delete element\n\t3 To show the tree in particular orders\n\t4 To search an element\n\t5 To find predecessor or successor of particular event\n\t6 To find the height of the tree\n\t7 To get no of nodes\n\t9 To exit\t";
cin >> x;
switch (x)
{
case 1:
cout << "\nEnter data to be inserted\t";
cin >> data;
v.push_back(data);
if (root == NULL)
{
root = insertNode(root, data);
break;
}
insertNode(root, data);
break;
case 2:
cout << "\nEnter element to be deleted\t";
cin >> data;
it = find(v.begin(), v.end(), data);
v.erase(it);
if (root->data == data)
{
root = deleteNode(root, data);
}
deleteNode(root, data);
break;
case 3:
cout << "\nWhich order you want?PRESS\n\t1 Preorder Traversal\n\t2 PostOrder Traversal\n\t3 Inorder Traversal\n\t4 Breadth First Traversal(level order traversal)\n\t5 Spiral level order traversal\n\t6 Vertical order traversal\n\t7 To have all traversals in one go\t";
cin >> y;
switch (y)
{
case 1:
cout << "\nPreorder traversal is ";
preOrder(root);
cout << endl;
break;
case 2:
cout << "\nPostorder traversal is ";
postOrder(root);
cout << endl;
break;
case 3:
cout << "\nInorder traversal is ";
inOrder(root);
cout << endl;
break;
case 4:
cout << "\nLevel order traversal is ";
levelOrder(root);
cout << endl;
break;
case 5:
cout << "\nSpiral Level order traversal is ";
levelOrderSpiral(root, v);
cout << endl;
break;
case 6:
printVerticalOrder(root);
cout << endl;
break;
case 7:
preOrder(root);
postOrder(root);
inOrder(root);
levelOrder(root);
levelOrderSpiral(root, v);
printVerticalOrder(root);
break;
}
break;
case 4:
cout << "\nEnter element to be searched\t";
cin >> data;
searchNode(root, data);
break;
case 5:
cout << "\nEnter element whose predecessor/successor is to be determined\t";
cin >> data;
cout << "PRESS \n\t1-SUCCESSOR\n\t2-PREDECESOR\t";
cin >> y;
if (y == 1)
{
cout << "\nSuccessor of " << data << " is " << successor(root, data) << endl;
}
else
{
cout << "\nPredecessor of " << data << " is " << predecessor(root, data) << endl;
}
break;
case 6:
cout << "\nHeight of Bst tree is " << height(root) << endl;
break;
case 7:
cout << "\nPRESS\n\t1 for internal node\n\t2 for external nodes\t";
cin >> y;
if (y == 1)
{
cout << "\nNo. of internal nodes is " << internalNode(root) << endl;
}
else
{
cout << "\nNo. of external nodes is " << externalNode(root) << endl;
}
break;
case 9:
exit(0);
}
} while (1);
}