-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.hpp
1167 lines (990 loc) · 30.4 KB
/
tree.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
#ifndef BINTREE_TREE_HPP
#define BINTREE_TREE_HPP
#include <functional>
#include <iostream>
#include <list>
#include <deque>
#include <cstdint>
/*
using uint_t = uint32_t;
using int_t = int32_t;
// **************************************
// ************* treenode_t *************
// **************************************
template <typename T>
class treenode_t {
protected:
void *_left = nullptr;
void *_right = nullptr;
void *_parent = nullptr;
public:
T value;
constexpr inline treenode_t<T>*& left() noexcept;
constexpr inline treenode_t<T>*& right() noexcept;
constexpr inline treenode_t<T>*& parent() noexcept;
public:
treenode_t(T value);
treenode_t(treenode_t &&node);
void hookleft(treenode_t *newparent);
void hookright(treenode_t *newparent);
void addleft(treenode_t *child);
void addleft(T val);
void addright(treenode_t *child);
void addright(T val);
uint_t get_height();
uint_t get_degree();
int get_balance_factor();
};
typedef enum {
NODE_ROOT = 0,
NODE_LEFT,
NODE_RIGHT
} left_or_right_e;
// *************************************
// ************* bintree_T *************
// *************************************
template <typename T, typename node_T = treenode_t<T> >
class bintree_t {
public:
using trav_action_t = std::function<void (node_T *, uint_t, left_or_right_e)>;
using node_type = node_T;
using value_type = T;
public:
node_T *root = nullptr;
public:
bintree_t() = default;
~bintree_t();
node_T *get_sibling(node_T *node);
void trav_bfs(trav_action_t action);
void trav_pre(trav_action_t action);
void trav_in(trav_action_t action);
void trav_post(trav_action_t action);
void print_tree();
void rotate_right(node_T *node);
void rotate_left(node_T *node);
#ifdef DEBUG
node_T * operator[](T val);
#endif
};
// *****************************************
// ************* search_tree_t *************
// *****************************************
template <typename T, typename node_T = treenode_t<T> >
class search_tree_t : public bintree_t<T, node_T> {
private:
using base_type = bintree_t<T, node_T>;
public:
enum { EQ_REPLACE = 0, EQ_KEEP } replace_policy = EQ_REPLACE;
using comparer_type = std::function<bool (const T &a, const T &b)>;
using equaler_type = std::function<bool (const T &a, const T &b)>;
protected:
comparer_type comparer = [] (const T &a, const T &b) -> bool { return a < b; };
equaler_type equaler = [] (const T &a, const T &b) -> bool { return a == b; };
public:
search_tree_t() = default;
search_tree_t(decltype(comparer) cmp);
search_tree_t(decltype(comparer) cmp, decltype(equaler) eql);
inline void set_replace_policy(decltype(replace_policy) rp);
node_T *search_value(T val);
node_T *push(T val);
void remove(T val);
// 返回删除操作后被删除节点原位置的新节点
node_T *erase(node_T *node);
};
// **************************************
// ************* AVL_tree_t *************
// **************************************
template <typename T, typename node_T = treenode_t<T> >
class AVL_tree_t : public search_tree_t<T, node_T> {
private:
using base_type = search_tree_t<T, node_T>;
public:
AVL_tree_t() = default;
AVL_tree_t(typename base_type::comparer_type cmp);
AVL_tree_t(typename base_type::comparer_type cmp,
typename base_type::equaler_type eql);
node_T *rotate(node_T *node);
node_T *push(T val);
void erase(node_T *node);
void remove(T val);
};
// *************************************
// ************* RB_tree_t *************
// *************************************
template <typename T>
class RB_treenode_t : public treenode_t<T> {
public:
RB_treenode_t(T val);
constexpr inline RB_treenode_t<T>*& left() noexcept;
constexpr inline RB_treenode_t<T>*& right() noexcept;
constexpr inline RB_treenode_t<T>*& parent() noexcept;
enum { COLOR_BLACK = 0, COLOR_RED } color;
};
template <typename T, typename node_T = RB_treenode_t<T> >
class RB_tree_t : public search_tree_t<T, node_T> {
private:
using base_type = search_tree_t<T, node_T>;
void maintain(node_T *node);
void maintain_delete(node_T *node);
public:
RB_tree_t() = default;
RB_tree_t(typename base_type::comparer_type cmp);
RB_tree_t(typename base_type::comparer_type cmp,
typename base_type::equaler_type eql);
node_T *rotate(node_T *node);
node_T *push(T val);
void erase(node_T *node);
void remove(T val);
void print_tree();
};
*/
using uint_t = uint32_t;
using int_t = int32_t;
// **************************************
// ************* treenode_t *************
// **************************************
template <typename T>
class treenode_t {
protected:
void *_left = nullptr;
void *_right = nullptr;
void *_parent = nullptr;
public:
T value;
constexpr inline treenode_t<T>*& left() noexcept {
return *static_cast<treenode_t<T> **>(static_cast<void *>(&this->_left));
}
constexpr inline treenode_t<T>*& right() noexcept {
return *static_cast<treenode_t<T> **>(static_cast<void *>(&this->_right));
}
constexpr inline treenode_t<T>*& parent() noexcept {
return *static_cast<treenode_t<T> **>(static_cast<void *>(&this->_parent));
}
public:
treenode_t(T value) : value(value) {}
treenode_t(treenode_t &&node) {
this->_left = node._left;
this->_right = node._right;
this->_parent = node._parent;
this->value = node.value;
node._left = nullptr;
node._right = nullptr;
node._parent = nullptr;
}
void hookleft(treenode_t *newparent) {
newparent->left() = this;
this->parent() = newparent;
}
void hookright(treenode_t *newparent) {
newparent->right() = this;
this->parent() = newparent;
}
void addleft(treenode_t *child) {
this->left() = child;
child->parent() = this;
}
void addleft(T val) {
this->addleft(new treenode_t(val));
}
void addright(treenode_t *child) {
this->right() = child;
child->parent() = this;
}
void addright(T val) {
this->addright(new treenode_t(val));
}
uint_t get_height() {
uint_t height = 0;
std::function<void (treenode_t*, uint_t)> traversal =
[&] (treenode_t *node, uint_t level) -> void {
if (level > height) height = level;
if (node->left())
traversal(node->left(), level + 1);
if (node->right())
traversal(node->right(), level + 1);
};
traversal(this, 0);
return height;
}
uint_t get_degree() {
if (this->left() && this->right())
return 2;
else if (this->left() || this->right())
return 1;
else return 0;
}
int_t get_balance_factor() {
int_t left_bfactor = 0;
int_t right_bfactor = 0;
if (this->left())
left_bfactor = this->left()->get_height() + 1;
if (this->right())
right_bfactor = this->right()->get_height() + 1;
return left_bfactor - right_bfactor;
}
};
typedef enum {
NODE_ROOT = 0,
NODE_LEFT,
NODE_RIGHT
} left_or_right_e;
// *************************************
// ************* bintree_T *************
// *************************************
template <typename T, typename node_T = treenode_t<T> >
class bintree_t {
public:
using trav_action_t = std::function<void (node_T *, uint_t, left_or_right_e)>;
using node_type = node_T;
using value_type = T;
public:
node_T *root = nullptr;
public:
bintree_t() = default;
~bintree_t() {
std::list<node_T *> todel;
this->trav_bfs([&](node_T *node, uint_t, left_or_right_e) {
todel.push_back(node);
});
for (const auto &each : todel)
delete each;
}
node_T *get_sibling(node_T *node) {
if (node->parent()) {
if (node->parent()->left() == node)
return node->parent()->right();
else
return node->parent()->left();
} else return nullptr;
}
/* 层序遍历 */
void trav_bfs(trav_action_t action) {
if (!this->root) return;
using std::deque;
deque<node_T *> dq;
dq.push_back(this->root);
while (!dq.empty()) {
node_T *node = dq.front();
dq.pop_front();
action(node, 0, NODE_ROOT);
if (node->left())
dq.push_back(node->left());
if (node->right())
dq.push_back(node->right());
}
}
/* 前序遍历 */
void trav_pre(trav_action_t action) {
if (!this->root) return;
std::function<void (node_T *, trav_action_t, uint_t, left_or_right_e)> recur_trav =
[&] (node_T *node, trav_action_t action, uint_t level, left_or_right_e lorr) -> void {
action(node, level, lorr);
if (node->left())
recur_trav(node->left(), action, level + 1, NODE_LEFT);
if (node->right())
recur_trav(node->right(), action, level + 1, NODE_RIGHT);
};
recur_trav(this->root, action, 0, NODE_ROOT);
}
/* 中序遍历 */
void trav_in(trav_action_t action) {
if (!this->root) return;
std::function<void (node_T *, trav_action_t, uint_t, left_or_right_e)> recur_trav =
[&] (node_T *node, trav_action_t action, uint_t level, left_or_right_e lorr) -> void {
if (node->left())
recur_trav(node->left(), action, level + 1, NODE_LEFT);
action(node, level, lorr);
if (node->right())
recur_trav(node->right(), action, level + 1, NODE_RIGHT);
};
recur_trav(this->root, action, 0, NODE_ROOT);
}
/* 后序遍历 */
void trav_post(trav_action_t action) {
if (!this->root) return;
std::function<void (node_T *, trav_action_t, uint_t, left_or_right_e)> recur_trav =
[&] (node_T *node, trav_action_t action, uint_t level, left_or_right_e lorr) -> void {
if (node->left())
recur_trav(node->left(), action, level + 1, NODE_LEFT);
if (node->right())
recur_trav(node->right(), action, level + 1, NODE_RIGHT);
action(node, level, lorr);
};
recur_trav(this->root, action, 0, NODE_ROOT);
}
/* 打印树 */
void print_tree() {
this->trav_pre(
[] (node_T *node, uint_t level, left_or_right_e lorr) {
for (uint_t i = 0; i < level; i++)
std::cout << "======|";
std::cout << "\b";
switch (lorr) {
case NODE_ROOT:
std::cout << "=========== print_tree ==========\nRT ";
break;
case NODE_LEFT:
std::cout << "[L] ";
break;
case NODE_RIGHT:
std::cout << "[R] ";
break;
default:;
}
std::cout << node->value;
if (node->parent())
std::cout << " |P " << node->parent()->value << std::endl;
else
std::cout << " |P NUL" << std::endl;
}
);
std::cout << "Print OK." << std::endl;
}
/*
node:
左节点 == null
(不可右旋)
返回
左节点 != null
(可以右旋)
父节点 == null
(node是根节点)
根节点 = 左子
tmp = 左子->右子
左子->右子 = node
node->左子 = tmp
父节点 != null
(node不是根节点)
父->(左/右)子 = 左子
tmp = 左子->右子
左子->右子 = node
node->左子 = tmp
| node | L | L->L | L->R | R | R->L | R->R | P | YESorNO
0 * * * * * * * 0
1 0 * * * * * * 0
1 1
*/
void rotate_right(node_T *node) {
if (!node) {
std::cout << "不可对nullptr右旋!" << std::endl;
return;
}
if (!node->left()) {
// 不可右旋
std::cout << "不可右旋!!!" << std::endl;
return;
} else {
// 可以右旋
if (!node->parent()) {
// node 是根节点
this->root = node->left();
node->left()->parent() = nullptr;
node_T *tmp = node->left()->right();
node->left()->right() = node;
node->parent() = node->left();
node->left() = tmp;
if (tmp)
tmp->parent() = node;
} else {
// node 不是根节点
if (node->parent()->left() == node) {
node->parent()->left() = node->left();
node->left()->parent() = node->parent();
} else {
node->parent()->right() = node->left();
node->left()->parent() = node->parent();
}
node_T *tmp = node->left()->right();
node->left()->right() = node;
node->parent() = node->left();
node->left() = tmp;
if (tmp)
tmp->parent() = node;
}
}
}
/*
node:
右节点 == null
(不可左旋)
右节点 != null
(可以左旋)
父节点 == null
(node是根节点)
根节点 = 右子
tmp = 右子->左子
右子->左子 = node
右子 = tmp
父节点 != null
(node不是根节点)
父->(左/右)子 = 右子
tmp = 右子->左子
右子->左子 = node
右子 = tmp
| node | L | L->L | L->R | R | R->L | R->R | P | YESorNO
0 * * * * * * * 0
1 * * * 0 * * * 0
*/
void rotate_left(node_T *node) {
if (!node) {
std::cout << "不可对nullptr左旋!" << std::endl;
return;
}
if (!node->right()) {
// 不可左旋
std::cout << "不可左旋!!!" << std::endl;
return;
} else {
// 可以左旋
if (!node->parent()) {
// node是根节点
this->root = node->right();
node->right()->parent() = nullptr;
node_T *tmp = node->right()->left();
node->right()->left() = node;
node->parent() = node->right();
node->right() = tmp;
if (tmp)
tmp->parent() = node;
} else {
// node不是根节点
if (node->parent()->left() == node) {
node->parent()->left() = node->right();
node->right()->parent() = node->parent();
} else {
node->parent()->right() = node->right();
node->right()->parent() = node->parent();
}
node_T *tmp = node->right()->left();
node->right()->left() = node;
node->parent() = node->right();
node->right() = tmp;
if (tmp)
tmp->parent() = node;
}
}
}
#ifdef DEBUG
node_T * operator[](T val) {
node_T *target = nullptr;
this->trav_bfs([&](node_T *node, uint_t, left_or_right_e) -> void {
if (node->value == val) {
target = node;
return;
}
});
return target;
}
#endif
};
// *****************************************
// ************* search_tree_t *************
// *****************************************
template <typename T, typename node_T = treenode_t<T> >
class search_tree_t : public bintree_t<T, node_T> {
private:
using base_type = bintree_t<T, node_T>;
public:
enum { EQ_REPLACE = 0, EQ_KEEP } replace_policy = EQ_REPLACE;
using comparer_type = std::function<bool (const T &a, const T &b)>;
using equaler_type = std::function<bool (const T &a, const T &b)>;
protected:
comparer_type comparer = [] (const T &a, const T &b) -> bool { return a < b; };
equaler_type equaler = [] (const T &a, const T &b) -> bool { return a == b; };
public:
search_tree_t() = default;
search_tree_t(decltype(comparer) cmp) : comparer(cmp) {}
search_tree_t(decltype(comparer) cmp, decltype(equaler) eql)
: comparer(cmp), equaler(eql) {}
inline void set_replace_policy(decltype(replace_policy) rp) {
this->replace_policy = rp;
}
node_T *search_value(T val) {
node_T *cur = this->root;
for (;;) {
if (cur == nullptr)
return cur;
if (this->equaler(cur->value, val))
return cur;
else {
if (this->comparer(cur->value, val))
cur = cur->right();
else
cur = cur->left();
}
}
}
node_T* push(T val) {
if (!this->root) {
this->root = new node_T(val);
return this->root;
}
node_T *cur = this->root;
for (;;) {
if (this->equaler(cur->value, val)) {
switch (this->replace_policy) {
case EQ_KEEP:
return nullptr;
case EQ_REPLACE:
cur->value = std::forward<T>(val);
return cur;
default: return nullptr; // Shouldn't be here
}
}
if (this->comparer(cur->value, val)) {
if (!cur->right()) {
cur->addright(val);
return cur->right();
}
cur = cur->right();
} else {
if (!cur->left()) {
cur->addleft(val);
return cur->left();
}
cur = cur->left();
}
}
}
void remove(T val) {
node_T *node = this->search_value(val);
if (!node) return;
if (node)
this->erase(node);
}
node_T *erase(node_T *node) {
// 先确定此节点的度
uint_t degree = node->get_degree();
if (degree == 0) {
// 直接删
if (!node->parent()) // 根节点
this->root = nullptr;
else if (node->parent()->left() == node)
node->parent()->left() = nullptr;
else
node->parent()->right() = nullptr;
delete node;
return nullptr;
} else if (degree == 1) {
// 让子节点上来
if (node->left()) {
node->value = std::move(node->left()->value);
delete node->left();
node->left() = nullptr;
return node;
} else {
node->value = std::move(node->right()->value);
delete node->right();
node->right() = nullptr;
return node;
}
} else {
/*
以左子树的最大节点(left_max)替换此被删除的节点
ml存在:
mp继承ml
mr存在:
不可能,因为m不会存在r
*/
node_T *left_max = node->left();
for (;;) {
if (left_max->right())
left_max = left_max->right();
else break;
}
if (left_max->parent()->left() == left_max) {
// 出现这种情况,只可能是left_max是node->left()
// left_max没有right
node->left() = left_max->left();
if (left_max->left())
left_max->left()->parent() = node;
} else {
// 是父节点右子
// left_max没有right
left_max->parent()->right() = left_max->left();
if (left_max->left())
left_max->left()->parent() = left_max->parent();
}
node->value = std::move(left_max->value);
delete left_max;
return node;
}
}
};
// **************************************
// ************* AVL_tree_t *************
// **************************************
template <typename T, typename node_T = treenode_t<T> >
class AVL_tree_t : public search_tree_t<T, node_T> {
private:
using base_type = search_tree_t<T, node_T>;
public:
AVL_tree_t() = default;
AVL_tree_t(typename base_type::comparer_type cmp)
: base_type(cmp) {}
AVL_tree_t(typename base_type::comparer_type cmp,
typename base_type::equaler_type eql)
: base_type(cmp, eql) {}
node_T *rotate(node_T *node) {
int_t balance_factor = node->get_balance_factor();
int_t child_balance_factor;
if (balance_factor > 1) {
// 左偏树
child_balance_factor = node->left()->get_balance_factor();
if (child_balance_factor < 0) {
// 先左旋,后右旋
this->rotate_left(node->left());
this->rotate_right(node);
} else {
// 直接右旋
this->rotate_right(node);
}
return node->parent();
} else if (balance_factor < -1) {
// 右偏树
child_balance_factor = node->right()->get_balance_factor();
if (child_balance_factor > 0) {
// 先右旋,后左旋
this->rotate_right(node->right());
this->rotate_left(node);
} else {
// 直接左旋
this->rotate_left(node);
}
return node->parent();
} else {
return node;
}
}
node_T *push(T val) {
if (this->root == nullptr) {
this->root = new node_T(val);
return this->root;
}
node_T *newnode = dynamic_cast<base_type *>(this)->push(val);
if (!newnode) return nullptr;
// 自底向上旋转
node_T *cur = newnode;
for (;;) {
if (cur == nullptr) break;
this->rotate(cur);
cur = cur->parent();
}
return newnode;
}
void erase(node_T *node) {
if (!node) return;
node_T *delnode = dynamic_cast<base_type *>(this)->erase(node);
node_T *cur = delnode;
for (;;) {
if (cur == nullptr) break;
this->rotate(cur);
cur = cur->parent();
}
}
void remove(T val) {
this->erase(this->search_value(val));
}
};
// *************************************
// ************* RB_tree_t *************
// *************************************
template <typename T>
class RB_treenode_t : public treenode_t<T> {
public:
RB_treenode_t(T val) : treenode_t<T>(val) {}
constexpr inline RB_treenode_t<T>*& left() noexcept {
return *static_cast<RB_treenode_t<T> **> (static_cast<void *>(&this->_left));
}
constexpr inline RB_treenode_t<T>*& right() noexcept {
return *static_cast<RB_treenode_t<T> **> (static_cast<void *>(&this->_right));
}
constexpr inline RB_treenode_t<T>*& parent() noexcept {
return *static_cast<RB_treenode_t<T> **> (static_cast<void *>(&this->_parent));
}
enum { COLOR_BLACK = 0, COLOR_RED } color;
};
template <typename T, typename node_T = RB_treenode_t<T> >
class RB_tree_t : public search_tree_t<T, node_T> {
private:
using base_type = search_tree_t<T, node_T>;
void maintain(node_T *node) {
if (node == this->root) {
node->color = node_T::COLOR_BLACK;
} else if (node->parent()->color == node_T::COLOR_BLACK) {
node->color = node_T::COLOR_RED;
} else {// 父节点是红色,一定有黑色祖父节点
node_T *uncle = this->get_sibling(node->parent());
node_T *grandparent = node->parent()->parent();
bool is_uncle_black = false;
if (!uncle)
is_uncle_black = true;
else if (uncle->color == node_T::COLOR_BLACK)
is_uncle_black = true;
if (is_uncle_black) {
// 叔节点是黑色 NIL
// 我红,父黑,祖红,旋转祖
node->color = node_T::COLOR_RED;
node->parent()->color = node_T::COLOR_BLACK;
grandparent->color = node_T::COLOR_RED;
grandparent->get_height();
if (node == node->parent()->left()
&& node->parent() == grandparent->left()) {
this->rotate_right(grandparent);
} else if (node == node->parent()->right()
&& node->parent() == grandparent->left()) {
this->rotate_left(node->parent());
this->rotate_right(grandparent);
} else if (node == node->parent()->left()
&& node->parent() == grandparent->right()) {
this->rotate_right(node->parent());
this->rotate_left(grandparent);
/* } else if (node == node->parent()->right()
&& node->parent() == grandparent->right()) { */
} else {
this->rotate_left(grandparent);
}
} else {
// 叔节点是红色
node->color = node_T::COLOR_RED;
node->parent()->color = node_T::COLOR_BLACK;
uncle->color = node_T::COLOR_BLACK;
this->maintain(grandparent);
}
}
}
void maintain_delete(node_T *node) {
// TODO
}
public:
RB_tree_t() = default;
RB_tree_t(typename base_type::comparer_type cmp)
: base_type(cmp) {}
RB_tree_t(typename base_type::comparer_type cmp,
typename base_type::equaler_type eql)
: base_type(cmp, eql) {}
[[noreturn]]
node_T *rotate(node_T *node) {
}
node_T *push(T val) {
node_T *newnode = dynamic_cast<base_type *>(this)->push(val);
this->maintain(newnode);
return newnode;
}
node_T *erase(node_T *node) {
if (node == this->root && node->get_degree() == 0) {
this->root = nullptr;
delete node;
return nullptr;
}
node_T *sibling = nullptr;
// 1. node 红,无子
if (node->color == node_T::COLOR_RED
&& node->get_degree() == 0) {
// 直接摘除
if (node->parent()->left() == node)
node->parent()->left() = nullptr;
else
node->parent()->right() = nullptr;
delete node;
return nullptr;
}
// 2. node黑,兄弟无子
if (node->get_degree() == 0
&& node->color == node_T::COLOR_BLACK
&& (sibling = this->get_sibling(node))->get_degree() == 0) {
// 此时兄弟必黑,父必红
sibling->color = node_T::COLOR_RED;
node->parent()->color = node_T::COLOR_BLACK;
if (node->parent()->left() == node)
node->parent()->left() = nullptr;
else
node->parent()->right() = nullptr;
delete node;
return nullptr;
}
// 3. node黑,兄弟度1,兄弟与侄子同侧
if (node->get_degree() == 0
&& node->color == node_T::COLOR_BLACK
&& (sibling = this->get_sibling(node))->get_degree() == 1
&& (
(sibling == node->parent()->left() && sibling->left())
||
(sibling == node->parent()->right() && sibling->right())
)) {
node_T *nephew = sibling->left() ? sibling->left() : sibling->right();
sibling->color = node->parent()->color;
node->parent()->color = node_T::COLOR_BLACK;
nephew->color = node_T::COLOR_BLACK;
// 删除node
if (node->parent()->left() == node)
node->parent()->left() = nullptr;
else
node->parent()->right() = nullptr;
delete node;
node = nullptr;
node_T *oriparent = sibling->parent();
if (nephew == sibling->left())
this->rotate_right(sibling->parent());
else
this->rotate_left(sibling->parent());
return oriparent;
}
// 4. node黑,兄弟度1,兄弟与侄子不同侧
if (node->get_degree() == 0
&& node->color == node_T::COLOR_BLACK
&& (sibling = this->get_sibling(node))->get_degree() == 1
&& (
(sibling == node->parent()->left() && sibling->right())
||
(sibling == node->parent()->right() && sibling->left())
)) {
// 此时侄子必为红色
node_T *nephew = sibling->left() ? sibling->left() : sibling->right();
// 删除node
if (node->parent()->left() == node)
node->parent()->left() = nullptr;
else