-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl.cpp
182 lines (143 loc) · 3.57 KB
/
avl.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
/*
* Avl tree's are self-balancing binary search tree such that
* the balance factor of each node will be -1 or 0 or 1.
*/
#include <iostream>
struct TreeNode{
TreeNode* left;
TreeNode* right;
int data, height;
TreeNode(const int& data): data(data), left(nullptr), right(nullptr), height(0){}
};
int height(TreeNode* node){
//Return the height of the node
if(node == nullptr){ return -1; }
return node->height;
}
void updateheight(TreeNode*& node){
//Update the height of the node
if(node == nullptr){ return; }
node->height = 1 + std::max(height(node->left), height(node->right));
}
int balance_factor(TreeNode* node){
//Find the balance factor of the given node
if(node != nullptr){ return height(node->right) - height(node->left); }
return 0;
}
void leftrotation(TreeNode*& node){
//Rotate the sub-tree rooted with the node to left
if(node == nullptr){ return; }
TreeNode* a = node;
TreeNode* b = node->right;
a->right = b->left;
b->left = a;
node = b;
updateheight(a);
updateheight(b);
}
void rightrotation(TreeNode*& node){
//Rotate the sub-tree rooted with the node to right
if(node == nullptr){ return; }
TreeNode* a = node;
TreeNode* b = node->left;
a->left = b->right;
b->right= a;
node = b;
updateheight(a);
updateheight(b);
}
void right_leftrotation(TreeNode*& node){
//Rotate the sub-tree rooted with the node to the right and then left
if(node == nullptr){ return; }
rightrotation(node->right);
leftrotation(node);
}
void left_rightrotation(TreeNode*& node){
//Rotate the sub-tree rooted with the node to the left and then right
if(node == nullptr){ return; }
leftrotation(node->left);
rightrotation(node);
}
void insert(TreeNode*& root, const int& data){
/*
* Create and insert the node in the appropriate place of the tree
* Update the height of every node in the tree
* Check the balance factor of the node and do the appropriate rotation
* To make the tree balanced
*/
if(root == nullptr) {
root = new TreeNode(data);
}
else if(root->data < data){ insert(root->right, data); }
else{ insert(root->left, data); }
updateheight(root);
const int initial_bal = balance_factor(root) ;
if(initial_bal == 2){
int r_bal = balance_factor(root->right);
if(r_bal == 1 || r_bal == 0){ leftrotation(root); }
else if(r_bal == -1){ right_leftrotation(root); }
}
else if(initial_bal == -2){
int l_bal = balance_factor(root->left);
if(l_bal == 1 || l_bal == 0){ left_rightrotation(root); }
else if(l_bal == -1){ rightrotation(root); }
}
}
void print(TreeNode* root){
if(root != nullptr){
std::cout << root->data << " ";
print(root->left);
print(root->right);
}
}
int main(){
TreeNode* root = nullptr;
insert(root, 1);
insert(root, 2);
insert(root, 3);
insert(root, 4);
insert(root, 5);
insert(root, 6);
insert(root, 7);
insert(root, 8);
insert(root, 9);
insert(root, 10);
/*
Binary tree
1
\
2
\
3
\
4
\
5
\
6
\
7
\
8
\
9
\
10
Output: 1 2 3 4 5 6 7 8 9 10
Here the balance factor of the root node is 9
Average case Time complexity for insert and remove: O(log(n))
Worst cose Time complexity for insert and remove: O(n)
AVL Tree
4
/ \
2 8
/\ / \
1 3 6 9
/ \ \
5 7 10
Output: 4 2 1 3 8 6 5 7 9 10
Here the balance factor of the root node is 1
Average and worst case time complexity for insert and remove: O(log(n))
*/
print(root);
}