-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathHedgeTree.hpp
1027 lines (973 loc) · 32.5 KB
/
HedgeTree.hpp
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright IBM Corp. and others 2000
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#ifndef HEDGETREE_INCL
#define HEDGETREE_INCL
#include <stdint.h>
#include <string.h>
#include "env/FrontEnd.hpp" // TR_FrontEnd (ptr only)
#include "compile/Compilation.hpp"
#include "env/IO.hpp"
#include "env/TRMemory.hpp"
#include "infra/Assert.hpp"
// Implementation of Symmetric Binary B-Trees
// (also called "hedge trees")
//
// Hedge trees have the properties:
// - every node has at most 2 pointers, each either to a sibling or a child
// - there are never two consecutive sibling pointers on any search path
// - all leaf nodes (ones with zero or one child) are at the same height
//
// Search paths are at most 2 * height.
// Tree balancing is needed less frequently than with AVL trees.
template <class T> class TR_HedgeNode
{
public:
TR_ALLOC(TR_Memory::HedgeTree)
TR_HedgeNode(int32_t key) {initialize(key);}
void initialize(int32_t key)
{
_key = key; _left = NULL; _right = NULL; _parent = NULL;
_leftSibling = false; _rightSibling = false;
}
int32_t getKey() {return _key;}
T *left() {return _left;}
T *right() {return _right;}
T *&leftReference() {return _left;}
T *&rightReference() {return _right;}
void setLeft(T *node) {_left = node; if (node) node->setParent((T*)this);}
void setRight(T *node) {_right = node; if (node) node->setParent((T*)this);}
bool isLeftSibling() {return _leftSibling;}
bool isRightSibling() {return _rightSibling;}
void setRightSibling(bool b) {_rightSibling = b;}
void setLeftSibling(bool b) {_leftSibling = b;}
T *getParent() {return _parent;}
void setParent(T *node) {_parent = node;}
bool verify(T *parent)
{
if (isLeftSibling() && (_left->isLeftSibling() || _left->isRightSibling()))
{
TR_ASSERT(0, "Double sibling chain for key %d", getKey());
return false;
}
if (isRightSibling() && (_right->isLeftSibling() || _right->isRightSibling()))
{
TR_ASSERT(0, "Double sibling chain for key %d", getKey());
return false;
}
if (_parent != parent)
{
TR_ASSERT(0, "Bad parent for key %d", getKey());
return false;
}
if (!_left && isLeftSibling())
{
TR_ASSERT(0, "Null left sibling for key %d", getKey());
return false;
}
if (!_right && isRightSibling())
{
TR_ASSERT(0, "Null right sibling for key %d", getKey());
return false;
}
if (!_left)
{
if (_right)
{
if (!isRightSibling())
{
TR_ASSERT(0, "Non-leaf is missing child for key %d", getKey());
return false;
}
return _right->verify((T*)this);
}
return true;
}
if (!_right)
{
if (!isLeftSibling())
{
TR_ASSERT(0, "Non-leaf is missing child for key %d", getKey());
return false;
}
return _left->verify((T*)this);
}
int32_t lh = _left->height();
int32_t rh = _right->height();
if (!isLeftSibling()) lh++;
if (!isRightSibling()) rh++;
if (lh != rh)
TR_ASSERT(0, "Lhs and rhs different heights for key %d", getKey());
return lh == rh;
}
int32_t height()
{
int32_t h;
if (_left)
{
h = _left->height();
if (!isLeftSibling()) h++;
}
else if (_right)
{
h = _right->height();
if (!isRightSibling()) h++;
}
else
h = 1;
return h;
}
#if DEBUG
void printTree(TR::FILE *outFile, int32_t indent, char *prefix, bool isLeftChild, bool isSibling)
{
int32_t prefixPos = isSibling ? indent : indent-3;
if (prefixPos > 0)
{
if (isLeftChild)
prefix[prefixPos] = '|';
else
prefix[prefixPos] = ' ';
}
if (_right)
{
if (isRightSibling())
_right->printTree(outFile, indent, prefix, false, true);
else
_right->printTree(outFile, indent+5, prefix, false, false);
}
else
trfprintf(outFile, "%.*s\n", prefixPos+1, prefix);
if (isSibling)
trfprintf(outFile, "%.*s%d\n", prefixPos, prefix, getKey());
else
trfprintf(outFile, "%.*s-->%d\n", prefixPos, prefix, getKey());
if (prefixPos > 0)
{
if (!isLeftChild)
prefix[prefixPos] = '|';
else
prefix[prefixPos] = ' ';
}
if (_left)
{
if (isLeftSibling())
_left->printTree(outFile, indent, prefix, true, true);
else
_left->printTree(outFile, indent+5, prefix, true, false);
}
else
trfprintf(outFile, "%.*s\n", prefixPos+1, prefix);
prefix[prefixPos] = ' ';
}
#else
void printTree(TR::FILE *outFile, int32_t indent, char *prefix, bool isLeftChild, bool isSibling) {}
#endif
private:
T *_left;
T *_right;
T *_parent;
int32_t _key;
bool _leftSibling;
bool _rightSibling;
};
template <class T> class TR_HedgeTree
{
public:
TR_ALLOC(TR_Memory::HedgeTree)
TR_HedgeTree() : _root(NULL) {}
T *getRoot() {return _root;}
void setRoot(T *node) {_root = node; if (node) node->setParent(NULL);}
T *&rootReference() {return _root;}
bool isEmpty() {return _root == NULL;}
bool verify()
{
return _root ? _root->verify(NULL) : true;
}
T *find(int32_t key)
{
// Search the tree for the node with the given key.
//
for (T *node = _root; node; )
{
if (key < node->getKey())
node = node->left();
else if (key > node->getKey())
node = node->right();
else
return node;
}
return NULL;
}
#if DEBUG
void print(TR::FILE *outFile)
{
if (outFile == NULL)
return;
if (!_root)
{
trfprintf(outFile, " empty\n");
return;
}
char prefix[80];
memset(prefix, ' ', 80);
_root->printTree(outFile, 3, prefix, false, false);
trfflush(outFile);
}
#else
void print(TR::FILE *outFile) {}
#endif
private:
T *_root;
};
template <class T> class TR_HedgeTreeIterator
{
public:
TR_ALLOC(TR_Memory::HedgeTree)
TR_HedgeTreeIterator()
: _tree(NULL), _next(NULL) {}
TR_HedgeTreeIterator(TR_HedgeTree<T> &tree) {reset(tree);}
TR_HedgeTree<T> *getBase() {return _tree;}
void reset(TR_HedgeTree<T> &tree) {_tree = &tree; _next = getLeftmost(tree.getRoot());}
T *getFirst()
{
T *node = getLeftmost(_tree->getRoot());
_next = getNextHigher(node);
return node;
}
T *getNext()
{
if (!_next)
return NULL;
T *node = _next;
_next = getNextHigher(node);
return node;
}
private:
T *getLeftmost(T *root)
{
if (root)
while (root->left())
root = root->left();
return root;
}
T *getNextHigher(T *node)
{
if (!node)
return NULL;
if (node->right())
return getLeftmost(node->right());
int32_t key = node->getKey();
for (node = node->getParent(); node && node->getKey() <= key; node = node->getParent())
{}
return node;
}
TR_HedgeTree<T> *_tree;
T *_next;
};
template <class T> class TR_HedgeTreeHandler
{
public:
TR_ALLOC(TR_Memory::HedgeTree)
T *find(int32_t key, TR_HedgeTree<T> &tree)
{
return tree.find(key);
}
T *findOrCreate(int32_t key, TR_HedgeTree<T> &tree)
{
int32_t rebalance;
T *result;
if (debug("traceHedge"))
diagnostic("\nCall findOrCreate with key %d\n", key);
if (tree.getRoot())
{
_nodeToBeInserted = NULL;
_treeChanged = false;
result = findOrCreate(key, tree.rootReference(), rebalance);
}
else
{
result = allocate(key);
tree.setRoot(result);
_treeChanged = true;
}
if (_treeChanged && debug("traceHedge"))
{
diagnostic("Tree after insertion of key %d:\n", key);
tree.print(comp()->getOutFile());
tree.verify();
}
return result;
}
T *insert(T *newNode, TR_HedgeTree<T> &tree)
{
int32_t rebalance;
T *result;
newNode->setLeft(NULL);
newNode->setRight(NULL);
newNode->setLeftSibling(false);
newNode->setRightSibling(false);
if (debug("traceHedge"))
diagnostic("\nCall insert with key %d\n", newNode->getKey());
if (tree.getRoot())
{
_nodeToBeInserted = newNode;
_treeChanged = false;
result = findOrCreate(newNode->getKey(), tree.rootReference(), rebalance);
}
else
{
result = newNode;
tree.setRoot(newNode);
_treeChanged = true;
}
if (_treeChanged && debug("traceHedge"))
{
diagnostic("Tree after insertion of key %d:\n", newNode->getKey());
tree.print(comp()->getOutFile());
tree.verify();
}
return result;
}
T *remove(int32_t key, TR_HedgeTree<T> &tree)
{
int32_t rebalance;
_treeChanged = false;
if (debug("traceHedge"))
diagnostic("\nCall remove with key %d\n", key);
T *result = remove(key, tree.rootReference(), rebalance);
if (_treeChanged && debug("traceHedge"))
{
diagnostic("Tree after removal of key %d:\n", key);
tree.print(comp()->getOutFile());
tree.verify();
}
return result;
}
T *copyAll(TR_HedgeTree<T> &tree)
{
return copySubtree(tree.getRoot());
}
T *copySubtree(T *node)
{
if (!node)
return NULL;
T *newNode = copy(node);
newNode->setLeft(copySubtree(node->left()));
newNode->setRight(copySubtree(node->right()));
newNode->setLeftSibling(node->isLeftSibling());
newNode->setRightSibling(node->isRightSibling());
return newNode;
}
void empty(TR_HedgeTree<T> &tree)
{
emptySubtree(tree.rootReference());
}
T *getRoot(TR_HedgeTree<T> &tree) {return tree.getRoot();}
void setRoot(TR_HedgeTree<T> &tree, T *node) {tree.setRoot(node);}
virtual TR::Compilation *comp() = 0;
protected:
virtual T *allocate(int32_t key) = 0;
virtual void free(T *node) = 0;
virtual T *copy(T *node) = 0;
private:
inline T *findOrCreate(int32_t key, T *&node, int32_t &rebalance);
inline T *remove(int32_t key, T *&node, int32_t &rebalance);
inline void swapRightmost(T *&node, T *&swap);
void emptySubtree(T *&node)
{
if (node)
{
emptySubtree(node->leftReference());
emptySubtree(node->rightReference());
free(node);
node = NULL;
}
}
T *_nodeToBeInserted;
bool _treeChanged;
};
template <class T> T *TR_HedgeTreeHandler<T>::findOrCreate(int32_t key, T *&node, int32_t &rebalance)
{
// Search the tree for the node with the given key. Insertion may cause the
// slot containing the node pointer to be updated.
// "rebalance" is a returned value with the following meaning:
// rebalance = 0: no change in structure of this subtree
// rebalance = 1: this node has acquired a sibling
// rebalance = 2: this subtree has increased in height
//
// If the search failed, create a new node and insert it
//
T *result;
T *p1, *p2;
if (key < node->getKey())
{
// Search the left subtree. Rebalance if necessary
//
if (!node->left())
{
if (_nodeToBeInserted)
result = _nodeToBeInserted;
else
result= allocate(key);
node->setLeft(result);
_treeChanged = true;
rebalance = 2;
}
else
result = findOrCreate(key,node->leftReference(), rebalance);
if (rebalance)
{
if (node->isLeftSibling())
{
// If the subtree acquired a sibling this level now has 3 nodes in
// the search path, so we must split the level.
// Thus the height of this subtree will always have increased.
//
rebalance = 2;
p1 = node->left();
node->setLeftSibling(false);
if (p1->isLeftSibling())
{
// Left subtree is lifted to a new level above this node
//
node->setLeft(p1->right());
p1->setLeftSibling(false);
p1->setParent(node->getParent());
p1->setRight(node);
node = p1;
}
else if (p1->isRightSibling())
{
// Left subtree's right subtree is lifted to a new level above
// this node
//
p2 = p1->right();
p1->setRight(p2->left());
p1->setRightSibling(false);
node->setLeft(p2->right());
node->setLeftSibling(false);
p2->setLeft(p1);
p2->setParent(node->getParent());
p2->setRight(node);
node = p2;
}
}
else
{
// Left subtree is a child. If its height has increased, raise it
// to become a sibling. Otherwise rebalancing is complete.
//
if (--rebalance)
node->setLeftSibling(true);
}
}
}
else if (key > node->getKey())
{
// Search the right subtree. Rebalance if necessary
//
if (!node->right())
{
if (_nodeToBeInserted)
result = _nodeToBeInserted;
else
result= allocate(key);
node->setRight(result);
_treeChanged = true;
rebalance = 2;
}
else
result = findOrCreate(key, node->rightReference(), rebalance);
if (rebalance)
{
if (node->isRightSibling())
{
// If the subtree acquired a sibling this level now has 3 nodes in
// the search path, so we must split the level.
// Thus the height of this subtree will always have increased.
//
rebalance = 2;
p1 = node->right();
node->setRightSibling(false);
if (p1->isRightSibling())
{
// Right subtree is lifted to a new level above this node
//
node->setRight(p1->left());
p1->setRightSibling(false);
p1->setParent(node->getParent());
p1->setLeft(node);
node = p1;
}
else if (p1->isLeftSibling())
{
// Right subtree's left subtree is lifted to a new level above
// this node
//
p2 = p1->left();
p1->setLeft(p2->right());
p1->setLeftSibling(false);
node->setRight(p2->left());
node->setRightSibling(false);
p2->setRight(p1);
p2->setParent(node->getParent());
p2->setLeft(node);
node = p2;
}
}
else
{
// Right subtree is a child. If its height has increased, raise it
// to become a sibling. Otherwise rebalancing is complete.
//
if (--rebalance)
node->setRightSibling(true);
}
}
}
else
{
rebalance = 0;
result = node;
}
return result;
}
template <class T> T *TR_HedgeTreeHandler<T>::remove(int32_t key, T *&node, int32_t &rebalance)
{
// Find the node with the given key and delete it from the tree
// Deletion may cause the slot containing the node pointer to be updated.
// "rebalance" is a returned value with the following meaning:
// rebalance = 0: no change in structure of this subtree
// rebalance = 1: this subtree has decreased in height
//
if (node == NULL)
{
// Node not found
//
rebalance = 0;
return NULL;
}
T *result;
T *p1, *p2, *p3;
bool takeLeftBranch = false;
if (key == node->getKey())
{
// This is the node to be deleted. If it is a leaf node just remove it
// from the tree. Otherwise swap it with the rightmost leaf node of the
// left subtree and continue down the left subtree. This way we end up
// always deleting a leaf node.
//
result = node;
_treeChanged = true;
if (node->right() == NULL)
{
rebalance = node->isLeftSibling() ? 0 : 1;
if (node->left())
node->left()->setParent(node->getParent());
node = node->left();
return result;
}
if (node->left() == NULL)
{
rebalance = node->isRightSibling() ? 0 : 1;
node->right()->setParent(node->getParent());
node = node->right();
return result;
}
swapRightmost(node->leftReference(), node);
takeLeftBranch = true;
}
if (takeLeftBranch || key < node->getKey())
{
// Look in the left subtree
//
result = remove(key, node->leftReference(), rebalance);
if (rebalance)
{
// The height of the subtree has changed. We must rebalance this node.
// In the diagrams that follow, B is this node and A is the subtree
// that has become shorter.
//
if (node->isLeftSibling())
{
// Left subtree is a sibling. Make it a child to bring the height
// back up to its original value.
//
// | |
// | |
// A--B ==> B
// \ / \
// C A C
//
node->setLeftSibling(false);
rebalance = 0;
}
else
{
p1 = node->right();
p2 = p1->left();
if ((node->isRightSibling() && p2->isRightSibling()) ||
(!node->isRightSibling() && p1->isLeftSibling()))
{
// Right subtree's left subtree is lifted to a new level above
// this node. The original height is then restored.
//
// | |
// | |
// B----C ==> D----C
// / / \ / / \
// A D-G E B G E
// / / \
// F A F
//
//
// | |
// | |
// B----C ==> D----C
// / / \ / / \
// A F-D-G E B-F G E
// /
// A
//
//
// | |
// | |
// B ==> D
// / \ / \
// A D-C-E B C--E
// / \ /\ /
// F G A F G
//
//
// | |
// | |
// B ==> D
// / \ / \
// A D-C B C
// / \ \ / \ /\
// F G E A F G E
//
//
p1->setLeft(p2->right());
p1->setLeftSibling(false);
p2->setRight(p1);
node->setRight(p2->left());
node->setRightSibling(p2->isLeftSibling());
p2->setParent(node->getParent());
p2->setLeft(node);
p2->setLeftSibling(false);
node = p2;
rebalance = 0;
}
else if (node->isRightSibling() && p2->isLeftSibling())
{
// Right subtree's left subtree's left subtree is lifted to a
// new level above this node. The original height is then restored.
//
// | |
// | |
// B-----C ==> F-----C
// / / \ / / \
// A F--D E B D E
// / \ \ / \ / \
// H I G A H I G
//
p3 = p2->left();
p2->setLeft(p3->right());
p2->setLeftSibling(false);
node->setRight(p3->left());
node->setRightSibling(false);
p3->setRight(p1);
p3->setRightSibling(true);
p3->setParent(node->getParent());
p3->setLeft(node);
node = p3;
rebalance = 0;
}
else if (node->isRightSibling())
{
// Right subtree is lifted to a new level above this node. The
// original height is then restored.
//
// | |
// | |
// B-----C ==> C
// / / \ / \
// A D E B--D E
// / \ / \ \
// F G A F G
//
node->setRight(p2->left());
node->setRightSibling(false);
p1->setParent(node->getParent());
p2->setLeft(node);
p2->setLeftSibling(true);
node = p1;
rebalance = 0;
}
else if (p1->isRightSibling())
{
// Right subtree is lifted to a new level above this node. The
// original height is not restored.
//
// | |
// | |
// B ==> B--C--E
// / \ / \
// A C--E A D
// /
// D
//
node->setRight(p1->left());
p1->setParent(node->getParent());
p1->setLeft(node);
p1->setLeftSibling(true);
node = p1;
}
else
{
// Raise the right subtree to be a sibling. The resulting tree
// is still shorter so the parent will have to rebalance.
// If this is a sibling of the parent, the parent will make it a
// child to restore the height, so no double-sibling chain will
// result.
//
// | |
// | |
// B ==> B--C
// / \ / / \
// A C A D E
// / \
// D E
//
node->setRightSibling(true);
}
}
}
}
else
{
// Look in the right subtree
//
result = remove(key, node->rightReference(), rebalance);
if (rebalance)
{
// The height of the subtree has changed. We must rebalance this node.
// In the diagrams that follow, B is this node and A is the subtree
// that has become shorter.
//
if (node->isRightSibling())
{
// Right subtree is a sibling. Make it a child to bring the height
// back up to its original value.
//
// | |
// | |
// B--A ==> B
// / / \
// C C A
//
node->setRightSibling(false);
rebalance = 0;
}
else
{
p1 = node->left();
p2 = p1->right();
if ((node->isLeftSibling() && p2->isLeftSibling()) ||
(!node->isLeftSibling() && p1->isRightSibling()))
{
// Left subtree's right subtree is lifted to a new level above
// this node. The original height is then restored.
//
// | |
// | |
// C----B ==> C----D
// / \ \ / \ \
// E G-D A E G B
// \ / \
// F F A
//
//
// | |
// | |
// C----B ==> C----D
// / \ \ / \ \
// E G-D-F A E G F-B
// \
// A
//
//
// | |
// | |
// B ==> D
// / \ / \
// E-C-D A E--C B
// / \ \ / \
// G F G F A
//
//
// | |
// | |
// B ==> D
// / \ / \
// C-D A C B
// / / \ /\ / \
// E G F E G F A
//
//
p1->setRight(p2->left());
p1->setRightSibling(false);
p2->setLeft(p1);
node->setLeft(p2->right());
node->setLeftSibling(p2->isRightSibling());
p2->setParent(node->getParent());
p2->setRight(node);
p2->setRightSibling(false);
node = p2;
rebalance = 0;
}
else if (node->isLeftSibling() && p2->isRightSibling())
{
// Left subtree's right subtree's right subtree is lifted to a
// new level above this node. The original height is then restored.
//
// | |
// | |
// C-----B ==> C-----F
// / \ \ / \ \
// E D--F A E D B
// / / \ / \ / \
// G I H G I H A
//
p3 = p2->right();
p2->setRight(p3->left());
p2->setRightSibling(false);
node->setLeft(p3->right());
node->setLeftSibling(false);
p3->setLeft(p1);
p3->setLeftSibling(true);
p3->setParent(node->getParent());
p3->setRight(node);
node = p3;
rebalance = 0;
}
else if (node->isLeftSibling())
{
// Left subtree is lifted to a new level above this node. The
// original height is then restored.
//
// | |
// | |
// C-----B ==> C
// / \ \ / \
// E D A E D--B
// / \ / / \
// G F G F A
//
node->setLeft(p2->right());
node->setLeftSibling(false);
p1->setParent(node->getParent());
p2->setRight(node);
p2->setRightSibling(true);
node = p1;
rebalance = 0;
}
else if (p1->isLeftSibling())
{
// Left subtree is lifted to a new level above this node. The
// original height is not restored.
//
// | |
// | |
// B ==> E--C--B
// / \ / \
// E--C A D A
// \
// D
//
node->setLeft(p1->right());
p1->setParent(node->getParent());
p1->setRight(node);
p1->setRightSibling(true);
node = p1;
}
else
{
// Raise the left subtree to be a sibling. The resulting tree
// is still shorter so the parent will have to rebalance.
// If this is a sibling of the parent, the parent will make it a
// child to restore the height, so no double-sibling chain will
// result.
//
// | |
// | |
// B ==> C--B
// / \ / \ \
// C A E D A
// / \
// E D
//
node->setLeftSibling(true);
}
}
}
}
return result;
}
template <class T> void TR_HedgeTreeHandler<T>::swapRightmost(T *&node, T *&swap)
{
// Find the rightmost node in this subtree and swap it with the given node
//
if (node->right())
{
swapRightmost(node->rightReference(), swap);
}
else
{
// This is it
//
bool b;
b = node->isLeftSibling(); node->setLeftSibling(swap->isLeftSibling()); swap->setLeftSibling(b);