-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL.c
221 lines (166 loc) · 6.04 KB
/
AVL.c
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
/*
In order to balance an AVL tree after insertion, apply the
following rules:-
**We have to search for the imbalanced node first.
1) For a left-Left insertion --> Right rotate once w.r.t the
first imbalanced nodoe.
Example:-
-->Insert 4
11 9
/ \ Right_Rotate w.r.t to 11 / \
9 18 (First imbalanced node = 11) 5 11 (All nodes are balanced --> AVL tree)
/ \ (start from 4 to all the way up and check) / / \
5 10 -----------------------------------------> 4 10 18
/
4
2) For a Right-Right insertion --> Left rotate once w.r.t the
first imbalanced nodoe.
Example:-
-->Insert 19
10 15
/ \ First Imbalanced node = 10 / \
1 15 Left rotate w.r.t 10 10 17 (All nodes are balance --> AVL tree)
/ \ ---------------------------> / \ \
11 17 1 11 19
\
19
3) For a left-Right insertion --> Left rotate once and then
Right rotate once.
Example:-
-->Insert 8
10 10 7
/ \ First inbalanced node = 10 / \ / \ (Balanced AVL tree)
6 12 Left rotate w.r.t child of 10 (w.r.t 6) 7 12 Right rotate w.r.t imbalanced node(10) 6 10
/ \ ----------------------------------------> / \ ---------------------------------------> / / \
4 7 6 8 4 8 12
\ /
8 4
4) For a Right-Left insertion --> Right rotate once w.r.t child of first
imbalnced node and then Left rotate once w.r.t first imbanced node.
Example:-
-->Insert 16
7 7 7
/ \ / \ / \
1 10 First imbalanced node = 10 1 10 1 16 (Balance AVL tree)
\ Right_Rotate w.r.t child of 10 (w.r.t 17) \ Left_Rotate w.r.t first imbalanced node(10) / \
17 ----------------------------------------> 16 ------------------------------------------> 10 17
/ \
16 17
*/
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int key;
struct Node* left;
struct Node* right;
int height;
}node;
int getHeight(node * h)
{
if(h == NULL)
return 0;
return h->height;
}
node *createNode(int value)
{
node *root = (node*)malloc(sizeof(node));
root->key = value;
root->left = NULL;
root->right = NULL;
root->height = 1;
return root;
}
int max(int n1, int n2)
{
if(n1 > n2)
return n1;
return n2;
}
int getBalanceFactor(node *root)
{
if((root->left == NULL && root->right == NULL) || root == NULL)
{
return 0;
}
return getHeight(root->left) - getHeight(root->right);
}
node * rightRotate(node * y)
{
node *x = y->left;
node *T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(getHeight(y->right), getHeight(y->left)) + 1;
x->height = max(getHeight(x->right), getHeight(x->left)) + 1;
return x;
}
node * leftRotate(node * y)
{
node *x = y->right;
node *T1 = x->left;
x->left = y;
y->right = T1;
y->height = max(getHeight(y->right), getHeight(y->left)) + 1;
x->height = max(getHeight(x->right), getHeight(x->left)) + 1;
return x;
}
node *insert(node *root, int key)
{
if(root == NULL)
{
return(createNode(key));
}
if(key < root->key)
{
root->left = insert(root->left, key);
}
else if(key > root->key)
{
root->right = insert(root->right, key);
// return root;
}
root->height = 1 + max(getHeight(root->left), getHeight(root->right));
int bf = getBalanceFactor(root);
// Left-Left insertion
if(bf > 1 && key < root->left->key){
return rightRotate(root);
}
// Right-Right insertion
if(bf < -1 && key > root->right->key){
return leftRotate(root);
}
// Left-Right insertion
if(bf > 1 && key > root->left->key){
root->left = leftRotate(root->left);
return rightRotate(root);
}
//Right-Left insertion
if(bf < -1 && key < root->right->key){
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
void preOrder(node *root)
{
if(root!=NULL)
{
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
int main(){
node * root = NULL;
root = insert(root, 11);
root = insert(root, 10);
root = insert(root, 18);
root = insert(root, 9);
root = insert(root, 5);
root = insert(root, 4);
root = insert(root, 19);
root = insert(root, 105);
preOrder(root);
return 0;
}