Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_tdRbTree.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
52# ifndef TPL_TDRBTREE_H
53# define TPL_TDRBTREE_H
54
55# include <functional>
56# include <tpl_binNode.H>
57# include <tpl_binNodeUtils.H>
58# include <tpl_arrayStack.H>
59# include <rbNode.H>
60# include <ah-concepts.H>
61
62namespace Aleph
63{
64
95template <template <class> class NodeType, typename Key,
96 class Compare = Aleph::less<Key>>
97 requires StrictWeakOrder<Compare, Key>
99{
100public:
101 using Node = NodeType<Key>;
102 using key_type = Key;
103 using compare_type = Compare;
104
105private:
111 Compare cmp;
112 size_t num_nodes;
113
116 bool less(const Key& a, const Key& b) const
117 noexcept(nothrow_compare_v<Compare, Key>)
118 {
119 return cmp(a, b);
120 }
121
124 bool equals(const Key& a, const Key& b) const
125 noexcept(nothrow_compare_v<Compare, Key>)
126 {
127 return not static_cast<bool>(cmp(a, b)) and not static_cast<bool>(cmp(b, a));
128 }
129
131 static Node* getSibling(Node *p, Node *fp) noexcept
132 {
133 assert(LLINK(fp) == p or RLINK(fp) == p);
134 return LLINK(fp) == p ? RLINK(fp) : LLINK(fp);
135 }
136
138 void restoreRedCondition(Node *p, Node *&fp, Node *ffp, Node *fffp) noexcept
139 {
140 assert(LLINK(fp) == p or RLINK(fp) == p);
141 assert(COLOR(fp) == RED);
142 assert(COLOR(p) == RED);
143
144 if (fp == root)
145 {
146 COLOR(fp) = BLACK;
147 return;
148 }
149
150 assert(LLINK(ffp) == fp or RLINK(ffp) == fp);
151 assert(COLOR(ffp) == BLACK);
152 assert(LLINK(fffp) == ffp or RLINK(fffp) == ffp);
153
154 COLOR(ffp) = RED;
155
156 if (LLINK(fp) == p and LLINK(ffp) == fp)
157 {
158 COLOR(fp) = BLACK;
159 rotate_to_right(ffp, fffp);
160 }
161 else if (RLINK(fp) == p and RLINK(ffp) == fp)
162 {
163 COLOR(fp) = BLACK;
164 rotate_to_left(ffp, fffp);
165 }
166 else
167 {
168 COLOR(p) = BLACK;
169 if (RLINK(fp) == p)
170 {
171 rotate_to_left(fp, ffp);
172 rotate_to_right(ffp, fffp);
173 }
174 else
175 {
176 rotate_to_right(fp, ffp);
177 rotate_to_left(ffp, fffp);
178 }
179 fp = fffp;
180 }
181 }
182
184 static void flipColors(Node* p) noexcept
185 {
186 assert(p != Node::NullPtr);
187 assert(COLOR(p) == BLACK);
188 assert(COLOR(LLINK(p)) == RED and COLOR(RLINK(p)) == RED);
189
190 COLOR(p) = RED;
191 COLOR(LLINK(p)) = COLOR(RLINK(p)) = BLACK;
192 }
193
199 noexcept(nothrow_compare_v<Compare, Key>)
200 {
201 assert(q != Node::NullPtr);
202 assert(root != Node::NullPtr);
203 assert(COLOR(q) == RED);
204 assert(LLINK(q) == Node::NullPtr and RLINK(q) == Node::NullPtr);
205
206 Node *p = root; // Current node
207 Node *fp = head; // p's parent
208 Node *ffp = fHead; // p's grandparent
209 Node *fffp = Node::NullPtr; // p's great-grandparent
210 Node *nextNode;
211
212 while (true)
213 {
214 if (equals(KEY(q), KEY(p)))
215 return nullptr; // Duplicated key, insertion not possible
216
217 // Proactive color flip: if p is black with two red children, flip
218 if (COLOR(p) == BLACK and COLOR(LLINK(p)) == RED
219 and COLOR(RLINK(p)) == RED)
220 {
221 flipColors(p);
222 if (COLOR(fp) == RED) // May violate red condition
223 {
224 assert(fffp != Node::NullPtr);
225 restoreRedCondition(p, fp, ffp, fffp);
226 }
227 }
228
229 // Descend to appropriate child
230 if (less(KEY(q), KEY(p)))
231 {
232 if (LLINK(p) == Node::NullPtr)
233 break;
234 nextNode = LLINK(p);
235 }
236 else
237 {
238 if (RLINK(p) == Node::NullPtr)
239 break;
240 nextNode = RLINK(p);
241 }
242
243 // Update ancestor chain
244 fffp = ffp;
245 ffp = fp;
246 fp = p;
247 p = nextNode;
248 }
249
250 // Perform insertion
251 if (less(KEY(q), KEY(p)))
252 LLINK(p) = q;
253 else
254 RLINK(p) = q;
255
256 ++num_nodes; // after the comparison, which may throw
257
258 if (COLOR(p) == RED) // May violate red condition
259 restoreRedCondition(q, p, fp, ffp);
260
261 return q;
262 }
263
268 noexcept(nothrow_compare_v<Compare, Key>)
269 {
270 assert(q != Node::NullPtr);
271 assert(root != Node::NullPtr);
272 assert(COLOR(q) == RED);
273 assert(LLINK(q) == Node::NullPtr and RLINK(q) == Node::NullPtr);
274
275 Node *p = root;
276 Node *fp = head;
277 Node *ffp = fHead;
278 Node *fffp = Node::NullPtr;
279 Node *nextNode;
280
281 while (true)
282 {
283 // Proactive color flip
284 if (COLOR(p) == BLACK and COLOR(LLINK(p)) == RED
285 and COLOR(RLINK(p)) == RED)
286 {
287 flipColors(p);
288 if (COLOR(fp) == RED)
289 {
290 assert(fffp != Node::NullPtr);
291 restoreRedCondition(p, fp, ffp, fffp);
292 }
293 }
294
295 // Descend (duplicates go right)
296 if (less(KEY(q), KEY(p)))
297 {
298 if (LLINK(p) == Node::NullPtr)
299 break;
300 nextNode = LLINK(p);
301 }
302 else // >= goes right
303 {
304 if (RLINK(p) == Node::NullPtr)
305 break;
306 nextNode = RLINK(p);
307 }
308
309 fffp = ffp;
310 ffp = fp;
311 fp = p;
312 p = nextNode;
313 }
314
315 if (less(KEY(q), KEY(p)))
316 LLINK(p) = q;
317 else
318 RLINK(p) = q;
319
320 ++num_nodes; // after the comparison, which may throw
321
322 if (COLOR(p) == RED)
323 restoreRedCondition(q, p, fp, ffp);
324
325 return q;
326 }
327
329 static Node* gotoLeftAndColorRed(Node *fp, Node *&ffp) noexcept
330 {
331 assert(fp != Node::NullPtr);
332 assert(ffp != Node::NullPtr);
333 assert(LLINK(ffp) == fp or RLINK(ffp) == fp);
334 assert(LLINK(fp) != Node::NullPtr);
335
336 Node *p = LLINK(fp);
337
338 if (COLOR(p) == RED)
339 return p;
340
341 Node *sp = RLINK(fp);
342
343 // fp black means we came from another black node
344 if (COLOR(fp) == BLACK)
345 {
346 assert(COLOR(sp) == RED);
347 rotate_to_left(fp, ffp);
348 COLOR(fp) = RED;
349 COLOR(sp) = BLACK;
350 ffp = sp;
351 sp = RLINK(fp);
352 }
353
354 if (COLOR(LLINK(p)) == BLACK and COLOR(RLINK(p)) == BLACK)
355 {
356 assert(COLOR(LLINK(fp)) == BLACK);
357 assert(COLOR(RLINK(fp)) == BLACK);
358 assert(COLOR(fp) == RED);
359
360 COLOR(p) = RED;
361 COLOR(fp) = BLACK;
362
363 Node *np = RLINK(sp); // Nephew of p
364 Node *snp = LLINK(sp); // Sibling-nephew of p
365
366 if (COLOR(snp) == BLACK and COLOR(np) == BLACK)
367 {
368 COLOR(sp) = RED;
369 return p;
370 }
371
372 if (COLOR(np) == RED)
373 {
374 ffp = rotate_to_left(fp, ffp);
375 COLOR(sp) = RED;
376 COLOR(np) = BLACK;
377 assert(ffp == sp);
378 return p;
379 }
380
381 assert(COLOR(snp) == RED);
382
383 rotate_to_right(sp, fp);
384 ffp = rotate_to_left(fp, ffp);
385 assert(ffp == snp);
386 }
387 return p;
388 }
389
391 static Node* gotoRightAndColorRed(Node *fp, Node *&ffp) noexcept
392 {
393 assert(fp != Node::NullPtr);
394 assert(ffp != Node::NullPtr);
395 assert(LLINK(ffp) == fp or RLINK(ffp) == fp);
396 assert(RLINK(fp) != Node::NullPtr);
397
398 Node *p = RLINK(fp);
399
400 if (COLOR(p) == RED)
401 return p;
402
403 Node *sp = LLINK(fp);
404
405 if (COLOR(fp) == BLACK)
406 {
407 assert(COLOR(sp) == RED);
408 rotate_to_right(fp, ffp);
409 COLOR(fp) = RED;
410 COLOR(sp) = BLACK;
411 ffp = sp;
412 sp = LLINK(fp);
413 }
414
415 if (COLOR(LLINK(p)) == BLACK and COLOR(RLINK(p)) == BLACK)
416 {
417 assert(COLOR(RLINK(fp)) == BLACK);
418 assert(COLOR(LLINK(fp)) == BLACK);
419 assert(COLOR(fp) == RED);
420
421 COLOR(p) = RED;
422 COLOR(fp) = BLACK;
423
424 Node *np = LLINK(sp); // Nephew of p
425 Node *snp = RLINK(sp); // Sibling-nephew of p
426
427 if (COLOR(snp) == BLACK and COLOR(np) == BLACK)
428 {
429 COLOR(sp) = RED;
430 return p;
431 }
432
433 if (COLOR(np) == RED)
434 {
435 ffp = rotate_to_right(fp, ffp);
436 COLOR(sp) = RED;
437 COLOR(np) = BLACK;
438 assert(ffp == sp);
439 return p;
440 }
441
442 assert(COLOR(snp) == RED);
443
444 rotate_to_left(sp, fp);
445 ffp = rotate_to_right(fp, ffp);
446 assert(ffp == snp);
447 }
448 return p;
449 }
450
452 static void findSuccAndSwap(Node *p, Node *&fp) noexcept
453 {
454 assert(p != Node::NullPtr);
455 assert(RLINK(p) != Node::NullPtr);
456 assert(fp != Node::NullPtr);
457 assert(LLINK(fp) == p or RLINK(fp) == p);
458
459 Node *fSucc = p;
460 Node *succ = gotoRightAndColorRed(p, fp);
461 Node *ffSucc = fp;
462
463 while (LLINK(succ) != Node::NullPtr)
464 {
465 ffSucc = fSucc;
466 fSucc = succ;
467 succ = gotoLeftAndColorRed(fSucc, ffSucc);
468 }
469
470 // Update parent of p to point to succ
471 if (LLINK(fp) == p)
472 LLINK(fp) = succ;
473 else
474 RLINK(fp) = succ;
475
476 // Swap left branches
477 LLINK(succ) = LLINK(p);
478 LLINK(p) = Node::NullPtr;
479
480 // Swap right branches (two cases)
481 if (RLINK(p) == succ)
482 {
483 RLINK(p) = RLINK(succ);
484 RLINK(succ) = p;
485 fSucc = fp;
486 fp = succ;
487 }
488 else
489 {
490 Node *tmp = fp;
491 Node *succr = RLINK(succ);
492
493 RLINK(succ) = RLINK(p);
494 LLINK(fSucc) = p;
495 RLINK(p) = succr;
496 fp = fSucc;
497 fSucc = tmp;
498 }
499
500 // Swap colors
501 Color tmp = COLOR(succ);
502 COLOR(succ) = COLOR(p);
503 COLOR(p) = tmp;
504 }
505
507 static void findPredAndSwap(Node *p, Node *&fp) noexcept
508 {
509 assert(p != Node::NullPtr);
510 assert(LLINK(p) != Node::NullPtr);
511 assert(fp != Node::NullPtr);
512 assert(LLINK(fp) == p or RLINK(fp) == p);
513
514 Node *fPred = p;
515 Node *pred = gotoLeftAndColorRed(p, fp);
516 Node *ffPred = fp;
517
518 while (RLINK(pred) != Node::NullPtr)
519 {
520 ffPred = fPred;
521 fPred = pred;
522 pred = gotoRightAndColorRed(fPred, ffPred);
523 }
524
525 // Update parent of p to point to pred
526 if (LLINK(fp) == p)
527 LLINK(fp) = pred;
528 else
529 RLINK(fp) = pred;
530
531 // Swap right branches
532 RLINK(pred) = RLINK(p);
533 RLINK(p) = Node::NullPtr;
534
535 // Swap left branches (two cases)
536 if (LLINK(p) == pred)
537 {
538 LLINK(p) = LLINK(pred);
539 LLINK(pred) = p;
540 fPred = fp;
541 fp = pred;
542 }
543 else
544 {
545 Node *tmp = fp;
546 Node *predl = LLINK(pred);
547
548 LLINK(pred) = LLINK(p);
549 RLINK(fPred) = p;
550 LLINK(p) = predl;
551 fp = fPred;
552 fPred = tmp;
553 }
554
555 // Swap colors
556 Color tmp = COLOR(pred);
557 COLOR(pred) = COLOR(p);
558 COLOR(p) = tmp;
559 }
560
562 void colorRootAsRed() noexcept
563 {
564 if (COLOR(root) == RED)
565 return;
566
567 if (COLOR(LLINK(root)) == BLACK and COLOR(RLINK(root)) == BLACK)
568 COLOR(root) = RED;
569 }
570
574 Node* searchAndColorRed(const Key& key, Node *&fp)
575 noexcept(nothrow_compare_v<Compare, Key>)
576 {
577 Node *p = root;
578 fp = head;
579 Node *ffp = fHead;
580
582
583 while (true)
584 {
585 if (equals(key, KEY(p)))
586 return p;
587
588 ffp = fp;
589 fp = p;
590
591 if (less(key, KEY(p)))
592 {
593 if (LLINK(p) == Node::NullPtr)
594 return p;
595 p = gotoLeftAndColorRed(fp, ffp);
596 }
597 else
598 {
599 if (RLINK(p) == Node::NullPtr)
600 return p;
601 p = gotoRightAndColorRed(fp, ffp);
602 }
603 }
604 }
605
607 static void removeAndRendLeafRed(Node *p, Node *fp) noexcept
608 {
609 assert(p != Node::NullPtr);
610 assert(fp != Node::NullPtr);
611 assert(LLINK(fp) == p or RLINK(fp) == p);
612
613 while (LLINK(p) != Node::NullPtr or RLINK(p) != Node::NullPtr)
614 {
615 if (RLINK(p) != Node::NullPtr)
616 findSuccAndSwap(p, fp);
617 else if (LLINK(p) != Node::NullPtr)
618 findPredAndSwap(p, fp);
619 }
620
621 if (LLINK(fp) == p)
622 LLINK(fp) = Node::NullPtr;
623 else
624 RLINK(fp) = Node::NullPtr;
625 }
626
628 void init() noexcept
629 {
630 RLINK(fHead) = head;
631 COLOR(Node::NullPtr) = BLACK;
632 COLOR(head) = BLACK;
633 num_nodes = 0;
634 }
635
636public:
644 noexcept(std::is_nothrow_default_constructible_v<Node> and
645 std::is_nothrow_default_constructible_v<Compare>)
646 : head(&headNode), fHead(&headParent), root(headNode.getR()), cmp()
647 {
648 init();
649 }
650
657 explicit GenTdRbTree(const Compare & __cmp)
658 noexcept(std::is_nothrow_default_constructible_v<Node> and
659 std::is_nothrow_copy_constructible_v<Compare>)
660 : head(&headNode), fHead(&headParent), root(headNode.getR()), cmp(__cmp)
661 {
662 init();
663 }
664
683 noexcept(bin_node_detail::nothrow_header_from_v<Node> and
684 bin_node_detail::nothrow_compare_taken_v<Compare>)
685 : headNode(bin_node_detail::header_from(other.headNode)),
686 headParent(bin_node_detail::header_from(other.headParent)),
688 cmp(bin_node_detail::compare_to_take(other.cmp)), num_nodes(other.num_nodes)
689 {
690 RLINK(fHead) = head;
691 COLOR(Node::NullPtr) = BLACK;
692 COLOR(head) = BLACK;
693 root = other.root;
694 other.root = Node::NullPtr;
695 other.num_nodes = 0;
696 }
697
706 noexcept(std::is_nothrow_move_assignable_v<Compare>)
707 {
708 if (this != &other)
709 {
710 // The comparison first: if its move throws, no tree has changed
711 cmp = std::move(other.cmp);
712 root = other.root;
713 num_nodes = other.num_nodes;
714 other.root = Node::NullPtr;
715 other.num_nodes = 0;
716 }
717 return *this;
718 }
719
721 GenTdRbTree(const GenTdRbTree &) = delete;
722
724 GenTdRbTree & operator=(const GenTdRbTree &) = delete;
725
727 void reset() noexcept
728 {
729 root = Node::NullPtr;
730 num_nodes = 0;
731 }
732
740 void swap(GenTdRbTree & other) noexcept(std::is_nothrow_swappable_v<Compare>)
741 {
742 // The comparison first: if its swap throws, the nodes stay where
743 // they were
744 std::swap(cmp, other.cmp);
745 std::swap(root, other.root);
746 std::swap(num_nodes, other.num_nodes);
747 }
748
749 virtual ~GenTdRbTree() = default;
750
752 size_t size() const noexcept { return num_nodes; }
753
755 bool is_empty() const noexcept { return root == Node::NullPtr; }
756
758 Compare & get_compare() noexcept { return cmp; }
759 const Compare & get_compare() const noexcept { return cmp; }
760
772 Node* insert(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
773 {
774 assert(p != Node::NullPtr);
775 assert(COLOR(p) == RED);
776 assert(LLINK(p) == Node::NullPtr and RLINK(p) == Node::NullPtr);
777
778 if (root == Node::NullPtr)
779 {
780 root = p;
781 ++num_nodes;
782 return p;
783 }
784
786 }
787
798 Node* insert_dup(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
799 {
800 assert(p != Node::NullPtr);
801 assert(COLOR(p) == RED);
802 assert(LLINK(p) == Node::NullPtr and RLINK(p) == Node::NullPtr);
803
804 if (root == Node::NullPtr)
805 {
806 root = p;
807 ++num_nodes;
808 return p;
809 }
810
812 }
813
824 Node* search_or_insert(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
825 {
826 assert(p != Node::NullPtr);
827 assert(COLOR(p) == RED);
828 assert(LLINK(p) == Node::NullPtr and RLINK(p) == Node::NullPtr);
829
830 if (root == Node::NullPtr)
831 {
832 root = p;
833 ++num_nodes;
834 return p;
835 }
836
837 // Search first
838 Node *found = search(KEY(p));
839 if (found != nullptr)
840 return found; // Return existing node
841
842 // Not found, insert
844 }
845
855 Node* search(const Key& key) const noexcept(nothrow_compare_v<Compare, Key>)
856 {
857 Node* p = root;
858 while (p != Node::NullPtr)
859 {
860 if (equals(key, KEY(p)))
861 return p;
862 p = less(key, KEY(p)) ? LLINK(p) : RLINK(p);
863 }
864 return nullptr;
865 }
866
878 Node* remove(const Key& key) noexcept(nothrow_compare_v<Compare, Key>)
879 {
880 if (root == Node::NullPtr)
881 return nullptr;
882
883 Node *fp;
884 Node *p = searchAndColorRed(key, fp);
885
886 if (not equals(KEY(p), key))
887 return nullptr;
888
890 --num_nodes;
891
892 return p;
893 }
894
896 Node*& getRoot() noexcept { return root; }
897
899 Node* getRoot() const noexcept { return root; }
900
901public:
910 bool verifyRedBlack() const noexcept
911 {
912 return is_red_black_tree(root);
913 }
914
920 bool verify() const
921 {
923 }
924
936};
937
943template <typename Key, class Compare = Aleph::less<Key>>
944 requires StrictWeakOrder<Compare, Key>
945class TdRbTree : public GenTdRbTree<RbNode, Key, Compare>
946{
948public:
949 using Base::Base;
950};
951
957template <typename Key, class Compare = Aleph::less<Key>>
959class TdRbTreeVtl : public GenTdRbTree<RbNodeVtl, Key, Compare>
960{
962public:
963 using Base::Base;
964};
965
966} // namespace Aleph
967
968# endif /* TPL_TDRBTREE_H */
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
@ KEY
Definition btreepic.C:169
Inorder iterator on the nodes of a binary tree.
Top-down red-black binary search tree implementation.
Node * searchFlipColorsAndInsert(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Search for insertion point, flipping colors proactively to maintain balance.
bool less(const Key &a, const Key &b) const noexcept(nothrow_compare_v< Compare, Key >)
Returns true if a < b according to comparator; it throws what the comparator throws.
void reset() noexcept
Reset tree to empty state (does not free nodes)
virtual ~GenTdRbTree()=default
void init() noexcept
Initialize header nodes.
Compare & get_compare() noexcept
Get reference to comparator.
bool is_empty() const noexcept
Check if tree is empty.
Node * searchFlipColorsAndInsertDup(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Search for insertion point allowing duplicates.
const Compare & get_compare() const noexcept
Node * fHead
Pointer to header's parent.
size_t size() const noexcept
Get number of nodes in tree (O(1))
Node *& root
Reference to root (right child of head)
static void flipColors(Node *p) noexcept
Flip colors: black parent with two red children becomes red with two black children.
bool equals(const Key &a, const Key &b) const noexcept(nothrow_compare_v< Compare, Key >)
Returns true if a == b (neither a < b nor b < a); it throws what the comparator throws.
GenTdRbTree(GenTdRbTree &&other) noexcept(bin_node_detail::nothrow_header_from_v< Node > and bin_node_detail::nothrow_compare_taken_v< Compare >)
Takes the nodes and the comparison criteria of other, which is left empty and can take nodes again.
static Node * gotoLeftAndColorRed(Node *fp, Node *&ffp) noexcept
Descend to left child, ensuring current node is red for deletion.
GenTdRbTree(const Compare &__cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_copy_constructible_v< Compare >)
Builds an empty tree with the comparison criteria __cmp.
Node headNode
Sentinel header node (parent of root)
void colorRootAsRed() noexcept
Color root red if safe (both children black)
void restoreRedCondition(Node *p, Node *&fp, Node *ffp, Node *fffp) noexcept
Restore red-black property after color flip creates two consecutive reds.
size_t num_nodes
Number of nodes in tree.
GenTdRbTree & operator=(const GenTdRbTree &)=delete
Deleted copy assignment.
GenTdRbTree & operator=(GenTdRbTree &&other) noexcept(std::is_nothrow_move_assignable_v< Compare >)
Takes the nodes and the comparison criteria of other, which is left empty.
static void findSuccAndSwap(Node *p, Node *&fp) noexcept
Find successor and swap with node p for deletion.
bool verify() const
Verify the red-black properties and the order of the keys.
static Node * gotoRightAndColorRed(Node *fp, Node *&ffp) noexcept
Descend to right child, ensuring current node is red for deletion.
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node, allowing duplicates.
Node headParent
Sentinel grandparent node.
NodeType< Key > Node
bool verifyRedBlack() const noexcept
Verify that tree satisfies all red-black properties.
Node * head
Pointer to header.
static void removeAndRendLeafRed(Node *p, Node *fp) noexcept
Remove node p and ensure it becomes a red leaf.
Node *& getRoot() noexcept
Get reference to root pointer.
static Node * getSibling(Node *p, Node *fp) noexcept
Get sibling of node p whose parent is fp.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search for a key in the tree.
Node * getRoot() const noexcept
Get const root pointer
Node * searchAndColorRed(const Key &key, Node *&fp) noexcept(nothrow_compare_v< Compare, Key >)
Search for key while coloring nodes red for deletion.
GenTdRbTree(const GenTdRbTree &)=delete
Deleted copy constructor (use explicit copy if needed)
Compare cmp
Comparison functor.
static void findPredAndSwap(Node *p, Node *&fp) noexcept
Find predecessor and swap with node p for deletion.
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove and return node with given key.
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search for key or insert if not found.
void swap(GenTdRbTree &other) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and other.
GenTdRbTree() noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare >)
Builds an empty tree with a comparison criteria built by default.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node into the tree.
Top-down red-black tree with virtual destructor.
Top-down red-black tree without virtual destructor.
Strict weak ordering constraint for BST comparators.
Node * rotate_to_left(Node *p) noexcept
Rotate to the left the tree with root p
Node * rotate_to_right(Node *p) noexcept
Rotate to the right the tree with root p
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
unsigned char Color
Definition rbNodeRk.H:50
STL namespace.
#define COLOR(p)
Definition quadnode.H:59
Red-Black tree node definition and validation utilities.
#define BLACK
Black color constant.
Definition rbNode.H:80
#define RED
Red color constant (newly inserted nodes are red)
Definition rbNode.H:77
bool is_red_black_tree(Node *node)
Test RB properties for entire subtree.
Definition rbNode.H:234
Iterator over tree nodes in sorted order.
Iterator(const GenTdRbTree &t)
#define RLINK(i, n)
#define LLINK(i, n)
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.
Basic binary tree node definitions.