5
* Created by fred on Mon Jun 16 2003.
12
* the algorithm explanation for this code comes from purists.org, which seems to have disappeared since
13
* it's a classic AVL tree rebalancing, nothing fancy
26
void AVLTree::MakeNew()
28
for (int i = 0; i < 2; i++)
38
void AVLTree::MakeDelete()
40
for (int i = 0; i < 2; i++) {
42
elem[i]->elem[1 - i] = elem[1 - i];
48
AVLTree *AVLTree::Leftmost()
50
return leafFromDad(NULL, LEFT);
53
AVLTree *AVLTree::leaf(AVLTree *from, Side s)
55
if (from == son[1 - s]) {
57
return son[s]->leafFromDad(this, s);
60
return dad->leaf(this, s);
63
else if (from == son[s]) {
65
return dad->leaf(this, s);
72
AVLTree *AVLTree::leafFromDad(AVLTree *from, Side s)
75
return son[s]->leafFromDad(this, s);
82
AVLTree::RestoreBalances (AVLTree * from, AVLTree * &racine)
87
return dad->RestoreBalances (this, racine);
93
if (from == son[LEFT])
95
if (from == son[RIGHT])
98
return dad->RestoreBalances (this, racine);
101
else if (balance > 0)
103
if (from == son[RIGHT])
108
if (son[LEFT] == NULL)
110
// cout << "mierda\n";
114
AVLTree *b = son[LEFT];
115
AVLTree *e = son[RIGHT];
116
AVLTree *c = son[LEFT]->son[LEFT];
117
AVLTree *d = son[LEFT]->son[RIGHT];
118
if (son[LEFT]->balance > 0)
136
if (r->son[LEFT] == a)
138
if (r->son[RIGHT] == a)
150
if (son[LEFT]->son[RIGHT] == NULL)
152
// cout << "mierda\n";
155
AVLTree *f = son[LEFT]->son[RIGHT]->son[LEFT];
156
AVLTree *g = son[LEFT]->son[RIGHT]->son[RIGHT];
179
if (r->son[LEFT] == a)
181
if (r->son[RIGHT] == a)
187
int old_bal = d->balance;
194
else if (old_bal > 0)
199
else if (old_bal < 0)
207
else if (balance < 0)
209
if (from == son[LEFT])
214
if (son[RIGHT] == NULL)
216
// cout << "mierda\n";
220
AVLTree *b = son[RIGHT];
221
AVLTree *e = son[LEFT];
222
AVLTree *c = son[RIGHT]->son[RIGHT];
223
AVLTree *d = son[RIGHT]->son[LEFT];
225
if (son[RIGHT]->balance < 0)
242
if (r->son[LEFT] == a)
244
if (r->son[RIGHT] == a)
255
if (son[RIGHT]->son[LEFT] == NULL)
257
// cout << "mierda\n";
260
AVLTree *f = son[RIGHT]->son[LEFT]->son[RIGHT];
261
AVLTree *g = son[RIGHT]->son[LEFT]->son[LEFT];
283
if (r->son[LEFT] == a)
285
if (r->son[RIGHT] == a)
290
int old_bal = d->balance;
297
else if (old_bal > 0)
302
else if (old_bal < 0)
315
AVLTree::RestoreBalances (int diff, AVLTree * &racine)
324
if (this == dad->son[RIGHT])
325
return dad->RestoreBalances (1, racine);
326
if (this == dad->son[LEFT])
327
return dad->RestoreBalances (-1, racine);
336
if (son[LEFT] == NULL)
338
// cout << "un probleme\n";
343
AVLTree *b = son[RIGHT];
344
AVLTree *e = son[LEFT];
345
AVLTree *f = e->son[RIGHT];
346
AVLTree *g = e->son[LEFT];
364
if (r->son[LEFT] == a)
366
if (r->son[RIGHT] == a)
375
if (e == r->son[RIGHT])
376
return r->RestoreBalances (1, racine);
377
if (e == r->son[LEFT])
378
return r->RestoreBalances (-1, racine);
382
else if (e->balance == 0)
399
if (r->son[LEFT] == a)
401
if (r->son[RIGHT] == a)
410
else if (e->balance < 0)
412
if (son[LEFT]->son[RIGHT] == NULL)
414
// cout << "un probleme\n";
417
AVLTree *i = son[LEFT]->son[RIGHT]->son[RIGHT];
418
AVLTree *j = son[LEFT]->son[RIGHT]->son[LEFT];
441
if (r->son[LEFT] == a)
443
if (r->son[RIGHT] == a)
448
int oBal = f->balance;
467
if (f == r->son[RIGHT])
468
return r->RestoreBalances (1, racine);
469
if (f == r->son[LEFT])
470
return r->RestoreBalances (-1, racine);
476
else if (balance == 0)
491
else if (balance < 0)
495
if (son[RIGHT] == NULL)
497
// cout << "un probleme\n";
502
AVLTree *b = son[LEFT];
503
AVLTree *e = son[RIGHT];
504
AVLTree *f = e->son[LEFT];
505
AVLTree *g = e->son[RIGHT];
523
if (r->son[LEFT] == a)
525
if (r->son[RIGHT] == a)
534
if (e == r->son[RIGHT])
535
return r->RestoreBalances (1, racine);
536
if (e == r->son[LEFT])
537
return r->RestoreBalances (-1, racine);
541
else if (e->balance == 0)
558
if (r->son[LEFT] == a)
560
if (r->son[RIGHT] == a)
569
else if (e->balance > 0)
571
if (son[RIGHT]->son[LEFT] == NULL)
573
// cout << "un probleme\n";
576
AVLTree *i = son[RIGHT]->son[LEFT]->son[LEFT];
577
AVLTree *j = son[RIGHT]->son[LEFT]->son[RIGHT];
600
if (r->son[LEFT] == a)
602
if (r->son[RIGHT] == a)
607
int oBal = f->balance;
626
if (f == r->son[RIGHT])
627
return r->RestoreBalances (1, racine);
628
if (f == r->son[LEFT])
629
return r->RestoreBalances (-1, racine);
642
if (this == dad->son[RIGHT])
643
return dad->RestoreBalances (1, racine);
644
if (this == dad->son[LEFT])
645
return dad->RestoreBalances (-1, racine);
657
AVLTree::Remove (AVLTree * &racine, bool rebalance)
659
AVLTree *startNode = NULL;
661
int res = Remove (racine, startNode, remDiff);
662
if (res == avl_no_err && rebalance && startNode)
663
res = startNode->RestoreBalances (remDiff, racine);
668
AVLTree::Remove (AVLTree * &racine, AVLTree * &startNode, int &diff)
671
elem[LEFT]->elem[RIGHT] = elem[RIGHT];
673
elem[RIGHT]->elem[LEFT] = elem[LEFT];
674
elem[LEFT] = elem[RIGHT] = NULL;
676
if (son[LEFT] && son[RIGHT])
678
AVLTree *newMe = son[LEFT]->leafFromDad(this, RIGHT);
679
if (newMe == NULL || newMe->son[RIGHT])
681
// cout << "pas normal\n";
684
if (newMe == son[LEFT])
688
newMe->son[RIGHT] = son[RIGHT];
689
son[RIGHT]->dad = newMe;
693
if (dad->son[LEFT] == this)
694
dad->son[LEFT] = newMe;
695
if (dad->son[RIGHT] == this)
696
dad->son[RIGHT] = newMe;
701
AVLTree *oDad = newMe->dad;
705
oDad->son[RIGHT] = newMe->son[LEFT];
706
if (newMe->son[LEFT])
707
newMe->son[LEFT]->dad = oDad;
710
newMe->son[LEFT] = son[LEFT];
711
newMe->son[RIGHT] = son[RIGHT];
714
if (dad->son[LEFT] == this)
715
dad->son[LEFT] = newMe;
716
if (dad->son[RIGHT] == this)
717
dad->son[RIGHT] = newMe;
720
son[LEFT]->dad = newMe;
722
son[RIGHT]->dad = newMe;
724
newMe->balance = balance;
734
if (this == dad->son[LEFT])
736
if (this == dad->son[RIGHT])
741
if (dad->son[LEFT] == this)
742
dad->son[LEFT] = son[LEFT];
743
if (dad->son[RIGHT] == this)
744
dad->son[RIGHT] = son[LEFT];
746
if (son[LEFT]->dad == this)
747
son[LEFT]->dad = dad;
757
if (this == dad->son[LEFT])
759
if (this == dad->son[RIGHT])
764
if (dad->son[LEFT] == this)
765
dad->son[LEFT] = son[RIGHT];
766
if (dad->son[RIGHT] == this)
767
dad->son[RIGHT] = son[RIGHT];
769
if (son[RIGHT]->dad == this)
770
son[RIGHT]->dad = dad;
780
if (this == dad->son[LEFT])
782
if (this == dad->son[RIGHT])
787
if (dad->son[LEFT] == this)
788
dad->son[LEFT] = NULL;
789
if (dad->son[RIGHT] == this)
790
dad->son[RIGHT] = NULL;
795
dad = son[RIGHT] = son[LEFT] = NULL;
804
AVLTree::Insert (AVLTree * &racine, int insertType, AVLTree * insertL,
805
AVLTree * insertR, bool rebalance)
807
int res = Insert (racine, insertType, insertL, insertR);
808
if (res == avl_no_err && rebalance)
809
res = RestoreBalances ((AVLTree *) NULL, racine);
814
AVLTree::Insert (AVLTree * &racine, int insertType, AVLTree * insertL,
824
if (insertType == not_found)
826
// cout << "pb avec l'arbre de raster\n";
829
else if (insertType == found_on_left)
831
if (insertR == NULL || insertR->son[LEFT])
833
// cout << "ngou?\n";
836
insertR->son[LEFT] = this;
838
insertOn(LEFT, insertR);
840
else if (insertType == found_on_right)
842
if (insertL == NULL || insertL->son[RIGHT])
844
// cout << "ngou?\n";
847
insertL->son[RIGHT] = this;
849
insertOn(RIGHT, insertL);
851
else if (insertType == found_between)
853
if (insertR == NULL || insertL == NULL
854
|| (insertR->son[LEFT] != NULL && insertL->son[RIGHT] != NULL))
856
// cout << "ngou?\n";
859
if (insertR->son[LEFT] == NULL)
861
insertR->son[LEFT] = this;
864
else if (insertL->son[RIGHT] == NULL)
866
insertL->son[RIGHT] = this;
869
insertBetween (insertL, insertR);
871
else if (insertType == found_exact)
875
// cout << "ngou?\n";
880
if (insertL->son[RIGHT])
882
insertL = insertL->son[RIGHT]->leafFromDad(insertL, LEFT);
883
if (insertL->son[LEFT])
885
// cout << "ngou?\n";
888
insertL->son[LEFT] = this;
890
insertBetween (insertL->elem[LEFT], insertL);
894
insertL->son[RIGHT] = this;
896
insertBetween (insertL, insertL->elem[RIGHT]);
901
// cout << "code incorrect\n";
909
AVLTree::Relocate (AVLTree * to)
912
elem[LEFT]->elem[RIGHT] = to;
914
elem[RIGHT]->elem[LEFT] = to;
915
to->elem[LEFT] = elem[LEFT];
916
to->elem[RIGHT] = elem[RIGHT];
920
if (dad->son[LEFT] == this)
922
if (dad->son[RIGHT] == this)
923
dad->son[RIGHT] = to;
927
son[RIGHT]->dad = to;
934
to->son[RIGHT] = son[RIGHT];
935
to->son[LEFT] = son[LEFT];
939
void AVLTree::insertOn(Side s, AVLTree *of)
946
void AVLTree::insertBetween(AVLTree *l, AVLTree *r)
949
l->elem[RIGHT] = this;
951
r->elem[LEFT] = this;
959
c-file-style:"stroustrup"
960
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
965
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :