-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmystl_rbtree.hpp
596 lines (497 loc) · 16 KB
/
mystl_rbtree.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
#ifndef _MYSTL_RBTREE_H
#define _MYSTL_RBTREE_H
#include "mystl_algobase.hpp"
#include "mystl_iterator_base.hpp"
#include "mystl_pair.hpp"
namespace numb {
enum _RB_COLOR { RED, BLACK };
template <typename _Val>
struct _rb_node {
typedef _rb_node* _Link_type;
typedef const _rb_node* _Const_Link_type;
static _Link_type _Nil;
_Link_type _parent;
_Link_type _left;
_Link_type _right;
_Val _value_field;
_RB_COLOR _c;
_rb_node() : _left(_Nil), _right(_Nil), _c(BLACK) {}
explicit _rb_node(_Val _x, _RB_COLOR _co, _Link_type _p = NULL, _Link_type _l = _Nil,
_Link_type _r = _Nil)
: _value_field(_x), _parent(_p), _left(_l), _right(_r), _c(_co) {}
_Link_type _maximum()
{ return (_right == _Nil) ? this : _right->_maximum(); }
_Link_type _minimum()
{ return (_left == _Nil) ? this : _left->_minimum(); }
_Link_type _suuc()
{
if (_right != _Nil)
return _right->_minimum();
else {
_Link_type _x = this;
_Link_type _y = _x->_parent;
while (_y != _Nil && _x == _y->_right) {
_x = _y;
_y = _y->_parent;
}
return _y;
}
}
_Link_type _prev()
{
if (_left != _Nil)
return _left->_maximum();
else {
_Link_type _x = this;
_Link_type _y = _x->_parent;
while (_y != _Nil && _x == _y->_left) {
_x = _y;
_y = _y->_parent;
}
return _y;
}
}
private:
static _rb_node SENTINEL_OBJ;
};
template <typename _Val>
_rb_node<_Val> _rb_node<_Val>::SENTINEL_OBJ = _rb_node<_Val>();
template <typename _Val>
_rb_node<_Val>* _rb_node<_Val>::_Nil = &_rb_node<_Val>::SENTINEL_OBJ;
template <typename _Tp>
struct _RB_iterator {
typedef _Tp value_type;
typedef _Tp& reference;
typedef _Tp* pointer;
typedef Bidirectional_iterator_tag iterator_category;
typedef ptrdiff_t difference_type;
typedef _RB_iterator<_Tp> _Self;
typedef _rb_node<_Tp>* _Link_type;
_Link_type _node;
_RB_iterator() : _node() {}
explicit _RB_iterator(_Link_type _x) : _node(_x) {}
reference operator*() const { return _node->_value_field; }
pointer operator->() const { return __Addressof(_node->_value_field); }
_Self& operator++() {
_node = _node->_suuc();
return *this;
}
_Self operator++(int) {
_Self _tmp = *this;
_node = _node->_suuc();
return _tmp;
}
_Self& operator--() {
_node = _node->_prev();
return *this;
}
_Self operator--(int) {
_Self _tmp = *this;
_node = _node->_prev();
return _tmp;
}
bool operator==(const _Self& _x) const { return _node == _x._node; }
bool operator!=(const _Self& _x) const { return _node != _x._node; }
};
template <typename _Tp>
struct _RB_const_iterator {
typedef _Tp value_type;
typedef _Tp& reference;
typedef _Tp* pointer;
typedef _RB_iterator<_Tp>* BIterator;
typedef Bidirectional_iterator_tag iterator_category;
typedef ptrdiff_t difference_type;
typedef _RB_const_iterator<_Tp> _Self;
typedef _rb_node<_Tp>* _Link_type;
_Link_type _node;
_RB_const_iterator() : _node() {}
explicit _RB_const_iterator(_Link_type _x) : _node(_x) {}
// explicit _AVL_const_iterator(const BIterator& _it) : _node(_it._node) {}
// BIterator _const_cast() const
//{ return BIterator(const_cast<typename BIterator::_Link_type>(_node)); }
reference operator*() const { return _node->_value_field; }
pointer operator->() const { return __Addressof(_node->_value_field); }
_Self& operator++() {
_node = _node->_suuc();
return *this;
}
_Self operator++(int) {
_Self _tmp = *this;
_node = _node->_suuc();
return _tmp;
}
_Self& operator--() {
_node = _node->_prev();
return *this;
}
_Self operator--(int) {
_Self _tmp = *this;
_node = _node->_prev();
return _tmp;
}
bool operator==(const _Self& _x) const { return _node == _x._node; }
bool operator!=(const _Self& _x) const { return _node != _x._node; }
};
template <typename _Val>
inline bool operator==(const _RB_iterator<_Val>& _x,
const _RB_iterator<_Val>& _y) {
return _x._node == _y._node;
}
template <typename _Val>
inline bool operator!=(const _RB_iterator<_Val>& _x,
const _RB_iterator<_Val>& _y) {
return _x._node != _y._node;
}
template <typename _Key, typename _Val, typename _KeyOfValue, typename _Compare>
class _RBtree {
public:
typedef _Key key_type;
typedef _Val value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef _rb_node<_Val> _node_type;
typedef _rb_node<_Val>* _Link_type;
typedef const _rb_node<_Val>* _Const_Link_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef _RB_iterator<value_type> Iterator;
typedef _RB_const_iterator<value_type> Const_iterator;
static _Link_type _Nil;
private:
size_type _node_count;
_Link_type _root;
_Compare key_comp;
protected:
static const _Key& _S_key(_Const_Link_type _x) {
return _KeyOfValue()(_S_value(_x));
}
static const_reference _S_value(_Const_Link_type _x) {
return _x->_value_field;
}
_Link_type _find(const key_type& _x) const {
_Link_type _p = _root;
while (_p != _Nil && _x != _S_key(_p)) {
if (key_comp(_x, _S_key(_p)))
_p = _p->_left;
else
_p = _p->_right;
}
return _p;
}
_Link_type _succ(_Link_type _x) {
if (_x->_right != _Nil) {
_x = _x->_right;
while (_x->_left != _Nil) _x = _x->_left;
return _x;
} else {
_Link_type _y = _x->_parent;
while (_y != _Nil && _x == _y->_right) {
_x = _y;
_y = _y->_parent;
}
return _y;
}
}
void _left_rotate(_Link_type _x)
{
_Link_type _y = _x->_right;
_x->_right = _y->_left;
_y->_left->_parent = _x;
_y->_parent = _x->_parent;
if (_x->_parent == _Nil)
_root = _y;
else if (_x == _x->_parent->_left)
_x->_parent->_left = _y;
else
_x->_parent->_right = _y;
_y->_left = _x;
_x->_parent = _y;
}
void _right_rotate(_Link_type _x)
{
_Link_type _y = _x->_left;
_x->_left = _y->_right;
_y->_right->_parent = _x;
_y->_parent = _x->_parent;
if (_x->_parent == _Nil)
_root = _y;
else
if (_x == _x->_parent->_left)
_x->_parent->_left = _y;
else
_x->_parent->_right = _y;
_y->_right = _x;
_x->_parent = _y;
}
void _rb_insert_fixup(_Link_type _x)
{
while (_x->_parent->_c == RED) {
if (_x->_parent == _x->_parent->_parent->_left) {
_Link_type _y = _x->_parent->_parent->_right;
if (_y->_c == RED) {
_x->_parent->_c = BLACK;
_y->_c = BLACK;
_x->_parent->_parent->_c = RED;
_x = _x->_parent->_parent;
}
else {
if (_x == _x->_parent->_right) {
_x = _x->_parent;
_left_rotate(_x);
}
_x->_parent->_c = BLACK;
_x->_parent->_parent->_c = RED;
_right_rotate(_x->_parent->_parent);
}
}
else {
_Link_type _y = _x->_parent->_parent->_left;
if (_y->_c == RED) {
_x->_parent->_c = BLACK;
_y->_c = BLACK;
_x->_parent->_parent->_c = RED;
_x = _x->_parent->_parent;
}
else {
if (_x == _x->_parent->_left) {
_x = _x->_parent;
_right_rotate(_x);
}
_x->_parent->_c = BLACK;
_x->_parent->_parent->_c = RED;
_left_rotate(_x->_parent->_parent);
}
}
}
_root->_c = BLACK;
}
void _rb_del_fixup(_Link_type _x)
{
while (_x != _root && _x->_c == BLACK) {
if (_x == _x->_parent->_left) {
_Link_type _w = _x->_parent->_right;
if (_w->_c == RED) {
_x->_parent->_c = RED;
_w->_c = BLACK;
_left_rotate(_x->_parent);
_w = _x->_parent->_right;
}
if (_w->_left->_c == BLACK && _w->_right->_c == BLACK) {
_w->_c = RED;
_x = _x->_parent;
}
else if (_w->_right->_c == BLACK) {
_w->_left->_c = BLACK;
_w->_c = RED;
_right_rotate(_w);
_w = _x->_parent->_right;
}
_w->_c = _x->_parent->_c;
_x->_parent->_c = BLACK;
_w->_right->_c = BLACK;
_left_rotate(_x->_parent);
_x = _root;
}
else {
_Link_type _w = _x->_parent->_left;
if (_w->_c == RED) {
_x->_parent->_c = RED;
_w->_c = BLACK;
_right_rotate(_x->_parent);
_w = _x->_parent->_left;
}
if (_w->_left->_c == BLACK && _w->_right->_c == BLACK) {
_w->_c = RED;
_x = _x->_parent;
}
else if (_w->_left->_c == BLACK) {
_w->_right->_c = BLACK;
_w->_c = RED;
_left_rotate(_w);
_w = _x->_parent->_left;
}
_w->_c = _x->_parent->_c;
_x->_parent->_c = BLACK;
_w->_left->_c = BLACK;
_right_rotate(_x->_parent);
_x = _root;
}
}
_x->_c = BLACK;
}
Iterator _lower_bound(const _Key& _k)
{
_Link_type _x = _root;
_Link_type _y = _Nil;
while (_x != _Nil) {
if (!key_comp(_S_key(_x), _k))
_y = _x, _x = _x->_left;
else
_x = _x->_right;
}
return Iterator(_y);
}
Const_iterator _lower_bound(const _Key& _k) const
{
_Link_type _x = _root;
_Link_type _y = _Nil;
while (_x != _Nil) {
if (!key_comp(_S_key(_x), _k))
_y = _x, _x = _x->_left;
else
_x = _x->_right;
}
return Const_iterator(_y);
}
Iterator _upper_bound(const _Key& _k)
{
_Link_type _x = _root;
_Link_type _y = _Nil;
while (_x != _Nil) {
if (key_comp(_k, _S_key(_x)))
_y = _x, _x = _x->_left;
else
_x = _x->_right;
}
return Iterator(_y);
}
Const_iterator _upper_bound(const _Key& _k) const
{
_Link_type _x = _root;
_Link_type _y = _Nil;
while (_x != _Nil) {
if (key_comp(_k, _S_key(_x)))
_y = _x, _x = _x->_left;
else
_x = _x->_right;
}
return Const_iterator(_y);
}
void _destroy(_Link_type _r) {
if (_r == _Nil)
return;
_destroy(_r->_left);
_destroy(_r->_right);
delete _r;
}
public:
_RBtree() : _node_count(0), key_comp(_Compare()) {
_root = _Nil;
}
~_RBtree() { _destroy(_root); }
_RBtree(const _RBtree& _rhs) {
_node_count = 0;
_root = _Nil;
key_comp = _rhs.key_comp;
for (Const_iterator _it = _rhs.begin(); _it != _rhs.end(); ++_it)
_insert_unique(*_it);
}
_RBtree& operator=(const _RBtree& _rhs) {
_RBtree _copy(_rhs);
swap(_copy);
return *this;
}
void swap(_RBtree& _rhs) {
using numb::Swap;
Swap(_node_count, _rhs._node_count);
Swap(_root, _rhs._root);
Swap(key_comp, _rhs.key_comp);
}
template <typename _InputIterator>
_RBtree(_InputIterator _first, _InputIterator _last) {
_node_count = 0;
_root = _Nil;
for (; _first != _last; ++_first) _insert_unique(*_first);
}
_Compare key_compare() { return key_comp; }
Iterator begin() throw() {
return Iterator(_root->_minimum());
}
Iterator end() throw() { return Iterator(_Nil); }
Const_iterator begin() const throw() {
return Const_iterator(_root->_minimum());
}
Const_iterator end() const throw() { return Const_iterator(_Nil); }
size_type size() const { return _node_count; }
bool empty() const { return _node_count == 0; }
Pair<Iterator, bool> _insert_unique(const value_type& _x) {
_Link_type _hot = _Nil;
_Link_type _p = _root;
while (_p != _Nil) {
_hot = _p;
if (key_comp(_KeyOfValue()(_x), _S_key(_p)))
_p = _p->_left;
else if (key_comp(_S_key(_p), _KeyOfValue()(_x)))
_p = _p->_right;
else
return Make_pair(Iterator(_p), false);
}
_Link_type _cur = new _rb_node<value_type>(_x, RED, _hot, _Nil, _Nil);
if (_hot == _Nil)
_root = _cur;
else {
if (key_comp(_KeyOfValue()(_x), _S_key(_hot)))
_hot->_left = _cur;
else
_hot->_right = _cur;
}
_rb_insert_fixup(_cur);
Iterator _iter(_cur);
++_node_count;
return Make_pair(_iter, true);
}
void erase(const key_type& _x) {
_Link_type _del = _find(_x);
if (_del == _Nil) return;
_Link_type _real;
if (_del->_left == _Nil || _del->_right == _Nil)
_real = _del;
else
_real = _succ(_del);
if (_real != _del) {
key_type* _tmp = const_cast<key_type*>(&_del->_value_field.first);
*_tmp = _real->_value_field.first;
_del->_value_field.second = _real->_value_field.second;
}
_Link_type _child;
if (_real->_left != _Nil)
_child = _real->_left;
else
_child = _real->_right;
_child->_parent = _real->_parent;
if (_real->_parent == _Nil)
_root = _child;
else {
if (_real == _real->_parent->_left)
_real->_parent->_left = _child;
else
_real->_parent->_right = _child;
}
if (_real->_c == BLACK)
_rb_del_fixup(_child);
delete _real;
--_node_count;
}
Iterator maximum() const
{ return (empty()) ? end() : Iterator(_root->_maximum()); }
Iterator minimum() const
{ return (empty()) ? end() : Iterator(_root->_minimum()); }
Iterator find(const key_type& _x) { return Iterator(_find(_x)); }
Iterator lower_bound(const key_type& _x)
{ return _lower_bound(_x); }
Const_iterator lower_bound(const key_type& _x) const
{ return _lower_bound(_x); }
Iterator upper_bound(const key_type& _x)
{ return _upper_bound(_x); }
Const_iterator upper_bound(const key_type& _x) const
{ return _upper_bound(_x); }
Iterator _insert_equal(const value_type& _x);
};
template <typename _Key, typename _Val, typename _KeyOfValue, typename _Compare>
typename _RBtree<_Key, _Val, _KeyOfValue, _Compare>::_Link_type
_RBtree<_Key, _Val, _KeyOfValue, _Compare>::_Nil =
_RBtree<_Key, _Val, _KeyOfValue, _Compare>::_node_type::_Nil;
}
#endif