-
Notifications
You must be signed in to change notification settings - Fork 0
/
__tree.hpp
639 lines (577 loc) · 15.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* __tree.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mannouao <mannouao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/18 17:49:54 by mannouao #+# #+# */
/* Updated: 2022/07/31 10:48:51 by mannouao ### ########.fr */
/* */
/* ************************************************************************** */
/* READ-BLACK-TREE */
# ifndef __TREE_HPP
# define __TREE_HPP
# include "iterator.hpp"
# include "utility.hpp"
# include <memory>
namespace ft
{
const bool RED = 0;
const bool BLACK = 1;
template<class key_type, class value_type, class value_compare , class allocator_type>
class tree
{
private:
struct node {
bool __color_;
node* __parent_;
node* __left_;
node* __right_;
value_type *__value_;
node(bool c, node* p, node* l, node* r)
: __color_(c),
__parent_(p),
__left_(l),
__right_(r)
{}
};
typename allocator_type::template rebind<node>::other node_alloc;
allocator_type value_alloc;
public:
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::difference_type difference_type;
typedef typename allocator_type::size_type size_type;
typedef ft::__map_iter<pointer, node*> iterator;
typedef ft::__map_iter<const_pointer, node*> const_iterator;
private:
node* __NIL_;
node* __head_;
node* __begin_;
size_type __size_;
value_compare __compar_;
public:
tree(const value_compare& comp, const allocator_type& alloc)
:
value_alloc(alloc),
__size_(0),
__compar_(comp)
{ __NIL_ = __head_ = __begin_ = get_node(BLACK); }
tree(const tree& __x)
: value_alloc(__x.value_alloc),
__size_(0),
__compar_(__x.__compar_)
{ __NIL_ = __head_ = __begin_ = get_node(BLACK); }
~tree()
{
clear();
value_alloc.destroy(__NIL_->__value_);
value_alloc.deallocate(__NIL_->__value_, 1);
node_alloc.destroy(__NIL_);
node_alloc.deallocate(__NIL_, 1);
}
iterator begin() { return iterator(__begin_); }
const_iterator begin() const { return const_iterator(__begin_); }
iterator end() { return iterator(__NIL_); }
const_iterator end() const { return const_iterator(__NIL_); }
iterator lower_bound(const key_type& __k, node** parent = NULL)
{
node* __tmp = __head_;
node* __res = __NIL_;
while (__tmp != NULL && __tmp != __NIL_)
{
if (parent != NULL)
*parent = __tmp;
if (!__compar_(*(__tmp->__value_), __k))
{
__res = __tmp;
__tmp = __tmp->__left_;
}
else
__tmp = __tmp->__right_;
}
return iterator(__res);
}
const_iterator lower_bound(const key_type& __k, node** parent = NULL) const
{
node* __tmp = __head_;
node* __res = __NIL_;
while (__tmp != NULL && __tmp != __NIL_)
{
if (parent != NULL)
*parent = __tmp;
if (!__compar_(*(__tmp->__value_), __k))
{
__res = __tmp;
__tmp = __tmp->__left_;
}
else
__tmp = __tmp->__right_;
}
return const_iterator(__res);
}
iterator upper_bound(const key_type& __k)
{
node* __tmp = __head_;
node* __res = __NIL_;
while (__tmp != NULL)
{
if (__compar_(__k, *(__tmp->__value_)))
{
__res = __tmp;
__tmp = __tmp->__left_;
}
else
__tmp = __tmp->__right_;
}
return iterator(__res);
}
const_iterator upper_bound(const key_type& __k) const
{
node* __tmp = __head_;
node* __res = __NIL_;
while (__tmp != NULL)
{
if (__compar_(__k, *(__tmp->__value_)))
{
__res = __tmp;
__tmp = __tmp->__left_;
}
else
__tmp = __tmp->__right_;
}
return const_iterator(__res);
}
iterator find(const key_type& __k, node** parent = NULL)
{
iterator __p = lower_bound(__k, parent);
if (__p != end() && __get_key(*__p) == __k)
return __p;
return end();
}
const_iterator find(const key_type& __k, node** parent = NULL) const
{
const_iterator __p = lower_bound(__k, parent);
if (__p != end() && __get_key(*__p) == __k)
return __p;
return end();
}
void clear()
{
destroy(__head_);
__size_ = 0;
__NIL_->__parent_ = NULL;
__head_ = __begin_ = __NIL_;
}
value_compare value_comp() const { return __compar_; }
value_compare& value_comp() { return __compar_; }
size_type size() const { return __size_; }
size_type max_size() const { return value_alloc.max_size(); }
allocator_type get_allocator() const { return value_alloc; }
void swap(tree& __x)
{
ft::swap(__head_, __x.__head_);
ft::swap(__begin_, __x.__begin_);
ft::swap(__size_, __x.__size_);
ft::swap(__NIL_, __x.__NIL_);
ft::swap(__compar_, __x.__compar_);
}
ft::pair<iterator, bool> insert(const value_type& __v)
{
if (__head_ == __NIL_)
{
__begin_ = __head_ = get_node(BLACK, __v);
__NIL_->__parent_ = __head_;
__head_->__right_ = __NIL_;
++__size_;
return (ft::make_pair(iterator(__head_), true));
}
node *parent;
node* __holder;
iterator __p = find(__get_key(__v), &parent);
if (__p != end())
return ft::make_pair(__p, false);
__holder = get_node(RED, __v);
__holder->__parent_ = parent;
if (__compar_(__v, *(parent->__value_)))
{
parent->__left_ = __holder;
if (__begin_ == parent)
__begin_ = __holder;
}
else
{
parent->__right_ = __holder;
if (__NIL_->__parent_ == parent)
{
__NIL_->__parent_ = __holder;
__holder->__right_ = __NIL_;
}
}
balance(__holder);
++__size_;
return ft::make_pair(iterator(__holder), true);
}
iterator insert(iterator __hint, const value_type& __v)
{
if (__head_ == __NIL_)
{
__begin_ = __head_ = get_node(BLACK, __v);
__NIL_->__parent_ = __head_;
__head_->__right_ = __NIL_;
++__size_;
return (iterator(__head_));
}
node *parent = NULL;
node* __holder;
iterator __p = end();
if (__compar_(__get_key(__v), *__hint))
{
iterator pre(__hint);
if ((__hint == begin() || __compar_(*--pre, __get_key(__v))) && __hint.base()->__left_ == NULL)
parent = __hint.base();
else
__p = find(__get_key(__v), &parent);
}
else if (__compar_(*__hint, __get_key(__v)))
{
iterator next(__hint);
++next;
if ((next == end() || __compar_(__get_key(__v), *next)) && check_right(__hint.base()))
parent = __hint.base();
else
__p = find(__get_key(__v), &parent);
}
else
return (iterator(__hint));
if (__p != end())
return end();
__holder = get_node(RED, __v);
__holder->__parent_ = parent;
if (__compar_(__get_key(__v), *(parent->__value_)))
{
parent->__left_ = __holder;
if (__begin_ == parent)
__begin_ = __holder;
}
else
{
parent->__right_ = __holder;
if (__NIL_->__parent_ == parent)
{
__NIL_->__parent_ = __holder;
__holder->__right_ = __NIL_;
}
}
balance(__holder);
++__size_;
return iterator(__holder);
}
void erase(iterator __pos)
{
node* __p = __pos.base();
while(__p->__left_ != NULL || !check_right(__p))
{
node* __old = __p;
if (__p->__left_ != NULL)
__p = max_child(__p->__left_);
else
__p = min_child(__p->__right_);
swap_nodes(__old, __p);
__p = __old;
}
node *to_delet = __p;
if (__p->__color_ == RED || __p == __head_)
destroy_node(__p);
else
{
while (__p != __head_)
{
if (__p == __p->__parent_->__left_)
{
node *__sib = __p->__parent_->__right_;
if (check_right(__p->__parent_) || __sib->__color_ == BLACK)
{
if (!check_right(__p->__parent_) && !check_right(__sib) && __sib->__right_->__color_ == RED)
{
ft::swap(__sib->__color_, __p->__parent_->__color_);
left_rotate(__p->__parent_);
__sib->__right_->__color_ = BLACK ;
if (__p == to_delet)
destroy_node(to_delet);
break ;
}
else if (!check_right(__p->__parent_) && __sib->__left_ != NULL && __sib->__left_->__color_ == RED)
{
__sib->__left_->__color_ = BLACK;
__sib->__color_ = RED;
right_rotate(__sib);
}
else
{
if (!check_right(__p->__parent_))
__sib->__color_ = RED;
if (__p == to_delet)
{
__p = __p->__parent_;
destroy_node(to_delet);
}
else
__p = __p->__parent_;
if (__p->__color_ == RED)
{
__p->__color_ = BLACK;
break ;
}
}
}
else
{
__p->__parent_->__color_ = RED;
if (!check_right(__p->__parent_))
__sib->__color_ = BLACK;
left_rotate(__p->__parent_);
}
}
else
{
node *__sib = __p->__parent_->__left_;
if (__sib == NULL || __sib->__color_ == BLACK)
{
if (__sib != NULL && __sib->__left_ != NULL && __sib->__left_->__color_ == RED)
{
ft::swap(__sib->__color_, __p->__parent_->__color_);
right_rotate(__p->__parent_);
__sib->__left_->__color_ = BLACK;
if (__p == to_delet)
destroy_node(to_delet);
}
else if (__sib != NULL && !check_right(__sib) && __sib->__right_->__color_ == RED)
{
__sib->__right_->__color_ = BLACK;
__sib->__color_ = RED;
left_rotate(__sib);
}
else
{
if (__sib != NULL)
__sib->__color_ = RED;
if (__p == to_delet)
{
__p = __p->__parent_;
destroy_node(to_delet);
}
else
__p = __p->__parent_;
if (__p->__color_ == RED)
{
__p->__color_ = BLACK;
break ;
}
}
}
else
{
__p->__parent_->__color_ = RED;
if (__sib != NULL)
__sib->__color_ = BLACK;
right_rotate(__p->__parent_);
}
}
}
}
--__size_;
}
private:
node* get_node(bool __c, const value_type val = value_type(), node* p = NULL, node* l = NULL, node* r = NULL)
{
node* __node;
__node = node_alloc.allocate(1);
node_alloc.construct(__node, node(__c, p, l, r));
__node->__value_ = value_alloc.allocate(1);
value_alloc.construct(__node->__value_, val);
return (__node);
}
void destroy(node* __p)
{
if (__p != NULL && __p != __NIL_)
{
destroy(__p->__left_);
destroy(__p->__right_);
value_alloc.destroy(__p->__value_);
value_alloc.deallocate(__p->__value_, 1);
node_alloc.destroy(__p);
node_alloc.deallocate(__p, 1);
}
}
void destroy_node(node *__p)
{
if (__begin_ == __p)
__begin_ = __p->__parent_ ? __p->__parent_ : __NIL_;
if (__NIL_->__parent_ == __p)
__NIL_->__parent_ = __p->__parent_;
if (__head_ == __p)
__head_ = __NIL_;
else
{
if (__p == __p->__parent_->__right_)
__p->__parent_->__right_ = __p->__right_;
else
__p->__parent_->__left_ = __p->__left_;
}
value_alloc.destroy(__p->__value_);
value_alloc.deallocate(__p->__value_, 1);
node_alloc.destroy(__p);
node_alloc.deallocate(__p, 1);
}
void left_rotate(node* __x)
{
node* __y = __x->__right_;
__x->__right_ = __y->__left_;
if (__x->__right_ != NULL)
__x->__right_->__parent_ = __x;
__y->__parent_ = __x->__parent_;
if (__x->__parent_ == NULL)
__head_ = __y;
else if (__x->__parent_->__left_ == __x)
__x->__parent_->__left_ = __y;
else
__x->__parent_->__right_ = __y;
__y->__left_ = __x;
__x->__parent_ = __y;
}
void right_rotate(node* __x)
{
node* __y = __x->__left_;
__x->__left_ = __y->__right_;
if (__x->__left_ != NULL)
__x->__left_->__parent_ = __x;
__y->__parent_ = __x->__parent_;
if (__x->__parent_ == NULL)
__head_ = __y;
else if (__x->__parent_->__right_ == __x)
__x->__parent_->__right_ = __y;
else
__x->__parent_->__left_ = __y;
__y->__right_ = __x;
__x->__parent_ = __y;
}
void balance(node* __x)
{
while (__x != __head_ && __x->__parent_->__color_ == RED)
{
if (__x->__parent_ == __x->__parent_->__parent_->__left_)
{
node *__p = __x->__parent_->__parent_->__right_;
if (__p != NULL && __p->__color_ == RED)
{
__x = __x->__parent_;
__x->__color_ = BLACK;
__x = __p->__parent_;
if (__x != __head_)
__x->__color_ = RED;
__p->__color_ = BLACK;
}
else
{
if (__x == __x->__parent_->__right_)
{
__x = __x->__parent_;
left_rotate(__x);
}
__x = __x->__parent_;
__x->__color_ = BLACK;
__x = __x->__parent_;
__x->__color_ = RED;
right_rotate(__x);
break ;
}
}
else
{
node *__p = __x->__parent_->__parent_->__left_;
if (__p != NULL && __p->__color_ == RED)
{
__x = __x->__parent_;
__x->__color_ = BLACK;
__x = __p->__parent_;
if (__x != __head_)
__x->__color_ = RED;
__p->__color_ = BLACK;
}
else
{
if (__x == __x->__parent_->__left_)
{
__x = __x->__parent_;
right_rotate(__x);
}
__x = __x->__parent_;
__x->__color_ = BLACK;
__x = __x->__parent_;
__x->__color_ = RED;
left_rotate(__x);
break ;
}
}
}
}
bool check_right(node *__p)
{
return (__p->__right_ == NULL || __p->__right_ == __NIL_);
}
node* min_child(node *__p)
{
while (__p->__left_ != NULL)
__p = __p->__left_;
return (__p);
}
node* max_child(node *__p)
{
while (!check_right(__p))
__p = __p->__right_;
return (__p);
}
void swap_nodes(node* __x, node* __y)
{
if (__x == __head_)
__head_ = __y;
else if (__x->__parent_->__right_ == __x)
__x->__parent_->__right_ = __y;
else
__x->__parent_->__left_ = __y;
if (__begin_ == __x)
__begin_ = __y;
else if (__begin_ == __y)
__begin_ = __x;
if (__y->__right_ != NULL)
__y->__right_->__parent_ = __x;
if (__y->__left_ != NULL)
__y->__left_->__parent_ = __x;
if (__x->__right_ != __y && __x->__right_ != NULL)
__x->__right_->__parent_ = __y;
if (__x->__left_ != __y && __x->__left_ != NULL)
__x->__left_->__parent_ = __y;
if (__y->__parent_->__right_ == __y && __y->__parent_ != __x)
__y->__parent_->__right_ = __x;
else if (__y->__parent_->__left_ == __y && __y->__parent_ != __x)
__y->__parent_->__left_ = __x;
ft::swap(__x->__parent_, __y->__parent_);
ft::swap(__x->__right_, __y->__right_);
ft::swap(__x->__left_, __y->__left_);
ft::swap(__x->__color_, __y->__color_);
if (__x->__parent_ == __x)
__x->__parent_ = __y;
if (__y->__right_ == __y)
__y->__right_ = __x;
if (__y->__left_ == __y)
__y->__left_ = __x;
}
template<class T> const T __get_key(const T& __x) const { return __x; }
template<class T, class U> const T __get_key(const ft::pair<T, U>& __x) const { return __x.first; }
};
} // ft
# endif