-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathavl_tree.cpp
541 lines (492 loc) · 16.9 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
539
540
/*
AVL Trees: These are self-balancing trees where the difference between the heights
of left and right subtree(for any node) is not more than 1 and less than -1. This concept
came about so as to ensure that the opeations on BST doesn't become of the order of O(n)
as it becomes more and more skewed. In other words, AVL trees ensures that the balance of
the tree is maintained, thus maintaining the order of the operations on it as O(log n).
properties of AVL:
(1) It is a BST and (2) No duplicate nodes allowed
(3) |height of left subtree - height of right subtree| <=1
*/
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct Node{
Node* left;
Node* right;
int data;
int bf;//balance factor of the node
int height;
};
int find_height(Node* current_node){
if(current_node==NULL) return(0);
//cur_node is the pointer to the current Node
Node* node=current_node;
//The balance factor >0 means that left subtree has greater height
//if it is <0 then, right subtree has greater height
if(node->bf>=0){
node = node->left;
}
else if(node->bf<0){
node = node->right;
}
int height=0;
while(node!=NULL){
height++;
if(node->bf<0){
node = node->right;
}
else if(node->bf>=0){
node = node->left;
}
}
//one is added as the height of a single node is 1 and is added to
//the height of the longest subtree.
return(1+height);
}
//critical node is a node which has a balance factor either
// greater than 1 or less than -1.
int find_balance_factor(Node* node){
return(find_height(node->left)-find_height(node->right));
}
/*
right rotation can be illustrated as follows:
(critical_node) (next_node)
/ (right rotation) / \
(next_node) ====================> x (critical_node)
/ \ /
x y y
Steps performed for above transformation:
(i) store the right node(i.e. y here) of the (next_node) to a temporary node (temp).
(ii) assign (critical_node) as (next_node)'s right child.
(iii) assign node stored in temp as the left node of (critical_node).
(iv) update the height and balance factor of the next_node and critical_node.
*/
Node* right_rotate(Node* critical_node){
Node* next = critical_node->left;
Node* temp = next->right;
next->right=critical_node;
critical_node->left=temp;
//updating height and balance factors for the rotated nodes
next->height=find_height(next);
next->bf = find_balance_factor(next);
critical_node->height=find_height(critical_node);
critical_node->bf = find_balance_factor(critical_node);
return(next);
}
/*
Left rotation can be illustrated as follows:
(critical_node) (next_node)
\ (left rotation) / \
(next_node) ====================> (critical_node) z
/ \ \
y z y
Steps performed for above transformation:
(i) store the left node(i.e. y here) of the (next_node) to a temporary node (temp).
(ii) assign (critical_node) as (next_node)'s left child.
(iii) assign node stored in temp as the right node of (critical_node).
(iv) update the height and balance factor of the next_node and critical_node.
*/
Node* left_rotate(Node* critical_node){
Node* next = critical_node->right;
Node* temp = next->left;
next->left = critical_node;
critical_node->right = temp;
//updating height and balance factors for the rotated nodes
next->height=find_height(next);
next->bf = find_height(next->left)-find_height(next->right);
critical_node->height=find_height(critical_node);
critical_node->bf = find_height(critical_node->left)-find_height(critical_node->right);
return(next);
}
/*
finds the parent node of the given node. It keeps
traversing the tree until it reaches the given node.
It traverses as:
-> if(root->data > node->data) then node is to be searched in the left subtree.
-> if(root->data < node->data) then node is to be searched in the right subtree.
*/
Node* find_parent_node(Node* root, Node* node){
Node* parent=NULL;
while(root!=node){
parent=root;
if(root->data>node->data){
root=root->left;
}
else if(root->data<node->data){
root=root->right;
}
}
return(parent);
}
/*
This function finds the critical node in the AVL Tree
The idea here is that critical nodes closest to the
deleted or inserted node are more likely to be found
in the subtree which contains the "affected area" and within that subtree,
the longest subtree. So, traversal of the tree is done:
(i) to the right if the balance factor of the given node<-1
(ii) to the left if balance factor>1
root of the affected subtree is given as the argument in this
function.
*/
Node* find_critical_node(Node* root){
Node* critical_node=NULL;
while(root!=NULL){
root->bf=find_balance_factor(root);
if(root->bf>1){
critical_node=root;
root=root->left;
}
else if(root->bf<-1){
critical_node=root;
root=root->right;
}
else{
root=root->left;
}
}
return(critical_node);
}
/*
This function finds the inorder predecessor of a node.
For a node, its predecessor can be found by finding the
rightmost element in the left subtree of the tree
whose root is the node whose predecessor is to be found.
*/
Node* find_predecessor(Node* root, Node* &parent){
Node* node=root;
parent=node;
node=node->left;
while(node->right!=NULL){
parent=node;
node=node->right;
}
return(node);
}
/*
this function balances our AVL tree(in case of deletion).
we have the following cases covered below:
(1)if critical node has balance factor>1 and the node to
its left has a balance factor either 0 or 1 then we apply right rotation.Example,
y y
/ /
x(0) or z (1)
/ \ /
w z x
(2)if it has balance factor>1 and balance factor of its left node= -1, then,
z z y
/ (left rotate) / (right rotate) / \
x =======> y ========> x z
\ /
y x
(3)if node has balance factor<-1 and its right node has balance factor
either 0 or -1 then apply left rotation. example of these cases,
x w
\ \
y(-1) or y(0)
\ / \
z x z
(4)if it has balance factor<-1 and balance factor of its left node= 1, then,
w(-2) w x
\ (right rotate) \ (left rotate) / \
y(1) ===============> x ===============> w y
/ \
x y
*/
Node* balance_tree(Node* node){
if(node->bf>1 && (node->left->bf==1 || node->left->bf==0)){
node=right_rotate(node);
}
else if(node->bf>1 && node->left->bf==-1){
node->left=left_rotate(node->left);
node=right_rotate(node);
}
else if(node->bf<-1 && (node->right->bf==-1 || node->right->bf==0)){
node=left_rotate(node);
}
else if(node->bf<-1 && node->right->bf==1){
node->right=right_rotate(node->right);
node=left_rotate(node);
}
return(node);
}
Node* delete_node(Node* node, int key ,int root_value){
if(node==NULL){
return(node);
}
if(node->data>key){
node->left=delete_node(node->left,key,root_value);
}
else if(node->data<key){
node->right=delete_node(node->right,key,root_value);
}
else{
if(node->left==NULL && node->right==NULL){
//for node having no child, just delete it.
node=NULL;
}
else if(node->left!=NULL && node->right==NULL){
//if node to be deleted only has left child
node=node->left;
}
else if(node->left==NULL && node->right!=NULL){
//if node to be deleted only has right child
node=node->right;
}
else{
/*
if the node to be deleted has two child nodes, then we
find its inorder predecessor and the parent node of that
predecessor.
we now have two cases:
(i)if parent of predecessor = node to be deleted then the case
would look like this:
(node to deleted)
/ \
(predecessor) (right node)
/
(node)
In this case, we just assign predecessor->right = (node_to_be_deleted)->right
(ii) (node_to_be_deleted)
/ \
x z
/ \
w y
here, y is found to be the predecessor for the node to be deleted.
so we perform the following:
- y->left = node->left
- y->right= node->right
- x->right = NULL
- update the height and balance factor of predecessor and the parent of
predecessor.
x->right=NULL
*/
Node* parent_of_pred=NULL;//parent of predecessor node
Node* pred=find_predecessor(node,parent_of_pred);
if(parent_of_pred==node){
pred->right=node->right;
}
else{
pred->left=node->left;
pred->right=node->right;
parent_of_pred->right=NULL;
parent_of_pred->height=find_height(parent_of_pred);
parent_of_pred->bf=find_balance_factor(parent_of_pred);
}
node=pred;
}
if(node!=NULL){
//updating the height and balance factor of predecessor node
//after inserting it in deleted node's position
node->height=find_height(node);
node->bf=find_balance_factor(node);
if(node->bf>1 || node->bf<-1){
node=balance_tree(node);
}
}
if(key!=root_value){
//if node deleted isn't root node
return(node);
}
else if(node==NULL){
//if only root node existed originally,
//but now its deleted.
return(node);
}
}
if(key==root_value){
/*
if root node is deleted, then our recursive function won't be able to
backtrack and check the balance of the tree after deletion.
So, here we first find critical node and if any, then we find its
parent as well. We then balance the tree using appropriate rotations and
attach this new node to its parent node.
Then we return the root node.
*/
Node* critical=find_critical_node(node);
if(critical!=NULL){
Node* parent=find_parent_node(node,critical);
critical=balance_tree(critical);
if(parent->data>critical->data){
parent->left=critical;
}
else{
parent->right=critical;
}
}
return(node);
}
/*
if any other node is being deleted, then the function moves backwards
from the position where the node is deleted from, and checks for the
balance factor and updates it along with their height. If critical node
is found then, the tree is balanced with appropriate rotations and that
node is returned.
*/
node->height=find_height(node);
node->bf=find_balance_factor(node);
node=balance_tree(node);
return(node);
}
Node* create_node(int data){
Node* node = new Node;
node->bf=0;
node->height=1;
node->left=NULL;
node->right=NULL;
node->data=data;
return(node);
}
Node* insert_node(Node* node,int data){
if(node==NULL){
node=create_node(data);
return(node);
}
if(node->data>data){
node->left=insert_node(node->left,data);
}
else if(node->data<data){
node->right=insert_node(node->right,data);
}
else{
//if the same data is added more than once then, it returns the existing node with that data
return(node);
}
//Node->height=1+max(find_height(Node->left),find_height(Node->right));
node->height=find_height(node);
node->bf = find_balance_factor(node);
if(node->bf>1 && node->left->bf==1){
node=right_rotate(node);
}
else if(node->bf>1 && node->left->bf==-1){
node->left=left_rotate(node->left);
node=right_rotate(node);
}
else if(node->bf<-1 && node->right->bf==-1){
node=left_rotate(node);
}
else if(node->bf<-1 && node->right->bf==1){
node->right=right_rotate(node->right);
node=left_rotate(node);
}
return(node);
}
/*
preorder traversal of AVL tree and for each node it also displays
their balance factor in brackets.
*/
void preorder_traversal(Node* node){
if(node!=NULL){
cout<<node->data<<"("<<node->bf<<") ";
preorder_traversal(node->left);
preorder_traversal(node->right);
}
}
int main()
{
Node* root=NULL;
int n; //number of nodes in the tree
cout<<"Enter the number of nodes to be inserted: ";
cin>>n;
cout<<"Enter the node values: ";
vector<int>data(n);
for(int i=0;i<data.size();i++){
cin>>data[i];
root=insert_node(root,data[i]);
}
cout<<"\npreorder traversal of the AVL tree:\n";
preorder_traversal(root);
cout<<"\n";
int m;//number of elements to be deleted
cout<<"\nEnter the number of nodes to be deleted: ";
cin>>m;
cout<<"Enter the node values to be deleted: ";
vector<int>deletion_order(m);
for(int i=0;i<deletion_order.size();i++){
cin>>deletion_order[i];
root=delete_node(root,deletion_order[i],root->data);
cout<<"\nafter deleting node with key value = "<<deletion_order[i]<<"\n";
preorder_traversal(root);
cout<<"\n";
}
return 0;
}
/*
Time Complexities:
(1) insertion operation: O(log n)
(2) deletion operation: O(log n)
(3) finding critical node: O(log n)
(4) finding parent node: O(log n)
(5) finding height of the tree: O(log n)
*/
/*
Test Case 1:
(insertion)
Input:
13
14 17 11 7 53 4 13 12 8 60 19 16 20
Output(preorder traversal):
14(0) 11(0) 7(0) 4(0) 8(0) 12(-1) 13(0) 19(0) 17(1) 16(0) 53(0) 20(0) 60(0)
(deletion)
Input:
5
14 13 19 17 12
Output:
after deleting node with key value = 14
13(0) 11(1) 7(0) 4(0) 8(0) 12(0) 19(0) 17(1) 16(0) 53(0) 20(0) 60(0)
after deleting node with key value = 13
12(0) 7(-1) 4(0) 11(1) 8(0) 19(0) 17(1) 16(0) 53(0) 20(0) 60(0)
after deleting node with key value = 19
12(0) 7(-1) 4(0) 11(1) 8(0) 17(-1) 16(0) 53(0) 20(0) 60(0)
after deleting node with key value = 17
12(0) 7(-1) 4(0) 11(1) 8(0) 53(1) 16(-1) 20(0) 60(0)
after deleting node with key value = 12
11(-1) 7(1) 4(0) 53(1) 16(-1) 20(0) 60(0)
*/
/*
The AVL Tree example used in this code:
14
/ \
11 19
/ \ / \
7 12 17 53
/ \ \ / / \
4 8 13 16 20 60
after deleting 14 it should look like this:
13
/ \
11 19
/ \ / \
7 12 17 53
/ \ / / \
4 8 16 20 60
after deleting 13 it should look like this:
12
/ \
7 19
/ \ / \
4 11 17 53
/ / / \
8 16 20 60
after deleting 19 it should look like this:
12
/ \
7 17
/ \ / \
4 11 16 53
/ / \
8 20 60
after deleting 17 it should look like this:
12
/ \
7 53
/ \ / \
4 11 16 60
/ \
8 20
*/
//Other cases that you could try:
//vector<int>data={10,20,30,40,50,25};
//vector<int>data={33,13,53,9,21,61,8,11};
//vector<int>data={14,13};