CPPX 3.2.1
A Modern C++ Utility Library
Loading...
Searching...
No Matches
rbt.tpp
Go to the documentation of this file.
1#pragma once
2#include "cppx/cppx.h"
3
4#include <stack>
5#include <stdexcept>
6
7namespace stl_ext
8{
9
10template <typename T> bool RBTree<T>::is_red(const Node<T> *node)
11{
12 return node != nullptr && node->m_color == Color::RED;
13}
14
15template <typename T> bool RBTree<T>::is_black(const Node<T> *node)
16{
17 return node == nullptr || node->m_color == Color::BLACK;
18}
19
20template <typename T> Color RBTree<T>::get_color(const Node<T> *node)
21{
22 return (node == nullptr) ? Color::BLACK : node->m_color;
23}
24
25template <typename T> Color RBTree<T>::node_color(const Node<T> *node) const
26{
27 return (node == nullptr) ? Color::BLACK : node->m_color;
28}
29
30template <typename T> Node<T> *RBTree<T>::tree_minimum(Node<T> *node) const
31{
32 while (node->p_left)
33 node = node->p_left;
34 return node;
35}
36
37template <typename T> void RBTree<T>::rotate_left(Node<T> *x)
38{
39 Node<T> *y = x->p_right;
40 Node<T> *p = x->p_parent;
41
42 x->p_right = y->p_left;
43 if (y->p_left)
44 y->p_left->p_parent = x;
45
46 y->p_left = x;
47 x->p_parent = y;
48
49 y->p_parent = p;
50 if (!p)
51 this->p_head = y;
52 else if (p->p_left == x)
53 p->p_left = y;
54 else
55 p->p_right = y;
56}
57
58template <typename T> void RBTree<T>::rotate_right(Node<T> *x)
59{
60 Node<T> *y = x->p_left;
61 Node<T> *p = x->p_parent;
62
63 x->p_left = y->p_right;
64 if (y->p_right)
65 y->p_right->p_parent = x;
66
67 y->p_right = x;
68 x->p_parent = y;
69
70 y->p_parent = p;
71 if (!p)
72 this->p_head = y;
73 else if (p->p_left == x)
74 p->p_left = y;
75 else
76 p->p_right = y;
77}
78
79template <typename T> void RBTree<T>::insert(const T &val)
80{
81 Node<T> *z = this->m_pool.allocate(val);
82 z->m_color = Color::RED;
83 z->p_left = nullptr;
84 z->p_right = nullptr;
85
86 if (!this->p_head)
87 {
88 z->m_color = Color::BLACK;
89 z->p_parent = nullptr;
90 this->p_head = z;
91 return;
92 }
93
94 Node<T> *curr = this->p_head;
95 Node<T> *parent = nullptr;
96
97 while (curr)
98 {
99 parent = curr;
100 if (val < curr->m_data)
101 curr = curr->p_left;
102 else if (val > curr->m_data)
103 curr = curr->p_right;
104 else
105 {
106 this->m_pool.deallocate(z);
107 return;
108 }
109 }
110
111 z->p_parent = parent;
112 if (val < parent->m_data)
113 parent->p_left = z;
114 else
115 parent->p_right = z;
116
117 fix_insert_violation(z);
118}
119
120template <typename T> void RBTree<T>::fix_insert_violation(Node<T> *z)
121{
122 while (z->p_parent && z->p_parent->m_color == Color::RED)
123 {
124 Node<T> *parent = z->p_parent;
125 Node<T> *grandparent = parent->p_parent;
126
127 if (!grandparent)
128 break;
129
130 if (parent == grandparent->p_left)
131 {
132 Node<T> *uncle = grandparent->p_right;
133
134 if (node_color(uncle) == Color::RED)
135 {
136 parent->m_color = Color::BLACK;
137 uncle->m_color = Color::BLACK;
138 grandparent->m_color = Color::RED;
139 z = grandparent;
140 }
141 else
142 {
143 if (z == parent->p_right)
144 {
145 z = parent;
146 rotate_left(z);
147 parent = z->p_parent;
148 grandparent = parent->p_parent;
149 }
150
151 parent->m_color = Color::BLACK;
152 grandparent->m_color = Color::RED;
153 rotate_right(grandparent);
154 }
155 }
156 else
157 {
158 Node<T> *uncle = grandparent->p_left;
159
160 if (node_color(uncle) == Color::RED)
161 {
162 parent->m_color = Color::BLACK;
163 uncle->m_color = Color::BLACK;
164 grandparent->m_color = Color::RED;
165 z = grandparent;
166 }
167 else
168 {
169 if (z == parent->p_left)
170 {
171 z = parent;
172 rotate_right(z);
173 parent = z->p_parent;
174 grandparent = parent->p_parent;
175 }
176
177 parent->m_color = Color::BLACK;
178 grandparent->m_color = Color::RED;
179 rotate_left(grandparent);
180 }
181 }
182 }
183
184 this->p_head->m_color = Color::BLACK;
185}
186
187template <typename T> void RBTree<T>::remove(const T &val)
188{
189 Node<T> *z = this->p_head;
190 while (z)
191 {
192 if (val < z->m_data)
193 z = z->p_left;
194 else if (val > z->m_data)
195 z = z->p_right;
196 else
197 break;
198 }
199 if (!z)
200 return;
201
202 Node<T> *y = z;
203 Color y_original_color = y->m_color;
204 Node<T> *x = nullptr;
205 Node<T> *x_parent = nullptr;
206
207 if (!z->p_left)
208 {
209 x = z->p_right;
210 x_parent = z->p_parent;
211
212 if (x)
213 x->p_parent = z->p_parent;
214
215 if (!z->p_parent)
216 this->p_head = x;
217 else if (z->p_parent->p_left == z)
218 z->p_parent->p_left = x;
219 else
220 z->p_parent->p_right = x;
221
222 this->m_pool.deallocate(z);
223 }
224 else if (!z->p_right)
225 {
226 x = z->p_left;
227 x_parent = z->p_parent;
228
229 if (x)
230 x->p_parent = z->p_parent;
231
232 if (!z->p_parent)
233 this->p_head = x;
234 else if (z->p_parent->p_left == z)
235 z->p_parent->p_left = x;
236 else
237 z->p_parent->p_right = x;
238
239 this->m_pool.deallocate(z);
240 }
241 else
242 {
243 y = tree_minimum(z->p_right);
244 y_original_color = y->m_color;
245 x = y->p_right;
246
247 if (y->p_parent == z)
248 {
249 x_parent = y;
250
251 if (x)
252 x->p_parent = y;
253
254 y->p_left = z->p_left;
255 if (z->p_left)
256 z->p_left->p_parent = y;
257
258 if (!z->p_parent)
259 this->p_head = y;
260 else if (z->p_parent->p_left == z)
261 z->p_parent->p_left = y;
262 else
263 z->p_parent->p_right = y;
264
265 y->p_parent = z->p_parent;
266 y->m_color = z->m_color;
267 this->m_pool.deallocate(z);
268 }
269 else
270 {
271 x_parent = y->p_parent;
272
273 if (x)
274 x->p_parent = y->p_parent;
275
276 if (y->p_parent->p_left == y)
277 y->p_parent->p_left = x;
278 else
279 y->p_parent->p_right = x;
280
281 y->p_left = z->p_left;
282 if (z->p_left)
283 z->p_left->p_parent = y;
284
285 y->p_right = z->p_right;
286 if (z->p_right)
287 z->p_right->p_parent = y;
288
289 if (!z->p_parent)
290 this->p_head = y;
291 else if (z->p_parent->p_left == z)
292 z->p_parent->p_left = y;
293 else
294 z->p_parent->p_right = y;
295
296 y->p_parent = z->p_parent;
297 y->m_color = z->m_color;
298 this->m_pool.deallocate(z);
299 }
300 }
301
303 {
304 fix_delete_violation(x, x_parent);
305 }
306}
307
308template <typename T> void RBTree<T>::fix_delete_violation(Node<T> *x, Node<T> *x_parent)
309{
310 while (x != this->p_head && node_color(x) == Color::BLACK)
311 {
312 if (x_parent == nullptr)
313 break;
314
315 if (x == x_parent->p_left)
316 {
317 Node<T> *w = x_parent->p_right;
318
319 if (!w)
320 break;
321
322 if (node_color(w) == Color::RED)
323 {
324 w->m_color = Color::BLACK;
325 x_parent->m_color = Color::RED;
326 rotate_left(x_parent);
327 w = x_parent->p_right;
328 if (!w)
329 break;
330 }
331
332 if (node_color(w->p_left) == Color::BLACK && node_color(w->p_right) == Color::BLACK)
333 {
334 w->m_color = Color::RED;
335 x = x_parent;
336 x_parent = x->p_parent;
337 }
338 else
339 {
340 if (node_color(w->p_right) == Color::BLACK)
341 {
342 if (w->p_left)
343 w->p_left->m_color = Color::BLACK;
344 w->m_color = Color::RED;
345 rotate_right(w);
346 w = x_parent->p_right;
347 }
348
349 w->m_color = x_parent->m_color;
350 x_parent->m_color = Color::BLACK;
351 if (w->p_right)
352 w->p_right->m_color = Color::BLACK;
353 rotate_left(x_parent);
354 x = this->p_head;
355 }
356 }
357 else
358 {
359 Node<T> *w = x_parent->p_left;
360
361 if (!w)
362 break;
363
364 if (node_color(w) == Color::RED)
365 {
366 w->m_color = Color::BLACK;
367 x_parent->m_color = Color::RED;
368 rotate_right(x_parent);
369 w = x_parent->p_left;
370 if (!w)
371 break;
372 }
373
374 if (node_color(w->p_left) == Color::BLACK && node_color(w->p_right) == Color::BLACK)
375 {
376 w->m_color = Color::RED;
377 x = x_parent;
378 x_parent = x->p_parent;
379 }
380 else
381 {
382 if (node_color(w->p_left) == Color::BLACK)
383 {
384 if (w->p_right)
385 w->p_right->m_color = Color::BLACK;
386 w->m_color = Color::RED;
387 rotate_left(w);
388 w = x_parent->p_left;
389 }
390
391 w->m_color = x_parent->m_color;
392 x_parent->m_color = Color::BLACK;
393 if (w->p_left)
394 w->p_left->m_color = Color::BLACK;
395 rotate_right(x_parent);
396 x = this->p_head;
397 }
398 }
399 }
400
401 if (x)
402 x->m_color = Color::BLACK;
403}
404
405template <typename T> int RBTree<T>::compute_black_height(const Node<T> *node) const
406{
407 if (!node)
408 return 1;
409
410 int left_bh = compute_black_height(node->p_left);
411 int right_bh = compute_black_height(node->p_right);
412
413 if (left_bh == -1 || right_bh == -1 || left_bh != right_bh)
414 return -1;
415
416 return left_bh + (node->m_color == Color::BLACK ? 1 : 0);
417}
418
419template <typename T> int RBTree<T>::get_black_height() const
420{
421 return compute_black_height(this->p_head);
422}
423
424template <typename T> bool RBTree<T>::validate_rb_helper(const Node<T> *node) const
425{
426 if (!node)
427 return true;
428
429 if (node->m_color == Color::RED)
430 {
431 if (node_color(node->p_left) == Color::RED || node_color(node->p_right) == Color::RED)
432 {
433 return false;
434 }
435 }
436
437 if (node->p_left && node->p_left->p_parent != node)
438 return false;
439 if (node->p_right && node->p_right->p_parent != node)
440 return false;
441
442 return validate_rb_helper(node->p_left) && validate_rb_helper(node->p_right);
443}
444
445template <typename T> bool RBTree<T>::validate_rb_properties() const
446{
447 if (!this->p_head)
448 return true;
449
450 if (this->p_head->m_color != Color::BLACK)
451 return false;
452
453 if (!validate_rb_helper(this->p_head))
454 return false;
455
456 if (compute_black_height(this->p_head) == -1)
457 return false;
458
459 if (this->p_head->p_parent != nullptr)
460 return false;
461
462 return true;
463}
464
465template <typename T> std::vector<T> RBTree<T>::to_sorted_vector() const
466{
467 std::vector<T> result;
468 std::stack<const Node<T> *> s;
469 const Node<T> *curr = this->p_head;
470
471 while (curr || !s.empty())
472 {
473 while (curr)
474 {
475 s.push(curr);
476 curr = curr->p_left;
477 }
478 curr = s.top();
479 s.pop();
480 result.push_back(curr->m_data);
481 curr = curr->p_right;
482 }
483
484 return result;
485}
486
487} // namespace stl_ext
static bool is_black(const Node< T > *node)
Definition rbt.tpp:15
static bool is_red(const Node< T > *node)
Definition rbt.tpp:10
bool validate_rb_properties() const
Definition rbt.tpp:445
static Color get_color(const Node< T > *node)
Definition rbt.tpp:20
std::vector< T > to_sorted_vector() const
Definition rbt.tpp:465
void remove(const T &val) override
Definition rbt.tpp:187
int get_black_height() const
Definition rbt.tpp:419
void insert(const T &val) override
Definition rbt.tpp:79
Definition cppx.h:11
Color
Definition cppx.h:14