-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrb_tree.h
1745 lines (1570 loc) · 44 KB
/
rb_tree.h
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 RE_TREE_H
#define RE_TREE_H
// 实现红黑树
// 太复杂了,原理看个大概,代码实现没太看懂
#include<initializer_list>
#include<cassert>
#include"functional.h"
#include"iterator.h"
#include"memory.h"
#include"type_traits.h"
#include"exceptdef.h"
namespace mystl
{
// re-tree的节点设计
typedef bool re_tree_color_type;
static constexpr re_tree_color_type rb_tree_red = false;
static constexpr re_tree_color_type rb_tree_black = true;
template<class T> struct rb_tree_node_base;
template<class T> struct rb_tree_node;
template<class T> struct rb_tree_iterator;
template<class T> struct rb_tree_const_iterator;
// rb tree value triats
template <class T,bool>
struct rb_tree_value_traits_imp
{
typedef T key_type;
typedef T mapped_type;
typedef T value_type;
template <class Ty>
static const value_type& get_value(const Ty& value)
{
return value;
}
};
template <class T>
struct rb_tree_value_traits_imp<T,true>
{
typedef typename std::remove_cv<typename T::first_type>::type key_type;
typedef typename T::sceond_type mapped_type;
typedef T value_type;
template <class Ty>
static const key_type& get_key(const Ty& value)
{
return value.first;
}
template <class Ty>
static const key_type& get_value(const Ty& value)
{
return value;
}
};
template <class T>
struct rb_tree_value_traits
{
static constexpr bool is_map = mystl::is_pair<T>::value;
typedef rb_tree_value_traits_imp<T, is_map> value_traits_type;
typedef typename value_traits_type::key_type key_type;
typedef typename value_traits_type::mapped_type mapped_type;
typedef typename value_traits_type::value_type value_type;
template <class Ty>
static const key_type& get_key(const Ty& value)
{
return value_traits_type::get_key(value);
}
template <class Ty>
static const value_type& get_value(const Ty& value)
{
return value_traits_type::get_value(value);
}
};
// rb tree node traits
template <class T>
struct rb_tree_node_traits
{
typedef rb_tree_color_type color_type;
typedef rb_tree_value_traits<T> value_traits;
typedef typename value_traits::key_type key_type;
typedef typename value_traits::mapped_type mapped_type;
typedef typename value_traits::value_type value_type;
typedef rb_tree_node_base<T>* base_ptr;
typedef rb_tree_node<T>* node_ptr;
};
// rb tree 的节点设计
template <class T>
struct rb_tree_node_base
{
typedef rb_tree_color_type color_type;
typedef rb_tree_node_base<T>* base_ptr;
typedef rb_tree_node<T>* node_ptr;
base_ptr parent; // 父节点
base_ptr left; // 左子节点
base_ptr right; // 右子节点
color_type color; // 节点颜色
base_ptr get_base_ptr()
{
return &*this;
}
node_ptr get_node_ptr()
{
return reinterpret_cast<node_ptr>(&*this);
}
node_ptr& get_node_ref()
{
return reinterpret_cast<node_ptr&>(*this);
}
};
template <class T>
struct rb_tree_node :public rb_tree_node_base<T>
{
typedef rb_tree_node_base<T>* base_ptr;
typedef rb_tree_node<T>* node_ptr;
T value; // 节点值
base_ptr get_base_ptr()
{
return static_cast<base_ptr>(&*this);
}
node_ptr get_node_ptr()
{
return &*this;
}
};
// rb tree traits
template <class T>
struct rb_tree_traits
{
typedef rb_tree_value_traits<T> value_traits;
typedef typename value_traits::key_type key_type;
typedef typename value_traits::mapped_type mapped_type;
typedef typename value_traits::value_type value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef const value_type* const_pointer;
typedef const value_type& const_reference;
typedef rb_tree_node_base<T> base_type;
typedef rb_tree_node<T> node_type;
typedef base_type* base_ptr;
typedef node_type* node_ptr;
};
// rb tree 的迭代器设计
template <class T>
struct rb_tree_iterator_base :public mystl::iterator<mystl::bidirectional_iterator_tag, T>
{
typedef typename rb_tree_traits<T>::base_ptr base_ptr;
base_ptr node; // 指向节点本身
rb_tree_iterator_base() :node(nullptr) {}
// 使迭代器前进
void inc()
{
if (node->right != nullptr)
{
node = rb_tree_min(node->right);
}
else
{ // 如果没有右子节点
auto y = node->parent;
while (y->right == node)
{
node = y;
y = y->parent;
}
if (node->right != y) // 应对“寻找根节点的下一节点,而根节点没有右子节点”的特殊情况
node = y;
}
}
// 使迭代器后退
void dec()
{
if (node->parent->parent == node && rb_tree_is_red(node))
{ // 如果 node 为 header
node = node->right; // 指向整棵树的 max 节点
}
else if (node->left != nullptr)
{
node = rb_tree_max(node->left);
}
else
{ // 非 header 节点,也无左子节点
auto y = node->parent;
while (node == y->left)
{
node = y;
y = y->parent;
}
node = y;
}
}
bool operator==(const rb_tree_iterator_base& rhs) { return node == rhs.node; }
bool operator!=(const rb_tree_iterator_base& rhs) { return node != rhs.node; }
};
template <class T>
struct rb_tree_iterator :public rb_tree_iterator_base<T>
{
typedef rb_tree_traits<T> tree_traits;
typedef typename tree_traits::value_type value_type;
typedef typename tree_traits::pointer pointer;
typedef typename tree_traits::reference reference;
typedef typename tree_traits::base_ptr base_ptr;
typedef typename tree_traits::node_ptr node_ptr;
typedef rb_tree_iterator<T> iterator;
typedef rb_tree_const_iterator<T> const_iterator;
typedef iterator self;
using rb_tree_iterator_base<T>::node;
// 构造函数
rb_tree_iterator() {}
rb_tree_iterator(base_ptr x) { node = x; }
rb_tree_iterator(node_ptr x) { node = x; }
rb_tree_iterator(const iterator& rhs) { node = rhs.node; }
rb_tree_iterator(const const_iterator& rhs) { node = rhs.node; }
// 重载操作符
reference operator*() const { return node->get_node_ptr()->value; }
pointer operator->() const { return &(operator*()); }
self& operator++()
{
this->inc();
return *this;
}
self operator++(int)
{
self tmp(*this);
this->inc();
return tmp;
}
self& operator--()
{
this->dec();
return *this;
}
self operator--(int)
{
self tmp(*this);
this->dec();
return tmp;
}
};
template <class T>
struct rb_tree_const_iterator :public rb_tree_iterator_base<T>
{
typedef rb_tree_traits<T> tree_traits;
typedef typename tree_traits::value_type value_type;
typedef typename tree_traits::const_pointer pointer;
typedef typename tree_traits::const_reference reference;
typedef typename tree_traits::base_ptr base_ptr;
typedef typename tree_traits::node_ptr node_ptr;
typedef rb_tree_iterator<T> iterator;
typedef rb_tree_const_iterator<T> const_iterator;
typedef const_iterator self;
using rb_tree_iterator_base<T>::node;
// 构造函数
rb_tree_const_iterator() {}
rb_tree_const_iterator(base_ptr x) { node = x; }
rb_tree_const_iterator(node_ptr x) { node = x; }
rb_tree_const_iterator(const iterator& rhs) { node = rhs.node; }
rb_tree_const_iterator(const const_iterator& rhs) { node = rhs.node; }
// 重载操作符
reference operator*() const { return node->get_node_ptr()->value; }
pointer operator->() const { return &(operator*()); }
self& operator++()
{
this->inc();
return *this;
}
self operator++(int)
{
self tmp(*this);
this->inc();
return tmp;
}
self& operator--()
{
this->dec();
return *this;
}
self operator--(int)
{
self tmp(*this);
this->dec();
return tmp;
}
};
// tree algorithm
template <class NodePtr>
NodePtr rb_tree_min(NodePtr x) noexcept
{
while (x->left != nullptr)
x = x->left;
return x;
}
template <class NodePtr>
NodePtr rb_tree_max(NodePtr x) noexcept
{
while (x->right != nullptr)
x = x->right;
return x;
}
template <class NodePtr>
bool rb_tree_is_lchild(NodePtr node) noexcept
{
return node == node->parent->left;
}
template <class NodePtr>
bool rb_tree_is_red(NodePtr node) noexcept
{
return node->color == rb_tree_red;
}
template <class NodePtr>
void rb_tree_set_black(NodePtr& node) noexcept
{
node->color = rb_tree_black;
}
template <class NodePtr>
void rb_tree_set_red(NodePtr& node) noexcept
{
node->color = rb_tree_red;
}
template <class NodePtr>
NodePtr rb_tree_next(NodePtr node) noexcept
{
if (node->right != nullptr)
return rb_tree_min(node->right);
while (!rb_tree_is_lchild(node))
node = node->parent;
return node->parent;
}
/*---------------------------------------*\
| p p |
| / \ / \ |
| x d rotate left y d |
| / \ ===========> / \ |
| a y x c |
| / \ / \ |
| b c a b |
\*---------------------------------------*/
// 左旋,参数一为左旋点,参数二为根节点
template <class NodePtr>
void rb_tree_rotate_left(NodePtr x, NodePtr& root) noexcept
{
auto y = x->right; // y 为 x 的右子节点
x->right = y->left;
if (y->left != nullptr)
y->left->parent = x;
y->parent = x->parent;
if (x == root)
{ // 如果 x 为根节点,让 y 顶替 x 成为根节点
root = y;
}
else if (rb_tree_is_lchild(x))
{ // 如果 x 是左子节点
x->parent->left = y;
}
else
{ // 如果 x 是右子节点
x->parent->right = y;
}
// 调整 x 与 y 的关系
y->left = x;
x->parent = y;
}
/*----------------------------------------*\
| p p |
| / \ / \ |
| d x rotate right d y |
| / \ ===========> / \ |
| y a b x |
| / \ / \ |
| b c c a |
\*----------------------------------------*/
// 右旋,参数一为右旋点,参数二为根节点
template <class NodePtr>
void rb_tree_rotate_right(NodePtr x, NodePtr& root) noexcept
{
auto y = x->left;
x->left = y->right;
if (y->right)
y->right->parent = x;
y->parent = x->parent;
if (x == root)
{ // 如果 x 为根节点,让 y 顶替 x 成为根节点
root = y;
}
else if (rb_tree_is_lchild(x))
{ // 如果 x 是右子节点
x->parent->left = y;
}
else
{ // 如果 x 是左子节点
x->parent->right = y;
}
// 调整 x 与 y 的关系
y->right = x;
x->parent = y;
}
// 插入节点后使 rb tree 重新平衡,参数一为新增节点,参数二为根节点
//
// case 1: 新增节点位于根节点,令新增节点为黑
// case 2: 新增节点的父节点为黑,没有破坏平衡,直接返回
// case 3: 父节点和叔叔节点都为红,令父节点和叔叔节点为黑,祖父节点为红,
// 然后令祖父节点为当前节点,继续处理
// case 4: 父节点为红,叔叔节点为 NIL 或黑色,父节点为左(右)孩子,当前节点为右(左)孩子,
// 让父节点成为当前节点,再以当前节点为支点左(右)旋
// case 5: 父节点为红,叔叔节点为 NIL 或黑色,父节点为左(右)孩子,当前节点为左(右)孩子,
// 让父节点变为黑色,祖父节点变为红色,以祖父节点为支点右(左)旋
//
// 参考博客: http://blog.csdn.net/v_JULY_v/article/details/6105630
// http://blog.csdn.net/v_JULY_v/article/details/6109153
template <class NodePtr>
void rb_tree_insert_rebalance(NodePtr x, NodePtr& root) noexcept
{
rb_tree_set_red(x); // 新增节点为红色
while (x != root && rb_tree_is_red(x->parent))
{
if (rb_tree_is_lchild(x->parent))
{ // 如果父节点是左子节点
auto uncle = x->parent->parent->right;
if (uncle != nullptr && rb_tree_is_red(uncle))
{ // case 3: 父节点和叔叔节点都为红
rb_tree_set_black(x->parent);
rb_tree_set_black(uncle);
x = x->parent->parent;
rb_tree_set_red(x);
}
else
{ // 无叔叔节点或叔叔节点为黑
if (!rb_tree_is_lchild(x))
{ // case 4: 当前节点 x 为右子节点
x = x->parent;
rb_tree_rotate_left(x, root);
}
// 都转换成 case 5: 当前节点为左子节点
rb_tree_set_black(x->parent);
rb_tree_set_red(x->parent->parent);
rb_tree_rotate_right(x->parent->parent, root);
break;
}
}
else // 如果父节点是右子节点,对称处理
{
auto uncle = x->parent->parent->left;
if (uncle != nullptr && rb_tree_is_red(uncle))
{ // case 3: 父节点和叔叔节点都为红
rb_tree_set_black(x->parent);
rb_tree_set_black(uncle);
x = x->parent->parent;
rb_tree_set_red(x);
// 此时祖父节点为红,可能会破坏红黑树的性质,令当前节点为祖父节点,继续处理
}
else
{ // 无叔叔节点或叔叔节点为黑
if (rb_tree_is_lchild(x))
{ // case 4: 当前节点 x 为左子节点
x = x->parent;
rb_tree_rotate_right(x, root);
}
// 都转换成 case 5: 当前节点为左子节点
rb_tree_set_black(x->parent);
rb_tree_set_red(x->parent->parent);
rb_tree_rotate_left(x->parent->parent, root);
break;
}
}
}
rb_tree_set_black(root); // 根节点永远为黑
}
// 删除节点后使 rb tree 重新平衡,参数一为要删除的节点,参数二为根节点,参数三为最小节点,参数四为最大节点
//
// 参考博客: http://blog.csdn.net/v_JULY_v/article/details/6105630
// http://blog.csdn.net/v_JULY_v/article/details/6109153z
template <class NodePtr>
NodePtr rb_tree_erase_rebalance(NodePtr z, NodePtr& root, NodePtr& leftmost, NodePtr& rightmost)
{
// y 是可能的替换节点,指向最终要删除的节点
auto y = (z->left == nullptr || z->right == nullptr) ? z : rb_tree_next(z);
// x 是 y 的一个独子节点或 NIL 节点
auto x = y->left != nullptr ? y->left : y->right;
// xp 为 x 的父节点
NodePtr xp = nullptr;
// y != z 说明 z 有两个非空子节点,此时 y 指向 z 右子树的最左节点,x 指向 y 的右子节点。
// 用 y 顶替 z 的位置,用 x 顶替 y 的位置,最后用 y 指向 z
if (y != z)
{
z->left->parent = y;
y->left = z->left;
// 如果 y 不是 z 的右子节点,那么 z 的右子节点一定有左孩子
if (y != z->right)
{ // x 替换 y 的位置
xp = y->parent;
if (x != nullptr)
x->parent = y->parent;
y->parent->left = x;
y->right = z->right;
z->right->parent = y;
}
else
{
xp = y;
}
// 连接 y 与 z 的父节点
if (root == z)
root = y;
else if (rb_tree_is_lchild(z))
z->parent->left = y;
else
z->parent->right = y;
y->parent = z->parent;
mystl::swap(y->color, z->color);
y = z;
}
// y == z 说明 z 至多只有一个孩子
else
{
xp = y->parent;
if (x)
x->parent = y->parent;
// 连接 x 与 z 的父节点
if (root == z)
root = x;
else if (rb_tree_is_lchild(z))
z->parent->left = x;
else
z->parent->right = x;
// 此时 z 有可能是最左节点或最右节点,更新数据
if (leftmost == z)
leftmost = x == nullptr ? xp : rb_tree_min(x);
if (rightmost == z)
rightmost = x == nullptr ? xp : rb_tree_max(x);
}
// 此时,y 指向要删除的节点,x 为替代节点,从 x 节点开始调整。
// 如果删除的节点为红色,树的性质没有被破坏,否则按照以下情况调整(x 为左子节点为例):
// case 1: 兄弟节点为红色,令父节点为红,兄弟节点为黑,进行左(右)旋,继续处理
// case 2: 兄弟节点为黑色,且两个子节点都为黑色或 NIL,令兄弟节点为红,父节点成为当前节点,继续处理
// case 3: 兄弟节点为黑色,左子节点为红色或 NIL,右子节点为黑色或 NIL,
// 令兄弟节点为红,兄弟节点的左子节点为黑,以兄弟节点为支点右(左)旋,继续处理
// case 4: 兄弟节点为黑色,右子节点为红色,令兄弟节点为父节点的颜色,父节点为黑色,兄弟节点的右子节点
// 为黑色,以父节点为支点左(右)旋,树的性质调整完成,算法结束
if (!rb_tree_is_red(y))
{ // x 为黑色时,调整,否则直接将 x 变为黑色即可
while (x != root && (x == nullptr || !rb_tree_is_red(x)))
{
if (x == xp->left)
{ // 如果 x 为左子节点
auto brother = xp->right;
if (rb_tree_is_red(brother))
{ // case 1
rb_tree_set_black(brother);
rb_tree_set_red(xp);
rb_tree_rotate_left(xp, root);
brother = xp->right;
}
// case 1 转为为了 case 2、3、4 中的一种
if ((brother->left == nullptr || !rb_tree_is_red(brother->left)) &&
(brother->right == nullptr || !rb_tree_is_red(brother->right)))
{ // case 2
rb_tree_set_red(brother);
x = xp;
xp = xp->parent;
}
else
{
if (brother->right == nullptr || !rb_tree_is_red(brother->right))
{ // case 3
if (brother->left != nullptr)
rb_tree_set_black(brother->left);
rb_tree_set_red(brother);
rb_tree_rotate_right(brother, root);
brother = xp->right;
}
// 转为 case 4
brother->color = xp->color;
rb_tree_set_black(xp);
if (brother->right != nullptr)
rb_tree_set_black(brother->right);
rb_tree_rotate_left(xp, root);
break;
}
}
else // x 为右子节点,对称处理
{
auto brother = xp->left;
if (rb_tree_is_red(brother))
{ // case 1
rb_tree_set_black(brother);
rb_tree_set_red(xp);
rb_tree_rotate_right(xp, root);
brother = xp->left;
}
if ((brother->left == nullptr || !rb_tree_is_red(brother->left)) &&
(brother->right == nullptr || !rb_tree_is_red(brother->right)))
{ // case 2
rb_tree_set_red(brother);
x = xp;
xp = xp->parent;
}
else
{
if (brother->left == nullptr || !rb_tree_is_red(brother->left))
{ // case 3
if (brother->right != nullptr)
rb_tree_set_black(brother->right);
rb_tree_set_red(brother);
rb_tree_rotate_left(brother, root);
brother = xp->left;
}
// 转为 case 4
brother->color = xp->color;
rb_tree_set_black(xp);
if (brother->left != nullptr)
rb_tree_set_black(brother->left);
rb_tree_rotate_right(xp, root);
break;
}
}
}
if (x != nullptr)
rb_tree_set_black(x);
}
return y;
}
// 模板类 rb_tree
// 参数一代表数据类型,参数二代表键值比较类型
template <class T, class Compare>
class rb_tree
{
public:
// rb_tree 的嵌套型别定义
typedef rb_tree_traits<T> tree_traits;
typedef rb_tree_value_traits<T> value_traits;
typedef typename tree_traits::base_type base_type;
typedef typename tree_traits::base_ptr base_ptr;
typedef typename tree_traits::node_type node_type;
typedef typename tree_traits::node_ptr node_ptr;
typedef typename tree_traits::key_type key_type;
typedef typename tree_traits::mapped_type mapped_type;
typedef typename tree_traits::value_type value_type;
typedef Compare key_compare;
typedef mystl::allocator<T> allocator_type;
typedef mystl::allocator<T> data_allocator;
typedef mystl::allocator<base_type> base_allocator;
typedef mystl::allocator<node_type> node_allocator;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::size_type size_type;
typedef typename allocator_type::difference_type difference_type;
typedef rb_tree_iterator<T> iterator;
typedef rb_tree_const_iterator<T> const_iterator;
typedef mystl::reverse_iterator<iterator> reverse_iterator;
typedef mystl::reverse_iterator<const_iterator> const_reverse_iterator;
allocator_type get_allocator() const { return node_allocator(); }
key_compare key_comp() const { return key_comp_; }
private:
// 用以下三个数据表现 rb tree
base_ptr header_; // 特殊节点,与根节点互为对方的父节点
size_type node_count_; // 节点数
key_compare key_comp_; // 节点键值比较的准则
private:
// 以下三个函数用于取得根节点,最小节点和最大节点
base_ptr& root() const { return header_->parent; }
base_ptr& leftmost() const { return header_->left; }
base_ptr& rightmost() const { return header_->right; }
public:
// 构造、复制、析构函数
rb_tree() { rb_tree_init(); }
rb_tree(const rb_tree& rhs);
rb_tree(rb_tree&& rhs) noexcept;
rb_tree& operator=(const rb_tree& rhs);
rb_tree& operator=(rb_tree&& rhs);
~rb_tree() { clear(); }
public:
// 迭代器相关操作
iterator begin() noexcept
{
return leftmost();
}
const_iterator begin() const noexcept
{
return leftmost();
}
iterator end() noexcept
{
return header_;
}
const_iterator end() const noexcept
{
return header_;
}
reverse_iterator rbegin() noexcept
{
return reverse_iterator(end());
}
const_reverse_iterator rbegin() const noexcept
{
return const_reverse_iterator(end());
}
reverse_iterator rend() noexcept
{
return reverse_iterator(begin());
}
const_reverse_iterator rend() const noexcept
{
return const_reverse_iterator(begin());
}
const_iterator cbegin() const noexcept
{
return begin();
}
const_iterator cend() const noexcept
{
return end();
}
const_reverse_iterator crbegin() const noexcept
{
return rbegin();
}
const_reverse_iterator crend() const noexcept
{
return rend();
}
// 容量相关操作
bool empty() const noexcept { return node_count_ == 0; }
size_type size() const noexcept { return node_count_; }
size_type max_size() const noexcept { return static_cast<size_type>(-1); }
// 插入删除相关操作
// emplace
template <class ...Args>
iterator emplace_multi(Args&& ...args);
template <class ...Args>
mystl::pair<iterator, bool> emplace_unique(Args&& ...args);
template <class ...Args>
iterator emplace_multi_use_hint(iterator hint, Args&& ...args);
template <class ...Args>
iterator emplace_unique_use_hint(iterator hint, Args&& ...args);
// insert
iterator insert_multi(const value_type& value);
iterator insert_multi(value_type&& value)
{
return emplace_multi(mystl::move(value));
}
iterator insert_multi(iterator hint, const value_type& value)
{
return emplace_multi_use_hint(hint, value);
}
iterator insert_multi(iterator hint, value_type&& value)
{
return emplace_multi_use_hint(hint, mystl::move(value));
}
template <class InputIterator>
void insert_multi(InputIterator first, InputIterator last)
{
size_type n = mystl::distance(first, last);
THROW_LENGTH_ERROR_IF(node_count_ > max_size() - n, "rb_tree<T, Comp>'s size too big");
for (; n > 0; --n, ++first)
insert_multi(end(), *first);
}
mystl::pair<iterator, bool> insert_unique(const value_type& value);
mystl::pair<iterator, bool> insert_unique(value_type&& value)
{
return emplace_unique(mystl::move(value));
}
iterator insert_unique(iterator hint, const value_type& value)
{
return emplace_unique_use_hint(hint, value);
}
iterator insert_unique(iterator hint, value_type&& value)
{
return emplace_unique_use_hint(hint, mystl::move(value));
}
template <class InputIterator>
void insert_unique(InputIterator first, InputIterator last)
{
size_type n = mystl::distance(first, last);
THROW_LENGTH_ERROR_IF(node_count_ > max_size() - n, "rb_tree<T, Comp>'s size too big");
for (; n > 0; --n, ++first)
insert_unique(end(), *first);
}
// erase
iterator erase(iterator hint);
size_type erase_multi(const key_type& key);
size_type erase_unique(const key_type& key);
void erase(iterator first, iterator last);
void clear();
// rb_tree 相关操作
iterator find(const key_type& key);
const_iterator find(const key_type& key) const;
size_type count_multi(const key_type& key) const
{
auto p = equal_range_multi(key);
return static_cast<size_type>(mystl::distance(p.first, p.second));
}
size_type count_unique(const key_type& key) const
{
return find(key) != end() ? 1 : 0;
}
iterator lower_bound(const key_type& key);
const_iterator lower_bound(const key_type& key) const;
iterator upper_bound(const key_type& key);
const_iterator upper_bound(const key_type& key) const;
mystl::pair<iterator, iterator>
equal_range_multi(const key_type& key)
{
return mystl::pair<iterator, iterator>(lower_bound(key), upper_bound(key));
}
mystl::pair<const_iterator, const_iterator>
equal_range_multi(const key_type& key) const
{
return mystl::pair<const_iterator, const_iterator>(lower_bound(key), upper_bound(key));
}
mystl::pair<iterator, iterator>
equal_range_unique(const key_type& key)
{
iterator it = find(key);
auto next = it;
return it == end() ? mystl::make_pair(it, it) : mystl::make_pair(it, ++next);
}
mystl::pair<const_iterator, const_iterator>
equal_range_unique(const key_type& key) const
{
const_iterator it = find(key);
auto next = it;
return it == end() ? mystl::make_pair(it, it) : mystl::make_pair(it, ++next);
}
void swap(rb_tree& rhs) noexcept;
private:
// node related
template <class ...Args>
node_ptr create_node(Args&&... args);
node_ptr clone_node(base_ptr x);
void destroy_node(node_ptr p);
// init / reset
void rb_tree_init();
void reset();
// get insert pos
mystl::pair<base_ptr, bool>
get_insert_multi_pos(const key_type& key);
mystl::pair<mystl::pair<base_ptr, bool>, bool>
get_insert_unique_pos(const key_type& key);
// insert value / insert node
iterator insert_value_at(base_ptr x, const value_type& value, bool add_to_left);
iterator insert_node_at(base_ptr x, node_ptr node, bool add_to_left);
// insert use hint
iterator insert_multi_use_hint(iterator hint, key_type key, node_ptr node);
iterator insert_unique_use_hint(iterator hint, key_type key, node_ptr node);
// copy tree / erase tree
base_ptr copy_from(base_ptr x, base_ptr p);
void erase_since(base_ptr x);
};
/*****************************************************************************************/
// 复制构造函数
template <class T, class Compare>
rb_tree<T, Compare>::
rb_tree(const rb_tree& rhs)
{
rb_tree_init();
if (rhs.node_count_ != 0)
{
root() = copy_from(rhs.root(), header_);
leftmost() = rb_tree_min(root());
rightmost() = rb_tree_max(root());
}
node_count_ = rhs.node_count_;
key_comp_ = rhs.key_comp_;
}
// 移动构造函数
template <class T, class Compare>
rb_tree<T, Compare>::
rb_tree(rb_tree&& rhs) noexcept
:header_(mystl::move(rhs.header_)),
node_count_(rhs.node_count_),
key_comp_(rhs.key_comp_)
{
rhs.reset();