-
Notifications
You must be signed in to change notification settings - Fork 91
/
Binary search tree operations.cpp
336 lines (310 loc) · 8.26 KB
/
Binary search tree operations.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
#include<iostream>
#define SPACE 10
using namespace std;
class TreeNode {
public:
int value;
TreeNode * left;
TreeNode * right;
TreeNode() {
value = 0;
left = NULL;
right = NULL;
}
TreeNode(int v) {
value = v;
left = NULL;
right = NULL;
}
};
class BST {
public:
TreeNode * root;
BST() {
root = NULL;
}
bool isTreeEmpty() {
if (root == NULL) {
return true;
} else {
return false;
}
}
void insertNode(TreeNode * new_node) {
if (root == NULL) {
root = new_node;
cout << "Value Inserted as root node!" << endl;
} else {
TreeNode * temp = root;
while (temp != NULL) {
if (new_node -> value == temp -> value) {
cout << "Value Already exist," <<
"Insert another value!" << endl;
return;
} else if ((new_node -> value < temp -> value) && (temp -> left == NULL)) {
temp -> left = new_node;
cout << "Value Inserted to the left!" << endl;
break;
} else if (new_node -> value < temp -> value) {
temp = temp -> left;
} else if ((new_node -> value > temp -> value) && (temp -> right == NULL)) {
temp -> right = new_node;
cout << "Value Inserted to the right!" << endl;
break;
} else {
temp = temp -> right;
}
}
}
}
TreeNode* insertRecursive(TreeNode *r, TreeNode *new_node)
{
if(r==NULL)
{
r=new_node;
cout <<"Insertion successful"<<endl;
return r;
}
if(new_node->value < r->value)
{
r->left = insertRecursive(r->left,new_node);
}
else if (new_node->value > r->value)
{
r->right = insertRecursive(r->right,new_node);
}
else
{
cout << "No duplicate values allowed!" << endl;
return r;
}
return r;
}
void print2D(TreeNode * r, int space) {
if (r == NULL) // Base case 1
return;
space += SPACE; // Increase distance between levels 2
print2D(r -> right, space); // Process right child first 3
cout << endl;
for (int i = SPACE; i < space; i++) // 5
cout << " "; // 5.1
cout << r -> value << "\n"; // 6
print2D(r -> left, space); // Process left child 7
}
void printPreorder(TreeNode * r) //(current node, Left, Right)
{
if (r == NULL)
return;
/* first print data of node */
cout << r -> value << " ";
/* then recur on left sutree */
printPreorder(r -> left);
/* now recur on right subtree */
printPreorder(r -> right);
}
void printInorder(TreeNode * r) // (Left, current node, Right)
{
if (r == NULL)
return;
/* first recur on left child */
printInorder(r -> left);
/* then print the data of node */
cout << r -> value << " ";
/* now recur on right child */
printInorder(r -> right);
}
void printPostorder(TreeNode * r) //(Left, Right, Root)
{
if (r == NULL)
return;
// first recur on left subtree
printPostorder(r -> left);
// then recur on right subtree
printPostorder(r -> right);
// now deal with the node
cout << r -> value << " ";
}
TreeNode * iterativeSearch(int v) {
if (root == NULL) {
return root;
} else {
TreeNode * temp = root;
while (temp != NULL) {
if (v == temp -> value) {
return temp;
} else if (v < temp -> value) {
temp = temp -> left;
} else {
temp = temp -> right;
}
}
return NULL;
}
}
TreeNode * recursiveSearch(TreeNode * r, int val) {
if (r == NULL || r -> value == val)
return r;
else if (val < r -> value)
return recursiveSearch(r -> left, val);
else
return recursiveSearch(r -> right, val);
}
int height(TreeNode * r) {
if (r == NULL)
return -1;
else {
/* compute the height of each subtree */
int lheight = height(r -> left);
int rheight = height(r -> right);
/* use the larger one */
if (lheight > rheight)
return (lheight + 1);
else return (rheight + 1);
}
}
/* Print nodes at a given level */
void printGivenLevel(TreeNode * r, int level) {
if (r == NULL)
return;
else if (level == 0)
cout << r -> value << " ";
else // level > 0
{
printGivenLevel(r -> left, level - 1);
printGivenLevel(r -> right, level - 1);
}
}
void printLevelOrderBFS(TreeNode * r) {
int h = height(r);
for (int i = 0; i <= h; i++)
printGivenLevel(r, i);
}
TreeNode * minValueNode(TreeNode * node) {
TreeNode * current = node;
/* loop down to find the leftmost leaf */
while (current -> left != NULL) {
current = current -> left;
}
return current;
}
TreeNode * deleteNode(TreeNode * r, int v) {
// base case
if (r == NULL) {
return NULL;
}
// If the key to be deleted is smaller than the root's key,
// then it lies in left subtree
else if (v < r -> value) {
r -> left = deleteNode(r -> left, v);
}
// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
else if (v > r -> value) {
r -> right = deleteNode(r -> right, v);
}
// if key is same as root's key, then This is the node to be deleted
else {
// node with only one child or no child
if (r -> left == NULL) {
TreeNode * temp = r -> right;
delete r;
return temp;
} else if (r -> right == NULL) {
TreeNode * temp = r -> left;
delete r;
return temp;
} else {
// node with two children: Get the inorder successor (smallest
// in the right subtree)
TreeNode * temp = minValueNode(r -> right);
// Copy the inorder successor's content to this node
r -> value = temp -> value;
// Delete the inorder successor
r -> right = deleteNode(r -> right, temp -> value);
//deleteNode(r->right, temp->value);
}
}
return r;
}
};
int main() {
BST obj;
int option, val;
do {
cout << "\nWhat operation do you want to perform? " <<
" Select Option number. Enter 0 to exit." << endl;
cout << "1. Insert Node" << endl;
cout << "2. Search Node" << endl;
cout << "3. Delete Node" << endl;
cout << "4. Print/Traversal BST values" << endl;
cout << "5. Height of Tree" << endl;
cout << "6. Clear Screen" << endl;
cout << "0. Exit Program" << endl;
cin >> option;
//Node n1;
TreeNode * new_node = new TreeNode();
switch (option) {
case 0:
break;
case 1:
cout <<"INSERT"<<endl;
cout <<"Enter VALUE of TREE NODE to INSERT in BST: ";
cin >> val;
new_node->value = val;
obj.root= obj.insertRecursive(obj.root,new_node);
//obj.insertNode(new_node);
cout<<endl;
break;
case 2:
cout << "SEARCH" << endl;
cout << "Enter VALUE of TREE NODE to SEARCH in BST: ";
cin >> val;
//new_node = obj.iterativeSearch(val);
new_node = obj.recursiveSearch(obj.root, val);
if (new_node != NULL) {
cout << "Value found " << obj.root<<endl;
} else {
cout << "Value NOT found" << endl;
}
break;
case 3:
cout << "DELETE" << endl;
cout << "Enter VALUE of TREE NODE to DELETE in BST: ";
cin >> val;
new_node = obj.iterativeSearch(val);
if (new_node != NULL) {
obj.deleteNode(obj.root, val);
cout << "Value Deleted" << endl;
} else {
cout << "Value NOT found" << endl;
}
break;
case 4:
cout << "PRINT 2D: " << endl;
obj.print2D(obj.root, 5);
cout << endl;
cout << "Print Level Order BFS: \n";
obj.printLevelOrderBFS(obj.root);
/*cout << endl;
cout <<"PRE-ORDER: ";
obj.printPreorder(obj.root);
cout<<endl;
cout <<"IN-ORDER: ";
obj.printInorder(obj.root);
cout<<endl;
cout <<"POST-ORDER: ";
obj.printPostorder(obj.root);*/
break;
case 5:
cout << "TREE HEIGHT" << endl;
cout << "Height : " << obj.height(obj.root) << endl;
break;
case 6:
system("cls");
break;
default:
cout << "Enter Proper Option number " << endl;
}
} while (option != 0);
return 0;
}