-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-search-tree.cpp
384 lines (334 loc) · 9.56 KB
/
binary-search-tree.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
#include <iostream>
#include <cmath>
//Structure of the Tree
struct TreeNode{
int data;
TreeNode* left;
TreeNode* right;
TreeNode(const int& data): data(data), left(nullptr), right(nullptr){}
};
TreeNode* find(TreeNode* root, const int& data){
/**
* Find the node that contains the given data and
* return that node
*
* @params: `root` root/parent node of the tree
* @params: `data` data to be find in the tree
* @return: tree node that contains the data
*
* Average case Time Complexity: O(log(n))
* Worst case Time Complexity: O(n)
*
*/
if(root == nullptr) { throw std::runtime_error("Error: find() cannot find the data. The data doesn't exist."); }
else if(root->data == data) { return root; }
else if(root->data < data) { return find(root->right, data); }
else { return find(root->left, data); }
}
void Insert(TreeNode*& root, const int& data){
/**
* Create and Insert the node in the appropriate place of the tree
*
* @params: `root` root/parent node of the tree
* @params: `data` data to be inserted in the tree
* @return: void
*
* Average case Time Complexity: O(log(n))
* Worst case Time Complexity: O(n)
*
*/
if(root == nullptr) { root = new TreeNode(data); }
else if(root->data == data) { throw std::runtime_error("The node already exist. Duplicates not allowed"); }
else if(root->data < data) { Insert(root->right, data); }
else { Insert(root->left, data); }
}
bool isfull(TreeNode* root){
/**
*
* Check if a binary tree is full or not
* A binary tree is full when every node in the
* tree has either two or zero child nodes.
*
* @params: `root` root/parent node of the tree
*
* @return: true if it the binary tree is full else false
*/
if(root == nullptr) { return true; }
if(root->left == nullptr && root->right == nullptr) { return true; }
if((root->left != nullptr && root->right != nullptr) ) { return isfull(root->left) && isfull(root->right); }
return false;
}
int depth(TreeNode* root){
/**
* Find the depth of the left most tree.
* Here the depth of the left most tree is found but
* it is only a matter of preference.
*
* @params: `root` root/parent node of the tree
*
* @return: `d` returns the depth of the left most tree
*/
int d = 0;
while(root != nullptr){
root = root->left;
d++;
}
return d;
}
bool perfect_recursive(TreeNode* cur, int depth, int level = 0){
/**
* A Recursive strategy to check if a tree is perfect or not
*
* A binary tree is perfect if when all the inner node's
* has two children and the all the leaf node's are at the
* same level.
*
* @params: `cur` node of the tree
* @params: `depth` depth of the left most tree
* @params: `level` level of the cur node
*
* @return: true if the binary tree is perfect else false
*/
if(cur == nullptr) { return true; }
if(cur->left == nullptr && cur->right == nullptr) { return depth == level; }
if(cur->left != nullptr && cur->right != nullptr) {
return perfect_recursive(cur->left, depth, level+1) && perfect_recursive(cur->right, depth, level+1);
}
return false;
}
int count_nodes(TreeNode* cur){
/**
* Count the number of node's in the tree
* @params: `cur` node of the tree
*/
if(cur != nullptr){ return 1 + count_nodes(cur->left) + count_nodes(cur->right); }
return 0;
}
int height(TreeNode* cur){
/**
* Find the height of the tree
*
* @params: `cur` node of the tree
*/
if(cur != nullptr){ return 1 + std::max(height(cur->left), height(cur->right)); }
return 0;
}
bool perfect(TreeNode* cur){
/**
* Knowing the height and the number of node's of
* the tree we can find whether the tree is perfect or not
*
* A binary tree is perfect if when all the inner node's
* has two children and the all the leaf node's are at the
* same level.
*/
int h = height(cur) - 1;
int N = count_nodes(cur);
if(N == pow(2, h+1) - 1) { return true; }
return false;
}
bool isperfect(TreeNode* root){
/**
*
* @params: `root` root/parent node of the tree
*
* @return: true if the binary tree is perfect else false
*/
//if(perfect(root)) { return true; }
if(perfect_recursive(root, depth(root) - 1)) { return true; }
return false;
}
void leaf_nodes(TreeNode* root) //Print all leafnode in BST
{
/* * Node which does not have a child is called as LEAF Node.
* Printing the nodes whose left and right pointer are null.
* @params: `root` root/parent node of the tree
*/
if (!root)
return;
if (root->left==NULL && root->right==NULL)
{
std::cout<<root->data<< " ";
return;
}
if (root->left)
leaf_nodes(root->left);
if (root->right)
leaf_nodes(root->right);
}
TreeNode* f_min(TreeNode* root) //Find Minimum element from root
{
/**
* Print the minimum value of the tree
*
* @params: `root` root/parent node of the tree
* @return: void
*/
if(root==NULL)
{
std::cout<<"No value present in the tree"<< std::endl;
return NULL;
}
TreeNode* p=root;
while(p->left!=NULL)
{
p=p->left;
}
return p;
}
TreeNode* f_max(TreeNode* root) //Find Maximum element from root
{
/**
* Print the maximum value of the tree
*
* @params: `root` root/parent node of the tree
* @return: void
*/
if(root==NULL)
{
std::cout<<"No value present in the tree"<< std::endl;
return NULL;
}
TreeNode* p=root;
while(p->right!=NULL)
{
p=p->right;
}
return p;
}
TreeNode* bstdelete(TreeNode* root, int x)
{
TreeNode* m;
if(root==NULL)
{
std::cout<<"NOT FOUND!!";
return root;
}
if(x < root->data)
{
root->left=bstdelete(root->left,x);
return root;
}
if(x>root->data)
{
root->right=bstdelete(root->right,x);
return root;
}
if(root->left==NULL && root->right==NULL)
{
m=root;
delete m;
return (NULL);
}
else if(root->left==NULL)
{
m=root;
root=root->right;
delete m;
return (root);
}
else if(root->right==NULL)
{
m=root;
root=root->left;
delete m;
return (root);
}
m=f_min(root->right);
root->data=m->data;
root->right=bstdelete(root->right, m->data);
return (root);
}
void print(TreeNode* root){
/**
* Print the tree in an inorder fashion
*
* @params: `root` root/parent node of the tree
* @return: void
*/
if(root != nullptr){
print(root->left);
std::cout << root->data << " ";
print(root->right);
}
}
void free(TreeNode* root){
/*
* Free up the memory in the heap
*
* @params: `root` root/parent node of the tree
*/
if(root != nullptr){
free(root->left);
free(root->right);
delete root;
root = nullptr;
}
}
int main(){
TreeNode* root = nullptr;
Insert(root, 37);
Insert(root, 19);
Insert(root, 4);
Insert(root, 22);
Insert(root, 51);
Insert(root, 55);
Insert(root, 42);
Insert(root, 20);
Insert(root, 11);
Insert(root, 2);
print(root);
TreeNode* n = find(root, 19);
std::cout << "\nValue of n: " << n->data << std::endl;
if(isfull(root)) { std::cout << "The binary tree is FULL" << std::endl; }
else { std::cout << "The binary tree is not FULL" << std::endl; }
if(isperfect(root)) { std::cout << "The binary tree is PERFECT" << std::endl; }
else { std::cout << "The binary tree is not PERFECT" << std::endl; }
std::cout << "Leaf Nodes present in the binary tree are : ";
leaf_nodes(root);
std::cout<< std::endl;
n = f_max(root);
if(n)
{
std::cout<<"Maximum Value Present in the tree is : "<<n->data<< std::endl;
}
n=f_min(root);
if(n)
{
std::cout<<"Minimum Value Present in the tree is : "<<n->data<< std::endl;
}
root = bstdelete(root, 19);
std::cout<<"Binary search Tree after Deletion is : "<<std::endl;;
print(root);
/*
Tree structure
37
/ \
19 51
/ \ / \
4 22 42 55
/\ /
2 11 20
OUTPUT:
2 4 11 19 20 22 37 42 51 55
Value of n: 19
The binary tree is not FULL
The binary tree is not PERFECT
Leaf Nodes present in the binary tree are : 2 11 20 42 55
Maximum Value Present in the tree is : 55
Minimum Value Present in the tree is : 2
Binary search Tree after Deletion is :
2 4 11 20 22 37 42 51 55
Tree structure after deletion
37
/ \
11 51
/ \ / \
4 22 42 55
/ /
2 20
*/
// free the memory
free(root);
return 0;
}