-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_redblacktree.hpp
executable file
·741 lines (676 loc) · 18.1 KB
/
ft_redblacktree.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_redblacktree.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: areggie <areggie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/24 19:16:57 by areggie #+# #+# */
/* Updated: 2022/06/15 11:56:10 by areggie ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_REDBLACKTREE_HPP
# define FT_REDBLACKTREE_HPP// need to have a gap
#include <memory>
#include "ft_reverse_iterator.hpp"
#include "ft_tree_iterator.hpp"
#include "ft_utility.hpp"
template<class Value, class Compare = std::less<Value>, class Alloc = std::allocator<Value> >
class RBTree
{
public:
typedef Value value_type;
typedef Compare value_compare;
typedef Alloc allocator_type;
//https://stackoverflow.com/questions/14148756/what-does-template-rebind-do
//https://cplusplus.com/reference/memory/allocator/rebind/
//https://docs.microsoft.com/ru-ru/cpp/standard-library/allocator-class?view=msvc-170 this explanation is good and has code
typedef typename Alloc::template
rebind<Node<Value> >::other node_allocator; // allocating memory to the other than container type of object: NODE
typedef typename node_allocator::pointer node_pointer;
typedef typename allocator_type::reference reference;
typedef typename allocator_type::const_reference const_reference;
typedef typename allocator_type::pointer pointer;
typedef typename allocator_type::const_pointer const_pointer;
typedef std::ptrdiff_t difference_type;
typedef std::size_t size_type;
typedef TreeIter<Value> iterator; //tree iterator
typedef TreeIter<const Value> const_iterator; //const iterator
typedef ft::reverse_iterator<iterator> reverse_iterator;
typedef ft::reverse_iterator<const_iterator> const_reverse_iterator;
private:
allocator_type _val_alloc;
node_allocator _node_alloc;
value_compare _compare;
node_pointer _nil;
node_pointer _header;
node_pointer _root;
size_type _size;
//HELPER FUNCTIONS
node_pointer tree_min(node_pointer n) const
{
while (n != NULL && !is_nil(n->left))
n = n->left;
return n;
}
node_pointer tree_max(node_pointer n) const
{
while (n != NULL && !is_nil(n->right))
n = n->right;
return n;
}
void _rotate_right(node_pointer node)
{
node_pointer y;
y = node->left;
node->left = y->right;
if (!is_nil(y->right))
y->right->parent = node;
y->parent = node->parent;
if (node->parent == NULL)
_root = y;
else if (node == node->parent->left)
node->parent->left = y;
else
node->parent->right = y;
y->right = node;
node->parent = y;
}
void _rotate_left(node_pointer node)
{
node_pointer y;
y = node->right;
node->right = y->left;
if (!is_nil(y->left))
y->left->parent = node;
y->parent = node->parent;
if (node->parent == NULL)
_root = y;
else if (node == node->parent->left)
node->parent->left = y;
else
node->parent->right = y;
y->left = node;
node->parent = y;
}
node_pointer _insert(node_pointer new_node)
{
if (_root == _header)
_root = new_node;
else
_insert_to_node(_root, new_node);
return new_node;
}
node_pointer _insert_to_node(node_pointer root, node_pointer new_node)
{
if (_compare(*new_node->value, *root->value))
{
if (!is_nil(root->left))
return (_insert_to_node(root->left, new_node));
root->left = new_node;
}
else{
if (!is_nil(root->right))
return (_insert_to_node(root->right, new_node));
root->right = new_node;
}
new_node->parent = root;
return (new_node);
}
node_pointer _insert_into_tree(node_pointer new_node, node_pointer where)
{
if (_root == _header)
_root = new_node;
else
_insert_to_node(where, new_node);
return (new_node);
}
//change color
void _insert_fixup(node_pointer node)
{
if (node != _root && node->parent != _root)
{
while (node != _root && !node->parent->is_black)
{
if (node->parent == node->parent->parent->left)
{
node_pointer uncle = node->parent->parent->right;
if (!uncle->is_black)
{
node->parent->is_black = true;
uncle->is_black = true;
node->parent->parent->is_black = false;
node = node->parent->parent;
}
else
{
if (node == node->parent->right)
{
node = node->parent;
_rotate_left(node);
}
node->parent->is_black = true;
node->parent->parent->is_black = false;
_rotate_right(node->parent->parent);
}
}
else
{
node_pointer uncle = node->parent->parent->left;
if (!uncle->is_black)
{
node->parent->is_black = true;
uncle->is_black = true;
node->parent->parent->is_black = false;
node = node->parent->parent;
}
else
{
if (node == node->parent->left)
{
node = node->parent;
_rotate_right(node);
}
node->parent->is_black = true;
node->parent->parent->is_black = false;
_rotate_left(node->parent->parent);
}
}
}
}
_root->is_black = true;
}
bool is_nil(node_pointer node) const
{
return node == _nil || node == _header;
}
void clear_node(node_pointer node)
{
if (node && !is_nil(node))
{
clear_node(node->right);
clear_node(node->left);
_val_alloc.destroy(node->value);
_val_alloc.deallocate(node->value, 1);
_node_alloc.deallocate(node, 1);
}
}
void init_nil_head()
{
_nil = _node_alloc.allocate(1);
_node_alloc.construct(_nil, Node<Value>());
_nil->is_black = true;
_nil->is_nil = true;
_header = _node_alloc.allocate(1);
_node_alloc.construct(_header, Node<Value>());
_header->value = _val_alloc.allocate(1);
_val_alloc.construct(_header->value, Value());
_header->is_black = true;
}
void transplant(node_pointer where, node_pointer what)
{
if (where == _root)
_root = what;
else if (where == where->parent->left)
where->parent->left = what;
else
where->parent->right = what;
what->parent = where->parent;
}
void free_node(node_pointer node)
{
_val_alloc.destroy(node->value);
_val_alloc.deallocate(node->value, 1);
_node_alloc.deallocate(node, 1);
}
//PUBLIC FUNCTIONS
public:
iterator end()
{
return (iterator(_header));
}
const_iterator end() const
{
return (const_iterator(_header));
}
iterator begin()
{
return (iterator(_size == 0 ? _header : iterator(tree_min(_root))));
}
const_iterator begin() const
{
return (const_iterator(_size == 0 ? _header : const_iterator(tree_min(_root))));
}
reverse_iterator rbegin()
{
return (reverse_iterator(end()));
}
const_reverse_iterator rbegin() const
{
return (const_reverse_iterator(end()));
}
reverse_iterator rend()
{
return (reverse_iterator(begin()));
}
const_reverse_iterator rend() const
{
return (const_reverse_iterator(begin()));
}
pointer create_value(const value_type &value)
{
pointer new_val = _val_alloc.allocate(1);
_val_alloc.construct(new_val, value);
return new_val;
}
node_pointer copy_node(node_pointer other)
{
node_pointer new_node = _node_alloc.allocate(1);
_node_alloc.construct(new_node, Node<Value>());
new_node->is_black = other->is_black;
new_node->is_nil = other->is_nil;
if (other->value)
{
new_node->value = _val_alloc.allocate(1);
_val_alloc.construct(new_node->value, *other->value);
}
return (new_node);
}
void copy_child(node_pointer my_node, node_pointer other)
{
if (other &&other->left->is_nil)
my_node->left = _nil;
else
{
my_node->left = copy_node(other->left);
my_node->left->parent = my_node;
copy_child(my_node->left, other->left);
}
if (other->right->is_nil)
my_node->right = _nil;
else if (other->right->right == NULL)
{
my_node->right = _header;
_header->parent = my_node;
}
else
{
my_node->right = copy_node(other->right);
my_node->right->parent = my_node;
copy_child(my_node->right, other->right);
}
}
node_pointer search(const value_type &value, node_pointer node) const
{
if(!node || is_nil(node))
return NULL;
if (_compare(value, *node->value))
return search(value, node->left);
if (_compare(*node->value, value))
return search(value, node->right);
return node;
}
iterator find(const value_type& value)
{
node_pointer find_res = search(value, _root);
return (find_res == NULL ? end() : iterator(find_res));
}
const_iterator find(const value_type& value) const
{
node_pointer find_res = search(value, _root);
return (find_res == NULL ? end() : const_iterator(find_res));
}
ft::pair<iterator, bool> insert(value_type const &value)
{
node_pointer find_val = search(value, _root);
if (find_val)
return ft::pair<iterator, bool>(iterator(find_val), false);
node_pointer new_node = _node_alloc.allocate(1);
_node_alloc.construct(new_node, Node<value_type>(create_value(value)));
new_node->left = _nil;
new_node->right = _nil;
_insert_into_tree(new_node, _root);
ft::pair<iterator, bool> res(iterator(new_node), true);
_insert_fixup(new_node);
_size++;
new_node = tree_max(_root);
new_node->right = _header;
_header->parent = new_node;
return res;
}
iterator insert(iterator position, const value_type& value)
{
node_pointer new_node = search(value,_root);
if (new_node)
return (iterator(new_node));
new_node = _node_alloc.allocate(1);
_node_alloc.construct(new_node, Node<value_type>(create_value(value)));
new_node->left = _nil;
new_node->right = _nil;
if (position == begin())
{
if (position != end() && _compare(value, *position))
_insert_into_tree(new_node, tree_min(_root));
else
_insert_into_tree(new_node, _root);
}
else if (position == end())
{
if (position != begin() && _compare(*(--position), value))
_insert_into_tree(new_node, _header->parent);
else
_insert_into_tree(new_node, _root);
}
else
_insert_into_tree(new_node, _root);
_insert_fixup(new_node);
_size++;
node_pointer max_of_tree = tree_max(_root);
max_of_tree->right = _header;
_header->parent = max_of_tree;
return (iterator(new_node));
}
template<class InputIt>
void insert(typename ft::enable_if< !ft::is_integral<InputIt>::value, InputIt >::type first,
InputIt last)
{
for (; first != last; ++first)
insert(*first);
}
void erase(iterator pos)
{
//if 1 child
node_pointer y = pos.node(), x, for_free = y;//pos.node - remove const //x for fixup
bool y_original_is_black = y->is_black;
if (is_nil(y->left))
{
x = y->right;
transplant(y, y->right);
}
else if (is_nil(y->right))
{
x = y->left;
transplant(y, y->left);
}
else {//if 2 child
node_pointer z = y;
y = tree_min(z->right);//succesor
y_original_is_black = y->is_black;
x = y->right;
if (y->parent != z)
{
transplant(y, y->right);
y->right = z->right;
z->right->parent = y;
}
transplant(z, y);
y->left = z->left;
y->left->parent = y;
y->is_black = z->is_black;
}
free_node(for_free);
if (y_original_is_black){
erase_fixup(x);//if del black node // here was seg
}
_size--;
_nil->parent = NULL;
if (_size == 0)
_root = _header;
else
{
if (_size != 1)
x = tree_max(_root);
else
x = _root;
x->right = _header;
_header->parent = x;
}
}
size_type erase(const value_type& value)
{
node_pointer res = search(value, _root);
if (res)
erase(iterator(res));
return (res != NULL);
}
void erase(iterator first, iterator last)
{
while (first != last)
erase(first++);
}
void erase_fixup(node_pointer x)
{
node_pointer brother;
while (x != _root && x->is_black)
{
if (x == x->parent->left)
{
brother = x->parent->right;
//case 1
if (!brother->is_black)
{
brother->is_black = true;
x->parent->is_black = false;
_rotate_left(x->parent);
brother = x->parent->right;
}
//case 2
if (brother->left->is_black && brother->right->is_black)
{
brother->is_black = false;
x = x->parent;
}
else
{
//case 3
if (brother->right->is_black)
{
brother->left->is_black = true;
brother->is_black = false;
_rotate_right(brother);
brother = x->parent->right;
}
//case 4
brother->is_black = x->parent->is_black;
x->parent->is_black = true;
brother->right->is_black = true;
_rotate_left(x->parent);
x = _root;
}
}
else
{
brother = x->parent->left;
//case 1
if (!brother->is_black)
{
brother->is_black = true;
x->parent->is_black = false;
_rotate_right(x->parent);
brother = x->parent->left;
}
//case 2
if (brother->right->is_black && brother->left->is_black)
{
brother->is_black = false;
x = x->parent;
}
else
{
//case 3
if (brother->left->is_black)
{
brother->right->is_black = true;
brother->is_black = false;
_rotate_left(brother);
brother = x->parent->left;
}
//case 4
brother->is_black = x->parent->is_black;
x->parent->is_black = true;
brother->left->is_black = true;
_rotate_right(x->parent);
x = _root;
}
}
}
}
//CONSTRUCTORS
RBTree(const Compare &comp, const allocator_type& a = allocator_type()):
_val_alloc(a), _node_alloc(node_allocator()), _compare(comp), _root(0), _size(0)
{
init_nil_head();
_root = _header;
}
RBTree() : _root(0), _val_alloc(allocator_type()), _node_alloc(node_allocator()), _compare(value_compare()), _size(0)
{
init_nil_head();
_root = _header;
}
RBTree(const RBTree& src) : _compare(src._compare), _root(NULL)
{
*this = src;
}
template<class InputIt>
RBTree(typename ft::enable_if< !ft::is_integral<InputIt>::value, InputIt >::type first, InputIt last,
const value_compare& comp, const allocator_type& alloc = allocator_type()): _val_alloc(alloc),
_node_alloc(node_allocator()), _compare(comp)
{
init_nil_head();
_root = _header;
for (; first != last; ++first)
insert(*first);
}
RBTree& operator=(const RBTree & src)
{
if (this == &src)
return *this;
this->_node_alloc = src._node_alloc;
this->_val_alloc = src._val_alloc;
this->_compare = src._compare;
if (this->_root == NULL)
init_nil_head();
else
clear_node(_root);
if (src._size == 0)
this->_root = this->_header;
else {
this->_root = copy_node(src._root);
copy_child(this->_root, src._root);
}
this->_size = src._size;
return *this;
}
~RBTree()
{
clear_node(_root);
_val_alloc.destroy(_header->value);
_val_alloc.deallocate(_header->value, 1);
_node_alloc.deallocate(_nil, 1);
_node_alloc.deallocate(_header, 1);
}
size_type size() const
{
return (_size);
}
size_type max_size() const
{
return (_val_alloc.max_size());
}
bool empty() const{
return (_size == 0);
}
value_compare value_comp() const
{
return (_compare);
}
void clear()
{
clear_node(_root);
_root = _header;
_header->parent = NULL;
_size = 0;
}
size_type count(const value_type& value) const
{
return (find(value) != end());
}
iterator lower_bound(const value_type& value){
iterator last = end();
for (iterator first = begin(); first != last; ++first)
{
if(!_compare(*first, value))
return (first);
}
return (last);
}
const_iterator lower_bound(const value_type& value) const
{
const_iterator last = end();
for (const_iterator first = begin(); first != last; ++first)
{
if(!_compare(*first, value))
return (first);
}
return (last);
}
iterator upper_bound(const value_type& value)
{
iterator last = end();
for (iterator first = begin(); first != last; ++first)
{
if(_compare(value, *first))
return (first);
}
return (last);
}
const_iterator upper_bound(const value_type& value) const
{
const_iterator last = end();
for (const_iterator first = begin(); first != last; ++first)
{
if(_compare(value, *first))
return (first);
}
return (last);
}
void swap(RBTree &other){
std::swap(this->_root, other._root);
std::swap(this->_nil, other._nil);
std::swap(this->_header, other._header);
std::swap(this->_size, other._size);
std::swap(this->_node_alloc, other._node_alloc);
std::swap(this->_val_alloc, other._val_alloc);
std::swap(this->_compare, other._compare);
}
ft::pair<iterator, iterator> equal_range(const value_type &value)
{
return (ft::make_pair(lower_bound(value), upper_bound(value)));
}
allocator_type get_allocator() const
{
return (_val_alloc);
}
};
template<class Content, class Compare, class Alloc>
bool operator<(const RBTree<Content, Compare, Alloc>& lhs, const RBTree<Content, Compare, Alloc>& rhs)
{
return (ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()));
}
template<class Content, class Compare, class Alloc>
bool operator>(const RBTree<Content, Compare, Alloc>& lhs, const RBTree<Content, Compare, Alloc>& rhs)
{
return (lhs < rhs);
}
// ft::equal used
template<class Content, class Compare, class Alloc>
bool operator==(const RBTree<Content, Compare, Alloc>& lhs, const RBTree<Content, Compare, Alloc>& rhs)
{
return (lhs.size() == rhs.size() && ft::equal(lhs.begin(), lhs.end(), rhs.begin()));
}
template<class Content, class Compare, class Alloc>
void swap(const RBTree<Content, Compare, Alloc>& lhs, const RBTree<Content, Compare, Alloc>& rhs)
{
lhs.swap(rhs);
}
#endif