-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL_tree.c
286 lines (215 loc) · 5.42 KB
/
AVL_tree.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
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
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* left;
struct Node* right;
//int height;
};
struct Node* newNode(int data){
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
struct Node* minValNode(struct Node* node){
struct Node* curr = node;
while(curr->left != NULL){
curr = curr->left;
}
return curr;
}
void inorder_succ_pred(struct Node* root, int key,struct Node** pred, struct Node** succ){
//struct Node* succ;
//struct Node* pred;
if(root == NULL) return;
if(root->data == key){
if(root->left != NULL){
struct Node* temp = root->left;
while(temp->right){
temp = temp->right;
}
*pred = temp;
}
if(root->right != NULL){
struct Node* temp = root->right;
while(temp->left){
temp = temp->left;
}
*succ = temp;
}
return;
}
if(key > root->data){
*pred = root;
inorder_succ_pred(root->right,key,pred,succ);
}
else{
*succ = root;
inorder_succ_pred(root->left,key,pred,succ);
}
}
void inorder_rec(struct Node* root){
if(root == NULL){
return;
}
inorder_rec(root->left);
printf("%d ",root->data);
inorder_rec(root->right);
}
void preorder_rec(struct Node* root){
if(root == NULL){
return;
}
printf("%d ",root->data);
preorder_rec(root->left);
preorder_rec(root->right);
}
void postorder_rec(struct Node* root){
if(root == NULL){
return;
}
postorder_rec(root->left);
postorder_rec(root->right);
printf("%d ",root->data);
}
int Max(int a, int b){
if (a>=b)
return a;
else
return b;
}
int height(struct Node* root){
int L;
int R;
if(root == NULL){
return 0;
}
L = height(root->left);
R = height(root->right);
return (1 + Max(L, R) );
}
int Bal_factor(struct Node* root){
if(root == NULL){
return 0;
}
return ( height(root->left) - height(root->right) );
}
struct Node* RightRotate(struct Node* Y){
struct Node* X = Y->left;
struct Node* T2 = X->right;
X->right = Y;
Y->left = T2;
//Y->height = max(height(Y->left), height(Y->right)) + 1;
return X;
}
struct Node* LeftRotate(struct Node* X){
struct Node* Y = X->right;
struct Node* T2 = Y->left;
Y->left = X;
X->right = T2;
return Y;
}
struct Node* insert(struct Node* root,int data){
if(root == NULL){
return newNode(data);
}
else if(data < root->data){
root->left = insert(root->left,data);
}
else if(data > root->data){
root->right = insert(root->right,data);
}
else
return root;
int balance = Bal_factor(root);
//printf("\nBal Fact = %d",balance);
//LL case
if(balance > 1 && data < root->left->data){
return RightRotate(root);
}
//LR case
if(balance > 1 && data > root->left->data){
root->left = LeftRotate(root->left);
return RightRotate(root);
}
//RR case
if(balance < -1 && data > root->right->data){
return LeftRotate(root);
}
//RL case
if(balance < -1 && data < root->right->data){
root->right = RightRotate(root->right);
return LeftRotate(root);
}
return root;
}
struct Node* Delete_Node(struct Node* root, int data){
if(root == NULL){
return root;
}
if(data < root->data){
root->left = Delete_Node(root->left,data);
}
else if(data > root->data){
root->right = Delete_Node(root->right,data);
}
else{
if(root->left == NULL){
struct Node* temp = root->right;
free(root);
return temp;
}
else if(root->right == NULL){
struct Node* temp = root->left;
free(root);
return temp;
}
struct Node* temp = minValNode(root->right);
root->data = temp->data;
root->right = Delete_Node(root->right,temp->data);
}
if(root == NULL){
return root;
}
int balance = Bal_factor(root);
//printf("\nBal Fact = %d",balance);
//LL case
if(balance > 1 && data < root->left->data){
return RightRotate(root);
}
//LR case
if(balance > 1 && data > root->left->data){
root->left = LeftRotate(root->left);
return RightRotate(root);
}
//RR case
if(balance < -1 && data > root->right->data){
return LeftRotate(root);
}
//RL case
if(balance < -1 && data < root->right->data){
root->right = RightRotate(root->right);
return LeftRotate(root);
}
return root;
}
int main(){
struct Node* root = NULL;
root = insert(root, 9);
root = insert(root, 5);
root = insert(root, 10);
root = insert(root, 0);
root = insert(root, 6);
root = insert(root, 11);
root = insert(root, -1);
root = insert(root, 1);
root = insert(root, 2);
printf("\nBefore");
preorder_rec(root);
root = Delete_Node(root,10);
printf("\nAfter");
inorder_rec(root);
return 0;
}