-
Notifications
You must be signed in to change notification settings - Fork 302
/
Copy pathavl_tree.cpp
538 lines (417 loc) · 9.69 KB
/
avl_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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#include <iostream>
using namespace std;
/*
Class for defining a node in binary search tree
Has -
int val - Value of the node
node * left - Pointer to the left node
node * right - Pointer to the right node
int heighe - Height of the node
*/
class node {
public:
/*
Constructor for node
Parameters:
int v - sets value of node to v.
*/
explicit node(int v) {
this->val = v;
this->left = NULL;
this->right = NULL;
this->height = 1;
}
/*
Getter functions
*/
int get_val() {
return val;
}
node * get_left() {
return left;
}
node * get_right() {
return right;
}
int get_height() {
return height;
}
/*
Function to return balance factor of a node
Parameters:
node * t - Pointer to node
Returns:
int : balance factor of node t.
*/
int get_bal_fact() {
int ht_lt = get_left() ? get_left()->get_height() : 0;
int ht_rt = get_right() ? get_right()->get_height() : 0;
return (ht_lt-ht_rt);
}
/*
Function to right rotate the node
Parameters:
none
Returns:
node * : Pointer to node which becomes the parent after rotation
*/
node * right_rotate() {
if(this->get_left() == NULL) return this;
node * lt = this->get_left();
node * tmp = lt->get_right();
lt->set_right(this);
this->set_left(tmp);
int ht_lt = this->get_left() ? this->get_left()->get_height() : 0;
int ht_rt = this->get_right() ? this->get_right()->get_height() : 0;
this->set_height(max(ht_lt, ht_rt) + 1);
int ht_lt_1 = lt->get_left() ? lt->get_left()->get_height() : 0;
int ht_rt_1 = lt->get_right() ? lt->get_right()->get_height() : 0;
lt->set_height(max(ht_lt_1, ht_rt_1) + 1);
return lt;
}
/*
Function to left rotate the node
Parameters:
none
Returns:
node * : Pointer to node which becomes the parent after rotation
*/
node * left_rotate() {
if(this->get_right() == NULL) return this;
node * rt = this->get_right();
node * tmp = rt->get_left();
rt->set_left(this);
this->set_right(tmp);
int ht_lt = this->get_left() ? this->get_left()->get_height() : 0;
int ht_rt = this->get_right() ? this->get_right()->get_height() : 0;
this->set_height(max(ht_lt, ht_rt) + 1);
int ht_lt_1 = rt->get_left() ? rt->get_left()->get_height() : 0;
int ht_rt_1 = rt->get_right() ? rt->get_right()->get_height() : 0;
rt->set_height(max(ht_lt, ht_rt) + 1);
return rt;
}
/*
Setter functions
*/
void set_val(int v) {
this->val = v;
}
void set_left(node * l) {
this->left = l;
}
void set_right(node * r) {
this->right = r;
}
void set_height(int h) {
this->height = h;
}
private:
int val;
node * left;
node * right;
int height;
};
/*
Class for defining AVL tree
Has -
node * root - Node pointer to the root of the tree
*/
class AVL {
public:
/*
Class' public member functions
*/
AVL() {
this->root = NULL;
}
/*
Function to search a key
Parameters:
int v - integer value to search in tree
Returns:
node * : Pointer to node having val v
*/
node * search(int v) {
return search_util(root, v);
}
/*
Function to insert a key
Parameters:
int v - integer value to insert in tree
Returns:
nothing
*/
void insert(int v) {
root = insert_util(root, v);
}
/*
Function for inorder traversal
Parameters:
none
Returns:
nothing
*/
void inorder_traversal() {
inorder_util(root);
}
/*
Function for preorder traversal
Parameters:
none
Returns:
nothing
*/
void preorder_traversal() {
preorder_util(root);
}
/*
Function for postorder traversal
Parameters:
none
Returns:
nothing
*/
void postorder_traversal() {
postorder_util(root);
}
/*
Function to delete a key
Parameters:
int v - integer value to delete from tree
Returns:
nothing
*/
void delete_node(int v) {
root = delete_node_util(root, v);
}
private:
node * root;
/*
Class' private member functions
*/
/*
Utility Function to search a key for a subtree
Parameters:
node * t - pointer to root of the tree
int v - integer value to search in tree
Returns:
node * : Pointer to node having val v
*/
node * search_util(node * t, int v) {
if(t == NULL || t->get_val() == v) return t;
else if (v < t->get_val())
return search_util(t->get_left(), v);
else
return search_util(t->get_right(), v);
}
/*
Utility Function to insert a key in a subtree
Parameters:
node * t - pointer to root of the tree
int v - integer value to insert in tree
Returns:
node * : Pointer to node having val v
*/
node * insert_util(node * t, int v) {
if(t == NULL) return new node(v);
if(v < t->get_val()) {
t->set_left(insert_util(t->get_left(), v));
} else if (v > t->get_val()) {
t->set_right(insert_util(t->get_right(), v));
} else {
return t;
}
int ht_lt = t->get_left() ? t->get_left()->get_height() : 0;
int ht_rt = t->get_right() ? t->get_right()->get_height() : 0;
t->set_height(max(ht_lt, ht_rt) + 1);
int bf = t->get_bal_fact();
// cout << "bf of " << t->get_val() << " is : " << bf << "\n";
// Skewed from left
if(bf > 1) {
if(v < t->get_left()->get_val()) {
return t->right_rotate();
} else if (v > t->get_left()->get_val()) {
t->set_left(t->get_left()->left_rotate());
return t->right_rotate();
}
}
// Skewed from right
if(bf < -1) {
if(v > t->get_right()->get_val()) {
return t->left_rotate();
} else if (v < t->get_right()->get_val()) {
t->set_right(t->get_right()->right_rotate());
return t->left_rotate();
}
}
return t;
}
/*
Utility Function for inorder traversal
Parameters:
node * t - Pointer to root of tree
Returns:
nothing
*/
void inorder_util(node * t) {
if(t != NULL) {
inorder_util(t->get_left());
cout << t->get_val() << "\n";
inorder_util(t->get_right());
}
}
/*
Utility Function for preorder traversal
Parameters:
node * t - Pointer to root of tree
Returns:
nothing
*/
void preorder_util(node * t) {
if(t != NULL) {
cout << t->get_val() << "\n";
preorder_util(t->get_left());
preorder_util(t->get_right());
}
}
/*
Utility Function for postorder traversal
Parameters:
node * t - Pointer to root of tree
Returns:
nothing
*/
void postorder_util(node * t) {
if(t != NULL) {
postorder_util(t->get_left());
postorder_util(t->get_right());
cout << t->get_val() << "\n";
}
}
/*
Function to find maximum value node in a tree
Parameters:
node * t - pointer to root node
Returns:
node * - pointer to maximum value node
*/
node * max_node(node * t) {
if(t == NULL) return t;
node * curr = t;
while(curr->get_right())
curr = curr->get_right();
return curr;
}
/*
Utility Function to delete a key from a subtree
Parameters:
node * t - pointer to root of the tree
int v - integer value to delete in tree
Returns:
node * : pointer t
*/
node * delete_node_util(node * t, int v) {
if (t == NULL) return t;
else if (v < t->get_val()) t->set_left(delete_node_util(t->get_left(), v));
else if (v > t->get_val()) t->set_right(delete_node_util(t->get_right(), v));
// Case where t->get_val() == v
// This is the node to be deleted.
else { // Ignore CPPLintBear
// There can be following cases:
// 1) Node has No child or 1 child
// 2) Node has No child or 1 child
// Case 1 - Node has No child or 1 child
if(t->get_left() == NULL) {
node * tmp = t->get_right();
free(t);
return tmp;
} else if (t->get_right() == NULL) {
node * tmp = t->get_left();
free(t);
return tmp;
}
// Case 2 - Node has No child or 1 child
// Find inorder predecessor
node * tmp = max_node(t->get_left());
// set the value
t->set_val(tmp->get_val());
// delete the predecessor
t->set_left(delete_node_util(t->get_left(), tmp->get_val()));
}
if (t == NULL) return t;
int ht_lt = t->get_left() ? t->get_left()->get_height() : 0;
int ht_rt = t->get_right() ? t->get_right()->get_height() : 0;
t->set_height(max(ht_lt, ht_rt) + 1);
int bf = t->get_bal_fact();
if(bf > 1) {
if(t->get_left()->get_bal_fact() >=0) {
return t->right_rotate();
} else if (t->get_left()->get_bal_fact() < 0) {
t->set_left(t->get_left()->left_rotate());
return t->right_rotate();
}
}
// Skewed from right
if(bf < -1) {
if(t->get_left()->get_bal_fact() <= 0) {
return t->left_rotate();
} else if (t->get_left()->get_bal_fact() > 0) {
t->set_right(t->get_right()->right_rotate());
return t->left_rotate();
}
}
return t;
}
};
int main() {
AVL b;
// {1, 2, 9, 293, 41, 15, 52, 3, 121, 7, 1223, 3449, 15, 1 };
b.insert(1);
b.insert(2);
b.insert(9);
b.insert(293);
b.insert(41);
b.insert(15);
b.insert(52);
b.insert(3);
b.insert(121);
b.insert(7);
b.insert(3449);
b.insert(1223);
b.insert(15);
b.insert(0);
b.insert(-1);
cout << "Traversals of Tree after inserting values: \n";
cout << "{1, 2, 9, 293, 41, 15, 52, 3, 121, 7, 1223, 3449, 15, 1}\n";
cout << "Preorder traversal:\n";
b.preorder_traversal();
cout << "Inorder traversal:\n";
b.inorder_traversal();
cout << "Postorder traversal:\n";
b.postorder_traversal();
int search_key = 52;
node * t = b.search(search_key);
if(t != NULL) {
cout << "Key value " << search_key << " is present in tree.\n";
} else {
cout << "Key value " << search_key << " is NOT present in tree.\n";
}
b.delete_node(52);
b.delete_node(121);
b.delete_node(293);
cout << "Deleting values: 52, 121, 293 from the tree.\n";
t = b.search(search_key);
if(t != NULL) {
cout << "Key value " << search_key << " is present in tree.\n";
} else {
cout << "Key value " << search_key << " is NOT present in tree.\n";
}
cout << "Traversals of Tree after deleting values: 52, 121, 293.\n";
cout << "Preorder traversal:\n";
b.preorder_traversal();
cout << "Inorder traversal:\n";
b.inorder_traversal();
cout << "Postorder traversal:\n";
b.postorder_traversal();
return 0;
}