-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAVLtree.cpp
More file actions
778 lines (708 loc) · 17.9 KB
/
AVLtree.cpp
File metadata and controls
778 lines (708 loc) · 17.9 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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
//HW5 by Wanxiang Xie
//SU Net ID: wxie15 SUID: 408358088
//HW5 AVL Tree
//Due: Friday (Nov. 8) at 11:59PM
//55 points
//This homework requires more efforts. You should get started as soon as possible.
#include <iostream> //to use cout
#include <algorithm> //to use max function such as i = max(a, b);
using namespace std;
//You need to use the following node class for each node of the AVL tree
class node {
public:
int value;
int height;//this is tree height. Leaf node is 1; empty node (i.e., NIL) is 0
node* parent;
node* l_child;
node* r_child;
node() {}
node(int i) { value = i; height = 1; parent = l_child = r_child = nullptr; }
};
class avl_tree {
public:
node* root;
avl_tree() {
root = nullptr;
}
//************************************************************************************
//Implement the following member functions
void add_node(int i);
//if case of a tie, add i to the last of existing nodes with value i in the in-order sequence
void delete_node(int i);
//Delete the node with value i. in case of multiple nodes with value i, delete the first node with value i in the in-order traveral sequence
void in_order_traversal(node* p); //such as 2 5 9 11 11 14 15 15 ...
pair<node*, int> height_update(node* p);
/*
This function will be invoked by add_node and delete_node.
p points to the first node that we need to check for possible height update. We then need to check possible height update toward root.
All nodes whose heights need to be updated will be performed in this function.
The function returns a pair. If no imbalance is detected, the first of the pair will be nullptr; otherwise it will be the address of the action node.
The second of the pair will be 0 if no imbalance is detected; otherwise its value is 1,2,3,4 for RR RL, LL, and LR pattern, respectively.
*/
void L_Rotate(node* p);
//p points to the node at which the rotation will be performed.
void R_Rotate(node* p);
//p points to the node at which the rotation will be performed.
};
void avl_tree::in_order_traversal(node*
p)
{
if (p == nullptr) return;
in_order_traversal(p->l_child);
cout << "(" << p->value << "," << p->height << ")" << " ";
//cout << p->value << " ";
//cout << p->value << ":" << p->height << ":" << height_update(p).second << " ";
in_order_traversal(p->r_child);
if (p == root) {
cout << endl;
}
}
pair<node*, int> avl_tree::height_update(node* p)
{
if (p == nullptr) return pair<node*, int>(nullptr, 0);
node* update = p;
/*node* parent = p->parent;
*/
while (update) {
if (update->l_child && update->r_child) {
update->height = max(update->l_child->height, update->r_child->height) + 1;
}
else if (update->l_child) {
update->height = update->l_child->height + 1;
}
else if (update->r_child) {
update->height = update->r_child->height + 1;
}
else
update->height = 1;
update = update->parent;
}
if (p->l_child && p->r_child) {
if (p->l_child->height - p->r_child->height > 1) { //L pattern
if (p->l_child->l_child && p->l_child->r_child) {
if (p->l_child->l_child->height >= p->l_child->r_child->height) { //LL
return pair<node*, int>(p, 3);
}
else //LR
{
return pair<node*, int>(p, 4);
}
}
else if (p->l_child->l_child) { //LL
return pair<node*, int>(p, 3);
}
else if (p->l_child->r_child) { //LR
return pair<node*, int>(p, 4);
}
else {
return pair<node*, int>(nullptr, 0);
}
}
else if (p->l_child->height - p->r_child->height < -1) { //R pattern
if (p->r_child->l_child && p->r_child->r_child) {
if (p->r_child->r_child->height >= p->r_child->l_child->height) { //RR
return pair<node*, int>(p, 1);
}
else { //RL
return pair<node*, int>(p, 2);
}
}
else if (p->r_child->l_child) { //RL
return pair<node*, int>(p, 2);
}
else if (p->r_child->r_child) { //RR
return pair<node*, int>(p, 1);
}
else
{
return pair<node*, int>(nullptr, 0);
}
}
else { //no violation
return pair<node*, int>(nullptr, 0);
}
}
else if (p->l_child) {
if (p->l_child->height > 1) { //L pattern
if (p->l_child->l_child && p->l_child->r_child) {
if (p->l_child->l_child->height >= p->l_child->r_child->height) { //LL
return pair<node*, int>(p, 3);
}
else //LR
{
return pair<node*, int>(p, 4);
}
}
else if (p->l_child->l_child) { //LL
return pair<node*, int>(p, 3);
}
else if (p->l_child->r_child) { //LR
return pair<node*, int>(p, 4);
}
else {
return pair<node*, int>(nullptr, 0);
}
}
else
{
return pair<node*, int>(nullptr, 0);
}
}
else if (p->r_child) {
if (p->r_child->height > 1) { //R pattern
if (p->r_child->l_child && p->r_child->r_child) {
if (p->r_child->r_child->height >= p->r_child->l_child->height) { //RR
return pair<node*, int>(p, 1);
}
else { //RL
return pair<node*, int>(p, 2);
}
}
else if (p->r_child->l_child) { //RL
return pair<node*, int>(p, 2);
}
else if (p->r_child->r_child) { //RR
return pair<node*, int>(p, 1);
}
else
{
return pair<node*, int>(nullptr, 0);
}
}
else
{
return pair<node*, int>(nullptr, 0);
}
}
else //no violation
{
return pair<node*, int>(nullptr, 0);
}
}
void avl_tree::L_Rotate(node* p)
{
node* p1 = p->r_child;
p->r_child = p1->l_child;
if (p->r_child != nullptr)
p->r_child->parent = p;
if (p->parent != nullptr) {
p1->parent = p->parent;
if (p1->value < p1->parent->value) {
p1->parent->l_child = p1;
}
else {
p1->parent->r_child = p1;
}
}
else {
p1->parent = nullptr;
root = p1;
}
p1->l_child = p;
p->parent = p1;
}
void avl_tree::R_Rotate(node* p)
{
node* p1 = p->l_child;
p->l_child = p1->r_child;
if (p->l_child != nullptr)
p->l_child->parent = p;
if (p->parent != nullptr) {
p1->parent = p->parent;
if (p1->value < p1->parent->value) {
p1->parent->l_child = p1;
}
else {
p1->parent->r_child = p1;
}
}
else {
p1->parent = nullptr;
root = p1;
}
p1->r_child = p;
p->parent = p1;
}
void avl_tree::add_node(int i)
{
if (root == nullptr) {
root = new node(i);
}
else {
node* p = root;
node* p0 = p;
while (p != nullptr) {
p0 = p;
if (i < p->value) {
p = p->l_child;
}
else {
p = p->r_child;
}
}
//add node
if (i < p0->value) {
p0->l_child = new node(i);
p0->l_child->parent = p0;
height_update(p0);
}
else {
p0->r_child = new node(i);
p0->r_child->parent = p0;
height_update(p0);
}
//check violation
node* p_check = p0;
while (p_check) {
if (height_update(p_check).first != nullptr) {
int pattern = height_update(p_check).second;
node* AN = height_update(p_check).first;
if (pattern == 1) { //RR
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 2) { //RL
R_Rotate(AN->r_child);
height_update(AN->r_child->r_child);
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 3) { //LL
R_Rotate(AN);
height_update(AN);
}
else if (pattern == 4) { //LR
L_Rotate(AN->l_child);
height_update(AN->l_child->l_child);
R_Rotate(AN);
height_update(AN);
}
else {}
break;
}
p_check = p_check->parent;
}
}
}
void avl_tree::delete_node(int i)
{
node* p = root;
while (p) {
if (i < p->value) {
p = p->l_child;
}
else if (i > p->value) {
p = p->r_child;
}
else { // i == p->value
while (p->l_child) { //delete lchild first if found
if (i == p->l_child->value)
p = p->l_child;
else
break;
}
break;
}
}
if (p == nullptr) return; //not found
else { //found p = i
if (p->height == 1) { //p is leaf
if (p == root) {
delete p;
root = nullptr;
}
else {
node* p0 = p->parent;
if (p->value < p0->value) {
delete p0->l_child;
p0->l_child = nullptr;
}
else {
delete p0->r_child;
p0->r_child = nullptr;
}
height_update(p0);
node* p_check = p0;
while (p_check) {
if (height_update(p_check).first != nullptr) {
int pattern = height_update(p_check).second;
node* AN = height_update(p_check).first;
if (pattern == 1) { //RR
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 2) { //RL
R_Rotate(AN->r_child);
height_update(AN->r_child->r_child);
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 3) { //LL
R_Rotate(AN);
height_update(AN);
}
else if (pattern == 4) { //LR
L_Rotate(AN->l_child);
height_update(AN->l_child->l_child);
R_Rotate(AN);
height_update(AN);
}
else {}
break;
}
p_check = p_check->parent;
}
}
}
else if (p->height == 2 && !(p->l_child && p->r_child))
{
if (p->l_child) {
p->value = p->l_child->value;
delete p->l_child;
p->l_child = nullptr;
}
else {
p->value = p->r_child->value;
delete p->r_child;
p->r_child = nullptr;
}
height_update(p);
//check violation
node* p_check = p;
while (p_check) {
if (height_update(p_check).first != nullptr) {
int pattern = height_update(p_check).second;
node* AN = height_update(p_check).first;
if (pattern == 1) { //RR
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 2) { //RL
R_Rotate(AN->r_child);
height_update(AN->r_child->r_child);
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 3) { //LL
R_Rotate(AN);
height_update(AN);
}
else if (pattern == 4) { //LR
L_Rotate(AN->l_child);
height_update(AN->l_child->l_child);
R_Rotate(AN);
height_update(AN);
}
else {}
break;
}
p_check = p_check->parent;
}
}
else { //p->height >=2 && p has l_child and r_child || p->height > 2
//find predecessor
if (p->l_child && p->r_child) {
if (p->l_child->height < p->r_child->height) // replace with successor
{
// find successor
node* p0 = p->r_child;
node* p_suc = p->r_child;
while (p0) {
p_suc = p0;
p0 = p0->l_child;
}
node* p_parent = p_suc->parent;
if (p_suc->height == 1) {
if (p_suc->value < p_parent->value) {
p->value = p_suc->value;
p_parent->l_child = nullptr;
}
else {
p->value = p_suc->value;
p_parent->r_child = nullptr;
}
delete p_suc;
}
else {
if (p_suc->value < p_parent->value) {
p->value = p_suc->value;
p_parent->l_child = p_suc->r_child;
}
else {
p->value = p_suc->value;
p_parent->r_child = p_suc->r_child;
}
if (p_suc->r_child->parent)
p_suc->r_child->parent = p_suc->parent;
delete p_suc;
}
height_update(p_parent);
//check violation
node* p_check = p_parent;
while (p_check) {
if (height_update(p_check).first != nullptr) {
int pattern = height_update(p_check).second;
node* AN = height_update(p_check).first;
if (pattern == 1) { //RR
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 2) { //RL
R_Rotate(AN->r_child);
height_update(AN->r_child->r_child);
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 3) { //LL
R_Rotate(AN);
height_update(AN);
}
else if (pattern == 4) { //LR
L_Rotate(AN->l_child);
height_update(AN->l_child->l_child);
R_Rotate(AN);
height_update(AN);
}
else {}
break;
}
p_check = p_check->parent;
}
}
else // replace with predecessor
{
// find predecessor
node* p0 = p->l_child;
node* p_pre = p->l_child;
while (p0) {
p_pre = p0;
p0 = p0->r_child;
}
node* p_parent = p_pre->parent;
if (p_pre->height == 1) {
/* p->value = p_pre->value;*/
if (p_pre->value < p_parent->value) {
p->value = p_pre->value;
p_parent->l_child = nullptr;
}
else {
p->value = p_pre->value;
p_parent->r_child = nullptr;
}
delete p_pre;
}
else {
if (p_pre->value < p_parent->value) {
p->value = p_pre->value;
p_parent->l_child = p_pre->l_child;
}
else {
p->value = p_pre->value;
p_parent->r_child = p_pre->l_child;
}
if (p_pre->l_child->parent)
p_pre->l_child->parent = p_pre->parent;
delete p_pre;
}
height_update(p_parent);
//check violation
node* p_check = p_parent;
while (p_check) {
if (height_update(p_check).first != nullptr) {
int pattern = height_update(p_check).second;
node* AN = height_update(p_check).first;
if (pattern == 1) { //RR
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 2) { //RL
R_Rotate(AN->r_child);
height_update(AN->r_child->r_child);
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 3) { //LL
R_Rotate(AN);
height_update(AN);
}
else if (pattern == 4) { //LR
L_Rotate(AN->l_child);
height_update(AN->l_child->l_child);
R_Rotate(AN);
height_update(AN);
}
else {}
break;
}
p_check = p_check->parent;
}
}
}
else if (p->l_child) {
// find predecessor
node* p0 = p->l_child;
node* p_pre = p->l_child;
while (p0) {
p_pre = p0;
p0 = p0->r_child;
}
node* p_parent = p_pre->parent;
if (p_pre->height == 1) {
/* p->value = p_pre->value;*/
if (p_pre->value < p_parent->value) {
p->value = p_pre->value;
p_parent->l_child = nullptr;
}
else {
p->value = p_pre->value;
p_parent->r_child = nullptr;
}
delete p_pre;
}
else {
if (p_pre->value < p_parent->value) {
p->value = p_pre->value;
p_parent->l_child = p_pre->l_child;
}
else {
p->value = p_pre->value;
p_parent->r_child = p_pre->l_child;
}
if (p_pre->l_child->parent)
p_pre->l_child->parent = p_pre->parent;
delete p_pre;
}
height_update(p_parent);
//check violation
node* p_check = p_parent;
while (p_check) {
if (height_update(p_check).first != nullptr) {
int pattern = height_update(p_check).second;
node* AN = height_update(p_check).first;
if (pattern == 1) { //RR
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 2) { //RL
R_Rotate(AN->r_child);
height_update(AN->r_child->r_child);
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 3) { //LL
R_Rotate(AN);
height_update(AN);
}
else if (pattern == 4) { //LR
L_Rotate(AN->l_child);
height_update(AN->l_child->l_child);
R_Rotate(AN);
height_update(AN);
}
else {}
break;
}
p_check = p_check->parent;
}
}
else if (p->r_child) {
// find successor
node* p0 = p->r_child;
node* p_suc = p->r_child;
while (p0) {
p_suc = p0;
p0 = p0->l_child;
}
node* p_parent = p_suc->parent;
if (p_suc->height == 1) {
if (p_suc->value < p_parent->value) {
p->value = p_suc->value;
p_parent->l_child = nullptr;
}
else {
p->value = p_suc->value;
p_parent->r_child = nullptr;
}
delete p_suc;
}
else {
if (p_suc->value < p_parent->value) {
p->value = p_suc->value;
p_parent->l_child = p_suc->r_child;
}
else {
p->value = p_suc->value;
p_parent->r_child = p_suc->r_child;
}
if (p_suc->r_child->parent)
p_suc->r_child->parent = p_suc->parent;
delete p_suc;
}
height_update(p_parent);
//check violation
node* p_check = p_parent;
while (p_check) {
if (height_update(p_check).first != nullptr) {
int pattern = height_update(p_check).second;
node* AN = height_update(p_check).first;
if (pattern == 1) { //RR
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 2) { //RL
R_Rotate(AN->r_child);
height_update(AN->r_child->r_child);
L_Rotate(AN);
height_update(AN);
}
else if (pattern == 3) { //LL
R_Rotate(AN);
height_update(AN);
}
else if (pattern == 4) { //LR
L_Rotate(AN->l_child);
height_update(AN->l_child->l_child);
R_Rotate(AN);
height_update(AN);
}
else {}
break;
}
p_check = p_check->parent;
}
}
}
}
}
int main() {
//Different test cases will be used during grading.
avl_tree t1;
t1.add_node(45);
t1.in_order_traversal(t1.root);
t1.delete_node(45);
t1.in_order_traversal(t1.root);
t1.add_node(50);
t1.add_node(46);
t1.add_node(30);
t1.add_node(34);
t1.in_order_traversal(t1.root);
t1.delete_node(48);
t1.in_order_traversal(t1.root);
getchar();
getchar();
return 0;
}
//The following is an example showing how to return a pair.
/*
#include <iostream>
using namespace std;
pair<int *, int> f1(){
return { new int {10}, 10 };
}
int main() {
cout << *f1().first << " " << f1().second << endl;
getchar();
getchar();
return 0;
}
*/