Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_hRbTree.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
45# ifndef TPL_HTDRBTREE_H
46# define TPL_HTDRBTREE_H
47
48# ifdef DEBUG
49# include <cmath>
50# endif
51# include <tpl_binNode.H>
52# include <tpl_binNodeUtils.H>
53# include <tpl_arrayStack.H>
54# include <rbNode.H>
55# include <ah-concepts.H>
56
57namespace Aleph
58{
59
83 template <class Key, class Compare = Aleph::less<Key>>
84 requires StrictWeakOrder<Compare, Key>
86 {
87 public:
89 using Color = unsigned char;
90
93
94 private:
95
96 private:
100
105
106 /* The path from the head node to the node being removed. remove() builds
107
108 it, consumes it and drops it: it lives in the frame of the operation,
109
110 so the tree never allocates. */
111
112 // The head node, then at most Node::MaxHeight nodes of the tree.
114
115 size_t node_count = 0;
116
117 Compare cmp;
118
120 static Node * getSibling(Node *p, Node *fp) noexcept
121 {
122 assert(LLINK(fp) == p || RLINK(fp) == p);
123 return LLINK(fp) == p ? RLINK(fp) : LLINK(fp);
124 }
125
126 // ===================== INSERTION ROUTINES =====================
127
139 Node *& fp,
140 Node *ffp,
141 Node *fffp)
142 {
143 /* Sanity checks */
144 assert(LLINK(fp) == p || RLINK(fp) == p);
145 assert(COLOR(fp) == RED);
146 assert(COLOR(p) == RED);
147
148 if (fp == root)
149 {
150 COLOR(fp) = BLACK;
151 return;
152 }
153
154 assert(LLINK(ffp) == fp || RLINK(ffp) == fp);
155 assert(COLOR(ffp) == BLACK);
156 assert(LLINK(fffp) == ffp || RLINK(fffp) == ffp);
157
158 COLOR(ffp) = RED;
159
160 if (LLINK(fp) == p && LLINK(ffp) == fp)
161 {
162 COLOR(fp) = BLACK;
163 rotate_to_right(ffp, fffp);
164 }
165 else if (RLINK(fp) == p && RLINK(ffp) == fp)
166 {
167 COLOR(fp) = BLACK;
168 rotate_to_left(ffp, fffp);
169 }
170 else
171 {
172 COLOR(p) = BLACK;
173 if (RLINK(fp) == p)
174 {
175 rotate_to_left(fp, ffp);
176 rotate_to_right(ffp, fffp);
177 }
178 else
179 {
180 rotate_to_right(fp, ffp);
181 rotate_to_left(ffp, fffp);
182 }
183 fp = fffp;
184 }
185 }
186
194 static void flipColors(Node *p) noexcept
195 {
196 assert(p != Node::NullPtr);
197 assert(COLOR(p) == BLACK);
198 assert(COLOR(LLINK(p)) == RED && COLOR(RLINK(p)) == RED);
199
200 COLOR(p) = RED;
201 COLOR(LLINK(p)) = COLOR(RLINK(p)) = BLACK;
202 }
203
218 noexcept(nothrow_compare_v<Compare, Key>)
219 {
220 assert(q != Node::NullPtr);
221 assert(root != Node::NullPtr);
222 assert(COLOR(q) == RED);
223 assert(LLINK(q) == Node::NullPtr && RLINK(q) == Node::NullPtr);
224
225 const Key & qk = KEY(q);
226
227 Node *p = root; // Current node
228 Node *fp = head; // p's parent
229 Node *ffp = fHead; // p's grand parent
230 Node *fffp = ffHead; // p's great grand parent
231 Node *nextNode;
232
233 while (true)
234 {
235 const Key & pk = KEY(p);
236
237 if (are_equals<Key, Compare>(qk, pk, cmp)) [[unlikely]]
238 return nullptr; /* Duplicated key, insertion is not possible */
239
240 if (COLOR(p) == BLACK && COLOR(LLINK(p)) == RED
241 && COLOR(RLINK(p)) == RED)
242 {
243 flipColors(p); // Rends p red
244 if (COLOR(fp) == RED) // violation of red condition
245 {
246 assert(fffp != Node::NullPtr);
247 restoreRedCondition(p, fp, ffp, fffp);
248 }
249 }
250
251 if (cmp(qk, pk))
252 {
253 if (LLINK(p) == Node::NullPtr)
254 break;
255 nextNode = LLINK(p);
256 }
257 else
258 {
259 if (RLINK(p) == Node::NullPtr)
260 break;
261 nextNode = RLINK(p);
262 }
263
264 fffp = ffp;
265 ffp = fp;
266 fp = p;
267 p = nextNode;
268 }
269
270 /* Insertion act */
271 const Key & pk = KEY(p);
272 if (cmp(qk, pk))
273 LLINK(p) = q;
274
275 else
276 RLINK(p) = q;
277
278 ++node_count; // after the comparison, which may throw
279
280 if (COLOR(p) == RED) // Violation of red condition
281 restoreRedCondition(q, p, fp, ffp);
282
283 return q;
284 }
285
289 noexcept(nothrow_compare_v<Compare, Key>)
290 {
291 assert(q != Node::NullPtr);
292 assert(root != Node::NullPtr);
293 assert(COLOR(q) == RED);
294 assert(LLINK(q) == Node::NullPtr && RLINK(q) == Node::NullPtr);
295
296 const Key & qk = KEY(q);
297
298 Node *p = root; // Current node
299 Node *fp = head; // p's parent
300 Node *ffp = fHead; // p's grand parent
301 Node *fffp = ffHead; // p's great grand parent
302 Node *nextNode;
303
304 while (true)
305 {
306 const Key & pk = KEY(p);
307
308 if (COLOR(p) == BLACK && COLOR(LLINK(p)) == RED
309 && COLOR(RLINK(p)) == RED)
310 {
311 flipColors(p); // Rends p red
312 if (COLOR(fp) == RED) // violation of red condition
313 {
314 assert(fffp != Node::NullPtr);
315 restoreRedCondition(p, fp, ffp, fffp);
316 }
317 }
318
319 if (cmp(qk, pk))
320 {
321 if (LLINK(p) == Node::NullPtr)
322 break;
323 nextNode = LLINK(p);
324 }
325 else
326 {
327 if (RLINK(p) == Node::NullPtr)
328 break;
329 nextNode = RLINK(p);
330 }
331
332 fffp = ffp;
333 ffp = fp;
334 fp = p;
335 p = nextNode;
336 }
337
338 const Key & pk = KEY(p);
339 if (cmp(qk, pk))
340 LLINK(p) = q;
341 else
342 RLINK(p) = q;
343
344 ++node_count; // after the comparison, which may throw
345
346 if (COLOR(p) == RED) // Violation of red condition
347 restoreRedCondition(q, p, fp, ffp);
348
349 return q;
350 }
351
352 // ===================== DELETION ROUTINES =====================
353
368 Node * searchAndBuildPath(const Key & key, Path & path)
369 noexcept(nothrow_compare_v<Compare, Key>)
370 {
371 assert(root != Node::NullPtr);
372
373 Node *p = root;
374 path.push(head);
375
376 while (true)
377 {
378 path.push(p);
379
380 const Key & pk = KEY(p);
381
382 if (are_equals<Key, Compare>(key, pk, cmp))
383 {
384# ifdef DEBUG
385 assert(path.size() - 1.0 <= 2*log(node_count + 1)/log(2));
386# endif
387 return p;
388 }
389
390 if (cmp(key, pk))
391 {
392 if (LLINK(p) == Node::NullPtr)
393 {
394# ifdef DEBUG
395 assert(path.size() - 1.0 <= 2*log(node_count + 1)/log(2));
396# endif
397 return p;
398 }
399
400 p = LLINK(p);
401 continue;
402 }
403
404 if (RLINK(p) == Node::NullPtr)
405 {
406# ifdef DEBUG
407 assert(path.size() - 1.0 <= 2*log(node_count + 1)/log(2));
408# endif
409 return p;
410 }
411
412 p = RLINK(p);
413 }
414 }
415
426 void findSuccAndSwap(Node *p, Node *& fp, Path & path) noexcept
427 {
428 assert(p != Node::NullPtr);
429 assert(RLINK(p) != Node::NullPtr);
430 assert(fp != Node::NullPtr);
431 assert(LLINK(fp) == p || RLINK(fp) == p);
432
433 /*
434 We save a reference to current stack content because p will be
435 swapped and p's position in stack should be occupied by
436 successor of p
437 */
438 Node *& refToSearchPath = path.top();
439
440 assert(refToSearchPath == p);
441
442 /* Find succesor while updating searchPath */
443 Node *fSucc = p; // succesor's parent
444 Node *succ = RLINK(p); // Searching starts from p's right child
445
446 path.push(succ);
447 while (LLINK(succ) != Node::NullPtr) // go down to leftmost
448 {
449 fSucc = succ;
450 succ = LLINK(succ);
451 path.push(succ);
452 }
453# ifdef DEBUG
454 assert(path.size() - 1.0 <= 2*log(node_count + 1)/log(2));
455# endif
456
457 /*
458 update old stack entry occupied by p These operations are
459 equivalents to swap old top with current top
460 */
461 refToSearchPath = succ;
462 path.top() = p;
463
464 /* Setting of parent of p to its new child (succ) */
465 if (LLINK(fp) == p)
466 LLINK(fp) = succ;
467 else
468 RLINK(fp) = succ;
469
470 /* Swaps left branches */
471 LLINK(succ) = LLINK(p);
472 LLINK(p) = Node::NullPtr;
473
474 /* For right branches there are two cases */
475 if (RLINK(p) == succ)
476 { /* successor is just right's child of p */
477 RLINK(p) = RLINK(succ);
478 RLINK(succ) = p;
479 fp = succ;
480 }
481 else
482 { /*
483 successor is the leftmost nodo descending from right child of p
484 */
485 Node *succr = RLINK(succ);
486 RLINK(succ) = RLINK(p);
487 LLINK(fSucc) = p;
488 RLINK(p) = succr;
489 fp = fSucc;
490 }
491
492 // Swap of colors
493 Color tmp = COLOR(succ);
494 COLOR(succ) = COLOR(p);
495 COLOR(p) = tmp;
496 }
497
498
511 void balanceDownAndColor(Node *p, Node *& fp, Node *& sp, Path & path) noexcept
512 {
513 assert(LLINK(fp) == p || RLINK(fp) == p);
514 assert(LLINK(fp) == sp || RLINK(fp) == sp);
515 assert(COLOR(fp) == BLACK);
516 assert(COLOR(sp) == RED);
517 assert(COLOR(p) == BLACK);
518 assert(!path.is_empty());
519
520 /* needed by rotation for links' update. We save a reference to search
521 stack because stack's head will cahnge after rotation */
522 Node *& ffp = path.top();
523
524 assert(LLINK(ffp) == fp || RLINK(ffp) == fp);
525
526 /* balancing down of p, update of out parameters and update of stack
527 entry corresponding to fp */
528 if (LLINK(fp) == p)
529 {
530 sp = LLINK(sp);
531 ffp = rotate_to_left(fp, ffp);
532 }
533 else
534 {
535 sp = RLINK(sp);
536 ffp = rotate_to_right(fp, ffp);
537 }
538
539 assert(LLINK(fp) == sp || RLINK(fp) == sp);
540 assert(COLOR(ffp) == RED);
541
542 /* coloring for ensuring to apply sibling black cases */
543 COLOR(ffp) = BLACK;
544 COLOR(fp) = RED;
545 }
546
558 void rotateNephewAndColor(Node *fp, Node *sp, Node *np, Path & path) noexcept
559 {
560 assert(LLINK(fp) == sp || RLINK(fp) == sp);
561 assert(LLINK(sp) == np || RLINK(sp) == np);
562 assert(COLOR(sp) == BLACK);
563 assert(COLOR(np) == RED);
564 assert(!path.is_empty());
565
566 Node *ffp = path.top();
567
568 assert(LLINK(ffp) == fp || RLINK(ffp) == fp);
569
570 /* rotation for downing low fp */
571 if (RLINK(sp) == np)
572 rotate_to_left(fp, ffp);
573 else
574 rotate_to_right(fp, ffp);
575
576 /* coloring for fixing up red black conditions */
577 COLOR(sp) = COLOR(fp);
578 COLOR(fp) = BLACK;
579 COLOR(np) = BLACK;
580 }
581
593 void doubleRotateNephewAndColor(Node *fp, Node *sp, Node *snp, Path & path) noexcept
594 {
595 assert(LLINK(fp) == sp || RLINK(fp) == sp);
596 assert(LLINK(sp) == snp || RLINK(sp) == snp);
597 assert(COLOR(sp) == BLACK);
598 assert(COLOR(snp) == RED);
599 assert(!path.is_empty());
600
601 Node *ffp = path.top();
602
603 assert(LLINK(ffp) == fp || RLINK(ffp) == fp);
604
605 /* double rotation for raising up of snp. snp becomes the new root of
606 sub tree fp */
607 if (LLINK(sp) == snp)
608 {
609 rotate_to_right(sp, fp);
610 rotate_to_left(fp, ffp);
611 }
612 else
613 {
614 rotate_to_left(sp, fp);
615 rotate_to_right(fp, ffp);
616 }
617
618 /* coloring for restoring all red black tree conditions */
619 COLOR(snp) = COLOR(fp);
620 COLOR(fp) = BLACK;
621 }
622
629 static void colorSiblingAsRed(Node *sp) noexcept
630 {
631 assert(COLOR(sp) == BLACK);
632 COLOR(sp) = RED;
633 }
634
642 static void colorParentAndSibling(Node *fp, Node *sp) noexcept
643 {
644 assert(LLINK(fp) == sp || RLINK(fp) == sp);
645 assert(COLOR(fp) == RED);
646 assert(COLOR(sp) == BLACK);
647
648 COLOR(fp) = BLACK;
649 COLOR(sp) = RED;
650 }
651
662 void removeAndFixBlackCondition(Node *q, Path & path) noexcept
663 {
664 assert(path.top() == q);
665
666 /* look at second item after stack's head. We cannot pop it because
667 successor finding would require stack pushing */
668 Node *fq = path.top(1);
669 Node *p; /* Saves the new child of fp after p has been deleted, this
670 node can be a violating black condition black node */
671
672 assert(fq != Node::NullPtr);
673 assert(LLINK(fq) == q || RLINK(fq) == q);
674
675 /* Deletion step: by pass if there is a Node::NullPtr link or swap
676 with successor */
677 while (1)
678 {
679 if (LLINK(q) == Node::NullPtr) // by pass to the left side
680 {
681 if (LLINK(fq) == q)
682 p = LLINK(fq) = RLINK(q);
683 else
684 p = RLINK(fq) = RLINK(q);
685 break;
686 }
687
688 if (RLINK(q) == Node::NullPtr) // by pass to the right side
689 {
690 if (LLINK(fq) == q)
691 p = LLINK(fq) = LLINK(q);
692 else
693 p = RLINK(fq) = LLINK(q);
694 break;
695 }
696
697 findSuccAndSwap(q, fq, path);
698 }
699
700 /* if color of deleted node is red, then all red black conditions are
701 met and adjust is not necessary */
702 if (COLOR(q) == RED)
703 {
704 assert(COLOR(p) == BLACK);
705 return;
706 }
707
708 /* if color of p is black, then it misses a black node and four condition
709 is violated. However, if p's child is red we can recoloring it black
710 for restoring the missing black node */
711 if (COLOR(p) == RED)
712 {
713 COLOR(p) = BLACK;
714 return;
715 }
716
717 /* Bad luck we must do recoloring and/or balancing for restoring the
718 four condition */
719 // p's sibling
720 Node *np, *snp; // p's nephew and nephewg's sibling
721 Node *fp = fq; // we process in function of p
722
723 path.popn(2); // pops deleted node and fp
724
725 /* we examine p and we restore red black properties */
726 while (true)
727 {
728 if (p == root)
729 break;
730
731 Node *sp = getSibling(p, fp);
732
733 /* if sibling is red, we rotate down for assuring that p' sibling
734 to be black */
735 if (COLOR(sp) == RED)
736 balanceDownAndColor(p, fp, sp, path);
737
738 assert(COLOR(sp) == BLACK);
739
740 /* Compute nephews */
741 if (LLINK(fp) == p)
742 {
743 np = RLINK(sp);
744 snp = LLINK(sp);
745 }
746 else
747 {
748 np = LLINK(sp);
749 snp = RLINK(sp);
750 }
751
752 if (COLOR(np) == RED)
753 {
754 rotateNephewAndColor(fp, sp, np, path);
755 break;
756 }
757
758 if (COLOR(snp) == RED)
759 {
760 doubleRotateNephewAndColor(fp, sp, snp, path);
761 break;
762 }
763
764 if (COLOR(fp) == RED)
765 {
766 colorParentAndSibling(fp, sp);
767 break;
768 }
769
770 /* color and restart process with fp */
772 p = fp;
773 fp = path.pop(); // colorSiblingAsRed(sp) does not pop it
774 }
775 }
776
777 public:
779 using key_type = Key;
780
782 Compare & key_comp() noexcept { return cmp; }
783
785 const Compare & key_comp() const noexcept { return cmp; }
786
788 Compare & get_compare() noexcept { return cmp; }
789
791 [[nodiscard]] constexpr const Compare & get_compare() const noexcept { return cmp; }
792
807 noexcept(std::is_nothrow_default_constructible_v<Node> and
808 std::is_nothrow_default_constructible_v<Compare> and
809 std::is_nothrow_constructible_v<Compare, Compare &>)
810 : HtdRbTree(Compare())
811 {
812 // empty
813 }
814
823 HtdRbTree(Compare __cmp)
824 noexcept(std::is_nothrow_default_constructible_v<Node> and
825 std::is_nothrow_constructible_v<Compare, Compare &>)
826 : head(&headNode),
829 root(headNode.getR()),
830 cmp(__cmp)
831 {
832 RLINK(fHead) = head;
833 RLINK(ffHead) = fHead;
835 COLOR(head) = BLACK;
836 COLOR(fHead) = BLACK;
837 COLOR(ffHead) = BLACK;
838 }
839
847 void swap(HtdRbTree & tree) noexcept(std::is_nothrow_swappable_v<Compare>)
848 {
849 // The comparison first: if its swap throws, the nodes stay where
850 // they were
851 std::swap(cmp, tree.cmp);
852 std::swap(root, tree.root);
853 std::swap(node_count, tree.node_count);
854 }
855
874 noexcept(bin_node_detail::nothrow_header_from_v<Node> and
875 bin_node_detail::nothrow_compare_taken_v<Compare>)
876 : headNode(bin_node_detail::header_from(tree.headNode)),
877 headParent(bin_node_detail::header_from(tree.headParent)),
878 headGrandParent(bin_node_detail::header_from(tree.headGrandParent)),
879 head(&headNode),
882 root(headNode.getR()),
884 cmp(bin_node_detail::compare_to_take(tree.cmp))
885 {
886 RLINK(fHead) = head;
887 RLINK(ffHead) = fHead;
889 COLOR(head) = BLACK;
890 COLOR(fHead) = BLACK;
891 COLOR(ffHead) = BLACK;
892 root = tree.root;
893 tree.root = Node::NullPtr;
894 tree.node_count = 0;
895 }
896
904 HtdRbTree & operator=(HtdRbTree && tree) noexcept(std::is_nothrow_swappable_v<Compare>)
905 {
906 swap(tree);
907 return *this;
908 }
909
911 HtdRbTree(const HtdRbTree &) = delete;
912
914 HtdRbTree & operator=(const HtdRbTree &) = delete;
915
917 virtual ~HtdRbTree() = default;
918
920 [[nodiscard]] constexpr bool is_empty() const noexcept { return root == Node::NullPtr; }
921
923 [[nodiscard]] constexpr size_t size() const noexcept { return node_count; }
924
926 void reset() noexcept
927 {
929 node_count = 0;
930 }
931
943 Node * insert(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
944 {
945 assert(p != Node::NullPtr);
946 assert(COLOR(p) == RED);
947 assert(LLINK(p) == Node::NullPtr && RLINK(p) == Node::NullPtr);
948
949 if (root == Node::NullPtr) [[unlikely]]
950 {
951 root = p;
952 ++node_count;
953 return p;
954 }
955
957 }
958
969 Node * search_or_insert(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
970 {
971 assert(p != Node::NullPtr);
972 assert(COLOR(p) == RED);
973 assert(LLINK(p) == Node::NullPtr && RLINK(p) == Node::NullPtr);
974
975 if (root == Node::NullPtr) [[unlikely]]
976 {
977 root = p;
978 ++node_count;
979 return p;
980 }
981
982 const Key & pk = KEY(p);
983 Node *found = search(pk);
984 if (found != nullptr) [[unlikely]]
985 return found;
986
987 return insert(p);
988 }
989
1000 Node * insert_dup(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
1001 {
1002 assert(p != Node::NullPtr);
1003 assert(COLOR(p) == RED);
1004 assert(LLINK(p) == Node::NullPtr && RLINK(p) == Node::NullPtr);
1005
1006 if (root == Node::NullPtr) [[unlikely]]
1007 {
1008 root = p;
1009 ++node_count;
1010 return p;
1011 }
1012
1014 }
1015
1025 Node * search(const Key & key) const noexcept(nothrow_compare_v<Compare, Key>)
1026 {
1027 Node *retVal = searchInBinTree(root, key, cmp);
1028 return retVal == Node::NullPtr ? nullptr : retVal;
1029 }
1030
1040 Node * remove(const Key & key) noexcept(nothrow_compare_v<Compare, Key>)
1041 {
1042 if (root == Node::NullPtr) [[unlikely]]
1043 return nullptr;
1044
1045 Path path;
1046
1047 Node *p = searchAndBuildPath(key, path);
1048
1049 if (no_equals<Key, Compare>(KEY(p), key, cmp))
1050 {
1051 return nullptr;
1052 }
1053
1055
1056 assert(node_count > 0);
1057 --node_count;
1058
1059 p->reset();
1060
1061 return p;
1062 }
1063
1064
1066 Node *& getRoot() noexcept { return root; }
1067
1069 Node * getRoot() const noexcept { return root; }
1070
1071 public:
1080 bool verifyRedBlack() const noexcept
1081 {
1082 return is_red_black_tree(root);
1083 }
1084
1090 bool verify() const
1091 {
1092 return is_red_black_bst(const_cast<Node *>(root),
1093 const_cast<HtdRbTree *>(this)->cmp);
1094 }
1095
1097 struct Iterator : public BinNodeInfixIterator<Node>
1098 {
1099 Iterator(HtdRbTree & t) noexcept
1100 : BinNodeInfixIterator<Node>(t.getRoot()) {}
1101
1102 Iterator(const HtdRbTree & t) noexcept
1103 : BinNodeInfixIterator<Node>(const_cast<HtdRbTree &>(t).getRoot()) {}
1104 };
1105 };
1106
1111 template <class Key, class Compare = Aleph::less<Key>>
1112 requires StrictWeakOrder<Compare, Key>
1113 class HtdRbTreeVtl : public HtdRbTree<Key, Compare>
1114 {
1115 public:
1117 using Node = typename Base::Node;
1118 using Base::Base; // Inherit constructors
1119 };
1120
1121} // namespace Aleph
1122
1123#endif /* TPL_HTDRBTREE_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.
Hybrid red-black tree with virtual node destructor.
typename Base::Node Node
Hybrid top-down/bottom-up red-black tree.
Definition tpl_hRbTree.H:86
HtdRbTree(const HtdRbTree &)=delete
Copy constructor deleted.
Compare cmp
Comparison functor.
static void colorParentAndSibling(Node *fp, Node *sp) noexcept
Recolor parent and sibling to fix violation.
Node * fHead
Pointer to head's parent.
Node * getRoot() const noexcept
Get root pointer (const)
Node * searchAndBuildPath(const Key &key, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
Search for key while building path stack.
static Node * getSibling(Node *p, Node *fp) noexcept
Returns sibling of p given its parent fp.
static void flipColors(Node *p) noexcept
Flip colors of a black node with two red children.
void balanceDownAndColor(Node *p, Node *&fp, Node *&sp, Path &path) noexcept
Balance down a violating black node.
void doubleRotateNephewAndColor(Node *fp, Node *sp, Node *snp, Path &path) noexcept
Double rotation and recolor to fix violation.
HtdRbTree(HtdRbTree &&tree) 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 tree, which is left empty and can take nodes again.
unsigned char Color
Color type for red-black nodes.
Definition tpl_hRbTree.H:89
Node *& root
Reference to root (right child of head)
void removeAndFixBlackCondition(Node *q, Path &path) noexcept
Remove node and fix any black-height violations.
void restoreRedCondition(Node *p, Node *&fp, Node *ffp, Node *fffp)
Restore red-black condition after insertion.
Key key_type
The key type stored in nodes.
void rotateNephewAndColor(Node *fp, Node *sp, Node *np, Path &path) noexcept
Rotate nephew up and recolor to fix violation.
Compare & key_comp() noexcept
Returns a reference to the comparison functor.
Node * searchFlipColorsAndInsert(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Search for insertion point with proactive color flipping.
size_t node_count
Number of nodes in tree.
static void colorSiblingAsRed(Node *sp) noexcept
Color sibling red to propagate violation upward.
Node *& getRoot() noexcept
Get reference to root pointer.
Node * head
Pointer to head node.
void reset() noexcept
Reset tree (does not free nodes)
HtdRbTree & operator=(const HtdRbTree &)=delete
Copy assignment deleted.
HtdRbTree & operator=(HtdRbTree &&tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps the nodes and the comparison criteria with tree.
Compare & get_compare() noexcept
Alias for key_comp()
Node headParent
Auxiliary node, grandparent of root.
Definition tpl_hRbTree.H:98
Node headNode
Auxiliary node, parent of root.
Definition tpl_hRbTree.H:97
void findSuccAndSwap(Node *p, Node *&fp, Path &path) noexcept
Find successor and swap with node to delete.
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert allowing duplicate keys.
HtdRbTree(Compare __cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with the comparison criteria __cmp.
Node * searchFlipColorsAndInsertDup(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Like searchFlipColorsAndInsert but allows duplicate keys; as it, a comparison that throws leaves a va...
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search for a key.
RbNode< Key > Node
The node type used by this tree.
Definition tpl_hRbTree.H:92
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node into the tree.
constexpr const Compare & get_compare() const noexcept
bool verifyRedBlack() const noexcept
Verify the red-black properties of every node.
void swap(HtdRbTree &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and tree.
constexpr size_t size() const noexcept
Get number of nodes in tree.
bool verify() const
Verify tree is a valid red-black BST.
Node * ffHead
Pointer to head's grandparent.
Node headGrandParent
Auxiliary node, great-grandparent of root.
Definition tpl_hRbTree.H:99
HtdRbTree() noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with a comparison criteria built by default.
constexpr bool is_empty() const noexcept
Check if tree is empty.
virtual ~HtdRbTree()=default
Virtual destructor.
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search for key or insert if not found.
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove node with given key.
const Compare & key_comp() const noexcept
Stack of fixed capacity whose storage is inside the object.
Declare RbNode type with 128-byte pool allocation.
Definition rbNode.H:116
static RbNode *const NullPtr
Definition rbNode.H:116
void reset() noexcept
Definition rbNode.H:116
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4141
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
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Search a key in a binary search tree using optimistic search.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
#define COLOR(p)
Definition quadnode.H:59
Red-Black tree node definition and validation utilities.
bool is_red_black_bst(Node *node, Compare &cmp)
Verify that tree is both a valid RB tree and valid BST.
Definition rbNode.H:251
#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(HtdRbTree &t) noexcept
Iterator(const HtdRbTree &t) noexcept
#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.