-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVL_Tree.cpp
More file actions
538 lines (496 loc) · 10.6 KB
/
AVL_Tree.cpp
File metadata and controls
538 lines (496 loc) · 10.6 KB
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
// here is the link of theory video to understand insertion in AVL and Rotation Expalanation
//https://www.youtube.com/watch?v=jDM6_TnYIqE
#include<iostream>
class TreeNode{
public:
int balance_factor;
int val;
TreeNode *left , *right, *parent;
TreeNode(int val, TreeNode* parent);
};
TreeNode::TreeNode(int val, TreeNode* parent)
{
this->val = val;
this->parent = parent;
left = right = NULL;
balance_factor = 0;
}
class AVL{
TreeNode* root;
bool LL, RR, LR, RL;
int left_depth, right_depth;
int temp_left, temp_right;
int set_left_depth( TreeNode* temp);
int set_right_depth( TreeNode* temp);
public:
AVL(){ root = NULL;temp_left = temp_right = 0;}
void set_bool(){LL = RR = LR = RL = false;}
void insert(int val, bool flag = true, TreeNode* temp = NULL);
void Inorder(TreeNode* temp = NULL);
void Pre_order(TreeNode* temp = NULL);
TreeNode* Check_depth();
int get_left_depth(TreeNode* temp){return (set_left_depth(temp->left) + 1);}
int get_right_depth(TreeNode* temp){return (set_right_depth(temp->right) + 1);}
TreeNode* get_root(){return root;}
void LL_rotation(TreeNode* temp);
void RR_rotation(TreeNode* temp);
void RL_rotation(TreeNode* temp);
void LR_rotation(TreeNode* temp);
void update_balance_factor(TreeNode* temp);
};
void AVL::insert(int val, bool flag, TreeNode* temp)
{
if(root == NULL)
{
root = new TreeNode(val, NULL);
root->balance_factor = 0;
return;
}
if(flag)
{
set_bool();
temp = root;
}
if(val < temp->val)
{
if(temp->left == NULL)
{
temp->left = new TreeNode(val, temp);
if(temp->parent == NULL)
return;
TreeNode* dep = temp->left;
TreeNode* mov;
dep = dep->parent->parent;
while(dep != NULL)
{
update_balance_factor(dep);
dep = dep->parent;
}
dep = temp->left;
dep = dep->parent->parent;
while(dep != NULL)
{
if(dep->balance_factor < -1 || dep->balance_factor > 1 )
{
while(true)
{
mov = dep;
if(val < mov->val)
{
mov = mov->left;
if(val < mov->val)
{
mov = mov->left;
LL = true;
break;
}
if(val > mov->val)
{
mov = mov->right;
LR = true;
break;
}
}
else if(val > mov->val)
{
mov = mov->right;
if(val > mov->val)
{
mov = mov->right;
RR = true;
break;
}
if(val < mov->val)
{
mov = mov->left;
RL = true;
break;
}
}
}
if(RR)
RR_rotation(dep);
else if(RL)
RL_rotation(dep);
else if(LL)
LL_rotation(dep);
else if(LR)
LR_rotation(dep);
return;
}
dep = dep->parent;
}
return;
}
insert(val, 0, temp->left);
return;
}
if(val > temp->val)
{
if(temp->right == NULL)
{
temp->right = new TreeNode(val, temp);
TreeNode* dep = temp->right;
TreeNode* mov;
dep = dep->parent->parent;
while(dep != NULL)
{
update_balance_factor(dep);
dep = dep->parent;
}
dep = temp->right;
dep = dep->parent->parent;
while(dep != NULL)
{
if(dep->balance_factor < -1 || dep->balance_factor > 1 )
{
while(true)
{
mov = dep;
if(val < mov->val)
{
mov = mov->left;
if(val < mov->val)
{
mov = mov->left;
LL = true;
break;
}
if(val > mov->val)
{
mov = mov->right;
LR = true;
break;
}
}
else if(val > mov->val)
{
mov = mov->right;
if(val > mov->val)
{
mov = mov->right;
RR = true;
break;
}
if(val < mov->val)
{
mov = mov->left;
RL = true;
break;
}
}
}
if(RR)
RR_rotation(dep);
else if(RL)
RL_rotation(dep);
else if(LL)
LL_rotation(dep);
else if(LR)
LR_rotation(dep);
return;
// unbalance
}
dep = dep->parent;
}
return;
}
insert(val, 0, temp->right);
}
}
void AVL::LL_rotation(TreeNode* temp)
{
// LL Imbalance
bool flag = false;
bool connect_left = false;
bool connect_right = false;
std::cout << "(LL Rotation in Active) Rotating Towards Right \n";
temp->balance_factor = 0;
TreeNode* parent = temp->parent;
if(parent != NULL)
flag = true;
if(flag)
{
if(parent->right == temp)
{
parent->right = NULL;
connect_right = true;
}
else if(parent->left == temp)
{
parent->left = NULL;
connect_left = true;
}
}
TreeNode* mid = temp->left;
TreeNode* end = temp->left->left;
TreeNode* right = mid->right;
temp->left = NULL;
mid->right = temp;
temp->parent = mid;
mid->left = end;
end->parent = mid;
temp = mid;
temp->parent = NULL;
mid = mid->right;
while(mid->left != NULL)
mid = mid->left;
mid->left = right;
////
if(connect_right)
{
parent->right = temp;
temp->parent = parent;
}
else if(connect_left)
{
parent->left = temp;
temp->parent = parent;
}
while(temp->parent != NULL)
temp= temp->parent;
root = temp;
}
void AVL::RR_rotation(TreeNode* temp)
{
// RR Imbalance
bool flag = false;
bool connect_left = false;
bool connect_right = false;
std::cout << "(RR Rotation in Active) Rotating Towards Left \n";
temp->balance_factor = 0;
TreeNode* p = temp->parent;
TreeNode* parent = temp->parent;
if(parent != NULL)
flag = true;
if(flag)
{
if(parent->left == temp)
{
parent->left = NULL;
connect_left = true;
}
else if(parent->right == temp)
{
parent->right = NULL;
connect_right = true;
}
}
TreeNode* mid = temp->right;
TreeNode* end = temp->right->right;
TreeNode* left = mid->left;
temp->right = NULL;
mid->left = temp;
temp->parent = mid;
mid->right = end;
end->parent = mid;
temp = mid;
temp->parent = NULL;
mid = mid->left;
while(mid->right != NULL)
mid = mid->right;
mid->right = left;
if(connect_right)
{
parent->right = temp;
temp->parent = parent;
}
else if(connect_left)
{
parent->left = temp;
temp->parent = parent;
}
while(temp->parent != NULL)
temp= temp->parent;
root = temp;
}
void AVL::RL_rotation(TreeNode* temp)
{
// (Right subtree will remain same) ist replace the head node with the last and the head node would go towards left , and manage all subtrees
bool flag = false;
bool connect_left = false;
bool connect_right = false;
std::cout << "(RL Rotation in Active) \n";
temp->balance_factor = 0;
// LR(Left Right) Imbalane
TreeNode* p = temp->parent;
if(p != NULL)
flag = true;
TreeNode* parent = temp->parent;
if(flag)
{
if(parent->left == temp)
{
parent->left = NULL;
connect_left = true;
}
else if(parent->right == temp)
{
parent->right = NULL;
connect_right = true;
}
}
TreeNode* mov = temp->right->left;
TreeNode* right = mov->right;
TreeNode* left = mov->left;
TreeNode* temp_right = temp->right;
temp->right = temp_right->left = NULL;
mov->left = temp;
temp->parent = mov;
if(flag)
{
if(connect_left)
{
mov->parent = parent;
parent->left = mov;
}
else if(connect_right)
{
mov->parent = parent;
parent->right = mov;
}
}
else
mov->parent = NULL;
mov->right = temp_right;
temp_right->parent = mov;
temp = mov;
mov = mov->right;
while(mov->left != NULL)
mov = mov->left;
mov->left = right;
if(right != NULL)
right->parent = mov;
mov = temp->left;
while(mov->right != NULL)
mov = mov->right;
mov->right = left;
if(left != NULL)
left->parent = mov;
while(temp->parent != NULL)
temp = temp->parent;
root = temp;
}
void AVL::LR_rotation(TreeNode* temp)
{
// (left subtree will remain same) ist replace the head node with the last and the head node would go towards right, and manage all subtrees
bool flag = false;
bool connect_left = false;
bool connect_right = false;
temp->balance_factor = 0;
// LR(Left Right) Imbalane
std::cout << "(LR Rotation in Active) \n";
TreeNode* p = temp->parent;
if(p != NULL)
flag = true;
TreeNode* mov = temp->left->right;
TreeNode* parent = temp->parent;
if(flag)
{
if(parent->right == temp)
{
parent->right = NULL;
connect_right = true;
}
else if(parent->left == temp)
{
parent->left = NULL;
connect_left = true;
}
}
TreeNode* right = mov->right;
TreeNode* left = mov->left;
TreeNode* temp_left = temp->left;
temp->left = temp_left->right = NULL;
mov->right = temp;
temp->parent = mov;
if(flag)
{
mov->parent = parent;
if(connect_right)
parent->right = mov;
else if(connect_left)
parent->left = mov;
}
else
mov->parent = NULL;
mov->left = temp_left ;
temp_left->parent = mov;
temp = mov;
mov = mov->right;
while(mov->left != NULL)
mov = mov->left;
mov->left = right;
if(right != NULL)
right->parent = mov;
mov = temp->left;
while(mov->right != NULL)
mov = mov->right;
mov->right = left;
if(left != NULL)
left->parent = mov;
while(temp->parent != NULL)
{
temp = temp->parent;
}
root = temp;
}
void AVL::Inorder(TreeNode* temp)
{
if(root == NULL)
return;
if(temp == NULL)
temp = root;
if(temp->left != NULL)
Inorder(temp->left);
std::cout << temp->val << " ";
if(temp->right != NULL)
Inorder(temp->right);
}
int AVL::set_left_depth(TreeNode* temp)
{
if(temp == NULL)
return -1;
int ldepth = set_right_depth(temp->left);
int rdepth = set_right_depth(temp->right);
if(ldepth > rdepth)
return (ldepth + 1);
return (rdepth + 1);
}
int AVL::set_right_depth(TreeNode* temp)
{
if(temp == NULL)
return -1;
int ldepth = set_right_depth(temp->left);
int rdepth = set_right_depth( temp->right);
if(ldepth > rdepth)
return (ldepth + 1);
return (rdepth + 1);
}
void AVL::Pre_order(TreeNode* temp)
{
if(temp == NULL)
temp = root;
std::cout << temp->val << " ";
if(temp->left != NULL)
Pre_order(temp->left);
if(temp->right != NULL)
Pre_order(temp->right);
}
void AVL::update_balance_factor(TreeNode* temp)
{
temp->balance_factor = get_left_depth(temp) - get_right_depth(temp);
}
int main()
{
AVL v;
v.insert(40);
v.insert(20);
v.insert(10);
v.insert(25);
v.insert(30);
v.insert(22);
v.insert(50);
v.insert(60);
std::cout << "\nPre Order\n[ ";v.Pre_order();std::cout << "]\n";
}