Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_avlRk.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
49#ifndef TPL_AVLRK_H
50#define TPL_AVLRK_H
51
52#include <algorithm>
53#include <ahFunction.H>
54#include <tpl_arrayStack.H>
55#include <tpl_binNodeXt.H>
56#include <tpl_binTreeOps.H>
57#include <tpl_binNodeUtils.H>
58#include <avlNodeRk.H>
59#include <ah-errors.H>
60#include <ah-concepts.H>
61
62namespace Aleph {
106template <template <typename> class NodeType, typename Key, class Compare>
107 requires StrictWeakOrder<Compare, Key>
109{
110public:
111 using Node = NodeType<Key>;
112
113private:
117 Compare cmp;
118
119 /* The path from the head node to the node an operation works on. Each
120 operation builds it, consumes it and drops it: it lives in the frame
121 of the operation, so the tree never allocates. Its base is head_ptr. */
122 // The head node, then at most Node::MaxHeight nodes of the tree.
124
126 static bool at_head(const Path &path) noexcept
127 {
128 return path.size() == 1;
129 }
130
131 // The searches only read the tree: a comparison that throws leaves it as
132 // it was, and the operations that call them change it after them
133 Node *search_and_stack_avl(const Key &key, signed char &cmp_result, Path &path)
134 noexcept(nothrow_compare_v<Compare, Key>)
135 {
136 assert(at_head(path) and path.base() == head_ptr);
137
138 Node *p = root;
139 Node *candidate = nullptr; // Tracks potential duplicate when going right
140 size_t candidate_pos = 0; // Stack size when candidate was set
141
142 do
143 {
144 __builtin_prefetch(LLINK(p));
145 __builtin_prefetch(RLINK(p));
146 path.push(p);
147 if (cmp(key, KEY(p))) // key < KEY(p)
148 {
149 cmp_result = CmpLess;
150 p = LLINK(p);
151 }
152 else // key >= KEY(p), could be equal
153 {
154 candidate = p;
155 candidate_pos = path.size();
156 cmp_result = CmpGreater;
157 p = RLINK(p);
158 }
159 } while (p != Node::NullPtr);
160
161 // Optimistic duplicate check: when going right, key >= KEY(candidate)
162 // If also KEY(candidate) >= key (i.e., not less), then key == KEY(candidate)
163 if (candidate != nullptr and not static_cast<bool>(cmp(KEY(candidate), key))) [[unlikely]]
164 {
165 cmp_result = CmpEqual;
166 // Truncate stack: remove nodes pushed after candidate
167 const size_t to_pop = path.size() > candidate_pos ? path.size() - candidate_pos : 0;
168 if (to_pop > 0)
169 path.popn(to_pop);
170 return candidate;
171 }
172
173 return path.top();
174 }
175
176 Node *search_dup_and_stack_avl(const Key &key, signed char &cmp_result, Path &path)
177 noexcept(nothrow_compare_v<Compare, Key>)
178 {
179 assert(at_head(path) and path.base() == head_ptr);
180 cmp_result = CmpEqual;
181
182 Node *p = root;
183 do
184 {
185 __builtin_prefetch(LLINK(p));
186 __builtin_prefetch(RLINK(p));
187 path.push(p);
188 if (cmp(key, KEY(p))) // key < KEY(p)?
189 {
190 cmp_result = CmpLess;
191 p = LLINK(p);
192 }
193 else // key >= KEY(p)
194 {
195 cmp_result = CmpGreater;
196 p = RLINK(p);
197 }
198 } while (p != Node::NullPtr);
199
200 return path.top();
201 }
202
203 // Rotate to left with counter update
204 [[gnu::always_inline]] static Node *rotateLeft(Node *p) noexcept
205 {
206 assert(DIFF(p) == 2);
207 assert(RLINK(p) != Node::NullPtr);
208
209 Node *q = RLINK(p);
210 RLINK(p) = LLINK(q);
211 LLINK(q) = p;
212
213 // Update counters
214 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
215 COUNT(q) = COUNT(LLINK(q)) + COUNT(RLINK(q)) + 1;
216
217 if (DIFF(q) == 0)
218 {
219 DIFF(q) = -1;
220 DIFF(p) = 1;
221 }
222 else
223 DIFF(q) = DIFF(p) = 0;
224
225 return q;
226 }
227
228 // Rotate to right with counter update
229 [[gnu::always_inline]] static Node *rotateRight(Node *p) noexcept
230 {
231 assert(DIFF(p) == -2);
232 assert(LLINK(p) != Node::NullPtr);
233
234 Node *q = LLINK(p);
235 LLINK(p) = RLINK(q);
236 RLINK(q) = p;
237
238 // Update counters
239 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
240 COUNT(q) = COUNT(LLINK(q)) + COUNT(RLINK(q)) + 1;
241
242 if (DIFF(q) == 0)
243 {
244 DIFF(q) = 1;
245 DIFF(p) = -1;
246 }
247 else
248 DIFF(q) = DIFF(p) = 0;
249
250 return q;
251 }
252
253 // Double rotate left with counter update
254 [[gnu::always_inline]] static Node *doubleRotateLeft(Node *p) noexcept
255 {
256 assert(DIFF(p) == 2 or DIFF(p) == -2);
257 assert(RLINK(p) != Node::NullPtr and LLINK(RLINK(p)) != Node::NullPtr);
258
259 Node *q = RLINK(p);
260 Node *r = LLINK(q);
261 RLINK(p) = LLINK(r);
262 LLINK(q) = RLINK(r);
263 LLINK(r) = p;
264 RLINK(r) = q;
265
266 // Update counters
267 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
268 COUNT(q) = COUNT(LLINK(q)) + COUNT(RLINK(q)) + 1;
269 COUNT(r) = COUNT(LLINK(r)) + COUNT(RLINK(r)) + 1;
270
271 unsigned char b, c;
272 if (DIFF(r) == 1)
273 {
274 c = 1;
275 b = 0;
276 }
277 else if (DIFF(r) == -1)
278 {
279 c = 0;
280 b = 1;
281 }
282 else
283 c = b = 1;
284
285 DIFF(r) = 0;
286 DIFF(p) = b - 1;
287 DIFF(q) = 1 - c;
288
289 return r;
290 }
291
292 // Double rotate right with counter update
293 [[gnu::always_inline]]
294 static Node *doubleRotateRight(Node *p) noexcept
295 {
296 assert(DIFF(p) == 2 or DIFF(p) == -2);
297 assert(LLINK(p) != Node::NullPtr and RLINK(LLINK(p)) != Node::NullPtr);
298
299 Node *q = LLINK(p);
300 Node *r = RLINK(q);
301 LLINK(p) = RLINK(r);
302 RLINK(q) = LLINK(r);
303 RLINK(r) = p;
304 LLINK(r) = q;
305
306 // Update counters
307 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
308 COUNT(q) = COUNT(LLINK(q)) + COUNT(RLINK(q)) + 1;
309 COUNT(r) = COUNT(LLINK(r)) + COUNT(RLINK(r)) + 1;
310
311 unsigned char b, c;
312 if (DIFF(r) == 1)
313 {
314 c = 1;
315 b = 0;
316 }
317 else if (DIFF(r) == -1)
318 {
319 c = 0;
320 b = 1;
321 }
322 else
323 c = b = 1;
324
325 DIFF(r) = 0;
326 DIFF(p) = 1 - c;
327 DIFF(q) = b - 1;
328
329 return r;
330 }
331
339
340 static Rotation_Type rotation_type(Node *p) noexcept
341 {
342 assert(DIFF(p) == 2 or DIFF(p) == -2);
343
344 Node *pc;
345 if (DIFF(p) == 2)
346 {
347 pc = RLINK(p);
348 if (DIFF(pc) == 1 or DIFF(pc) == 0)
349 return ROTATE_LEFT;
350 return DOUBLE_ROTATE_LEFT;
351 }
352
353 pc = LLINK(p);
354 if (DIFF(pc) == -1 or DIFF(pc) == 0)
355 return ROTATE_RIGHT;
356
357 return DOUBLE_ROTATE_RIGHT;
358 }
359
360 static Node *restore_avl(Node *p, Node *pp) noexcept
361 {
362 assert(LLINK(pp) == p or RLINK(pp) == p);
363 assert(DIFF(p) == -2 or DIFF(p) == 2);
364
365 Node **link = LLINK(pp) == p ? &LLINK(pp) : &RLINK(pp);
366 switch (rotation_type(p))
367 {
368 case ROTATE_LEFT:
369 return *link = rotateLeft(p);
370 case ROTATE_RIGHT:
371 return *link = rotateRight(p);
373 return *link = doubleRotateLeft(p);
375 return *link = doubleRotateRight(p);
376 default:
377 AH_ERROR("Invalid rotation type");
378 break;
379 }
380
381 return nullptr;
382 }
383
384 void restore_avl_after_insertion(Node *p, Path &path) noexcept
385 {
386 Node *pp = path.pop();
387 if (LLINK(pp) == p)
388 --DIFF(pp);
389 else
390 ++DIFF(pp);
391
392 if (DIFF(pp) == 0)
393 return;
394
395 if (at_head(path))
396 return;
397
398 do
399 {
400 Node *gpp = path.pop();
401 if (LLINK(gpp) == pp)
402 --DIFF(gpp);
403 else
404 ++DIFF(gpp);
405
406 if (DIFF(gpp) == 0)
407 break;
408 if (DIFF(gpp) == -2 or DIFF(gpp) == 2)
409 {
410 Node *ggpp = path.pop();
411 restore_avl(gpp, ggpp);
412 break;
413 }
414
415 pp = gpp;
416 } while (not at_head(path));
417 }
418
419 Node *swapWithSuccessor(Node *p, Node *&pp, Path &path) noexcept
420 {
421 Node *&ref_to_stack_top = path.top();
422
423 Node *fSucc = p;
424 Node *succ = RLINK(p);
425
426 path.push(succ);
427
428 while (LLINK(succ) != Node::NullPtr)
429 {
430 fSucc = succ;
431 succ = LLINK(succ);
432 path.push(succ);
433 }
434
435 ref_to_stack_top = succ;
436 path.top() = p;
437
438 if (LLINK(pp) == p)
439 LLINK(pp) = succ;
440 else
441 RLINK(pp) = succ;
442
443 LLINK(succ) = LLINK(p);
444 LLINK(p) = Node::NullPtr;
445
446 if (RLINK(p) == succ)
447 {
448 RLINK(p) = RLINK(succ);
449 RLINK(succ) = p;
450 pp = succ;
451 }
452 else
453 {
454 Node *succr = RLINK(succ);
455 RLINK(succ) = RLINK(p);
456 LLINK(fSucc) = p;
457 RLINK(p) = succr;
458 pp = fSucc;
459 }
460
461 // Swap balance factors
462 DIFF(succ) = DIFF(p);
463
464 // Swap counters
465 COUNT(succ) = COUNT(p);
466
467 return succ;
468 }
469
470 void restore_avl_after_deletion(bool left_deficit, Path &path) noexcept
471 {
472 Node *pp = path.top(1);
473 Node *ppp = path.popn(3);
474
475 while (true)
476 {
477 if (left_deficit)
478 ++DIFF(pp);
479 else
480 --DIFF(pp);
481
482 if (DIFF(pp) == -2 or DIFF(pp) == 2)
483 pp = restore_avl(pp, ppp);
484
485 if (DIFF(pp) != 0 or pp == root)
486 break;
487
488 left_deficit = LLINK(ppp) == pp;
489 pp = ppp;
490 ppp = path.pop();
491 }
492 }
493
494 // Update counters along the stack after insertion
495 void update_counters_after_insertion(const Path &path) noexcept
496 {
497 // Stack contains: [head_ptr, ..., nodes in path, ..., closest to insertion]
498 // top(0) = closest node, top(size-1) = head_ptr (don't update)
499 const size_t sz = path.size();
500 for (size_t i = 0; i < sz - 1; ++i)
501 ++COUNT(path.top(i));
502 }
503
504 // Update counters along the stack after deletion
505 void update_counters_after_deletion(const Path &path) noexcept
506 {
507 const size_t sz = path.size();
508 for (size_t i = 0; i < sz - 1; ++i)
509 --COUNT(path.top(i));
510 }
511
512public:
513 using key_type = Key;
514
516 [[nodiscard]] constexpr Compare &key_comp() noexcept
517 {
518 return cmp;
519 }
520
522 [[nodiscard]] constexpr Compare &get_compare() noexcept
523 {
524 return key_comp();
525 }
526
541 noexcept(std::is_nothrow_default_constructible_v<Node> and
542 std::is_nothrow_default_constructible_v<Compare> and
543 std::is_nothrow_constructible_v<Compare, Compare &>)
544 : Gen_Avl_Tree_Rk(Compare())
545 {
546 // empty
547 }
548
557 Gen_Avl_Tree_Rk(Compare cmf_fct)
558 noexcept(std::is_nothrow_default_constructible_v<Node> and
559 std::is_nothrow_constructible_v<Compare, Compare &>)
560 : head_ptr(&head_node), root(RLINK(head_ptr)), cmp(cmf_fct)
561 {
562 // empty
563 }
564
574
577
596 noexcept(bin_node_detail::nothrow_header_from_v<Node> and
597 bin_node_detail::nothrow_compare_taken_v<Compare>)
598 : head_node(bin_node_detail::header_from(tree.head_node)),
600 cmp(bin_node_detail::compare_to_take(tree.cmp))
601 {
602 root = tree.root;
603 tree.root = Node::NullPtr;
604 }
605
613 void swap(Gen_Avl_Tree_Rk &tree) noexcept(std::is_nothrow_swappable_v<Compare>)
614 {
615 // The comparison first: if its swap throws, the nodes stay where
616 // they were
617 std::swap(cmp, tree.cmp);
618 std::swap(root, tree.root);
619 }
620
621 virtual ~Gen_Avl_Tree_Rk() noexcept = default;
622
624 [[nodiscard]] constexpr Node *&getRoot() noexcept
625 {
626 return root;
627 }
628
630 [[nodiscard]] constexpr Node *getRoot() const noexcept
631 {
632 return root;
633 }
634
636 [[nodiscard]] size_t size() const noexcept
637 {
638 return COUNT(root);
639 }
640
642 [[nodiscard]] constexpr bool is_empty() const noexcept
643 {
644 return root == Node::NullPtr;
645 }
646
657 [[nodiscard]] Node *search(const Key &key) const
658 noexcept(nothrow_compare_v<Compare, Key>)
659 {
660 Node *result = searchInBinTree<Node, Compare>(root, key, cmp);
661 return result == Node::NullPtr ? nullptr : result;
662 }
663
673 [[nodiscard]] Node *insert(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
674 {
675 if (root == Node::NullPtr)
676 return root = p;
677
678 signed char cmp_result = CmpEqual;
679 Path path;
680 path.push(head_ptr);
681 Node *pp = search_and_stack_avl(KEY(p), cmp_result, path);
682 if (cmp_result == CmpLess)
683 LLINK(pp) = p;
684 else if (cmp_result == CmpGreater)
685 RLINK(pp) = p;
686 else
687 return nullptr;
688
691
692 return p;
693 }
694
706 [[nodiscard]] Node *search_or_insert(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
707 {
708 if (root == Node::NullPtr)
709 return root = p;
710
711 signed char cmp_result = CmpEqual;
712 Path path;
713 path.push(head_ptr);
714 Node *pp = search_and_stack_avl(KEY(p), cmp_result, path);
715 if (cmp_result == CmpLess)
716 LLINK(pp) = p;
717 else if (cmp_result == CmpGreater)
718 RLINK(pp) = p;
719 else
720 return pp;
721
724
725 return p;
726 }
727
737 [[nodiscard]] Node *insert_dup(Node *p) noexcept(nothrow_compare_v<Compare, Key>)
738 {
739 if (root == Node::NullPtr)
740 return root = p;
741
742 signed char cmp_result = CmpEqual;
743 Path path;
744 path.push(head_ptr);
745 Node *pp = search_dup_and_stack_avl(KEY(p), cmp_result, path);
746 if (cmp_result == CmpLess)
747 LLINK(pp) = p;
748 else
749 RLINK(pp) = p;
750
753
754 return p;
755 }
756
766 [[nodiscard]] Node *remove(const Key &key) noexcept(nothrow_compare_v<Compare, Key>)
767 {
768 if (root == Node::NullPtr)
769 return nullptr;
770
771 signed char cmp_result = CmpEqual;
772 Path path;
773 path.push(head_ptr);
774 Node *p = search_and_stack_avl(key, cmp_result, path);
775 if (cmp_result != CmpEqual)
776 return nullptr;
777
778 Node *pp = path.top(1);
779 bool left_deficit;
780
781 while (true)
782 {
783 left_deficit = LLINK(pp) == p;
784 if (LLINK(p) == Node::NullPtr)
785 {
786 if (LLINK(pp) == p)
787 LLINK(pp) = RLINK(p);
788 else
789 RLINK(pp) = RLINK(p);
790 break;
791 }
792
793 if (RLINK(p) == Node::NullPtr)
794 {
795 if (LLINK(pp) == p)
796 LLINK(pp) = LLINK(p);
797 else
798 RLINK(pp) = LLINK(p);
799 break;
800 }
801
802 swapWithSuccessor(p, pp, path);
803 }
804
805 p->reset();
806
807 if (pp == head_ptr)
808 return p;
809
811 restore_avl_after_deletion(left_deficit, path);
812
813 return p;
814 }
815
823 Node *select(const size_t i) const
824 {
825 return Aleph::select(root, i);
826 }
827
838 std::pair<long, Node *> position(const Key &key) const
839 noexcept(nothrow_compare_copy_v<Compare, Key>)
840 {
841 std::pair<long, Node *> ret_val;
842
843 ret_val.first =
845
846 return ret_val;
847 }
848
858 std::pair<long, Node *> find_position(const Key &key) const
859 noexcept(nothrow_compare_copy_v<Compare, Key>)
860 {
861 std::pair<long, Node *> r(-2, nullptr);
862
864
865 return r;
866 }
867
870 [[nodiscard]] bool verify() const
871 {
873 }
874
875private:
876 // ---------------------------------------------------------------------
877 // O(log n) join/split machinery: NOT yet wired to the public interface.
878 //
879 // The public join_exclusive(), split_key(), split_key_dup() and
880 // split_pos() below still use the simple O(n) dismantle-and-reinsert
881 // route. These routines are the intended replacement (join/split by
882 // spine descent with a pivot) and are the authoritative algorithm once
883 // connected; until then they are unreachable and unexercised, so treat
884 // the O(n) versions as the ones in force.
885 //
886 // TODO: wire these into the public join/split and drop the O(n) paths.
887 // Requires dedicated tests: nothing currently instantiates this code.
888 // ---------------------------------------------------------------------
889
890 // Compute height of AVL tree in O(log n) using balance factors
891 static size_t avl_height(Node *p) noexcept
892 {
893 size_t h = 0;
894 while (p != Node::NullPtr)
895 {
896 ++h;
897 // Follow the taller subtree
898 if (DIFF(p) >= 0)
899 p = RLINK(p); // right is taller or equal
900 else
901 p = LLINK(p); // left is taller
902 }
903 return h;
904 }
905
906 // Extract and remove the minimum node from tree rooted at p
907 // Returns the extracted node and updates p to the new root
908 static Node *extract_min(Node *&p) noexcept
909 {
910 if (p == Node::NullPtr)
911 return Node::NullPtr;
912
913 if (LLINK(p) == Node::NullPtr)
914 {
915 Node *ret = p;
916 p = RLINK(p);
917 LLINK(ret) = RLINK(ret) = Node::NullPtr;
918 COUNT(ret) = 1;
919 DIFF(ret) = 0;
920 return ret;
921 }
922
923 // Stack for rebalancing
925 Node **pp = &p;
926
927 while (LLINK(*pp) != Node::NullPtr)
928 {
929 stack.push(pp);
930 pp = &LLINK(*pp);
931 }
932
933 Node *ret = *pp;
934 *pp = RLINK(ret);
935
936 // Update counts and rebalance going up
937 while (not stack.is_empty())
938 {
939 Node **parent_ptr = stack.pop();
940 Node *parent = *parent_ptr;
941 --COUNT(parent);
942 ++DIFF(parent); // left subtree got shorter
943
944 if (DIFF(parent) == 2)
945 {
946 // Need to rebalance
947 Node *q = RLINK(parent);
948 if (DIFF(q) >= 0)
949 *parent_ptr = rotateLeft(parent);
950 else
951 *parent_ptr = doubleRotateLeft(parent);
952 }
953 else if (DIFF(parent) == 1)
954 break; // height didn't change
955 }
956
957 LLINK(ret) = RLINK(ret) = Node::NullPtr;
958 COUNT(ret) = 1;
959 DIFF(ret) = 0;
960 return ret;
961 }
962
963 // Extract and remove the maximum node from tree rooted at p
964 static Node *extract_max(Node *&p) noexcept
965 {
966 if (p == Node::NullPtr)
967 return Node::NullPtr;
968
969 if (RLINK(p) == Node::NullPtr)
970 {
971 Node *ret = p;
972 p = LLINK(p);
973 LLINK(ret) = RLINK(ret) = Node::NullPtr;
974 COUNT(ret) = 1;
975 DIFF(ret) = 0;
976 return ret;
977 }
978
980 Node **pp = &p;
981
982 while (RLINK(*pp) != Node::NullPtr)
983 {
984 stack.push(pp);
985 pp = &RLINK(*pp);
986 }
987
988 Node *ret = *pp;
989 *pp = LLINK(ret);
990
991 while (not stack.is_empty())
992 {
993 Node **parent_ptr = stack.pop();
994 Node *parent = *parent_ptr;
995 --COUNT(parent);
996 --DIFF(parent); // right subtree got shorter
997
998 if (DIFF(parent) == -2)
999 {
1000 Node *q = LLINK(parent);
1001 if (DIFF(q) <= 0)
1002 *parent_ptr = rotateRight(parent);
1003 else
1004 *parent_ptr = doubleRotateRight(parent);
1005 }
1006 else if (DIFF(parent) == -1)
1007 break;
1008 }
1009
1010 LLINK(ret) = RLINK(ret) = Node::NullPtr;
1011 COUNT(ret) = 1;
1012 DIFF(ret) = 0;
1013 return ret;
1014 }
1015
1016 // Recursive join of two AVL trees where all keys in t1 < all keys in t2
1017 // h1 and h2 are the heights of t1 and t2 respectively
1018 static Node *join_exclusive_rec(Node *t1, size_t h1, Node *t2, size_t h2) noexcept
1019 {
1020 if (t1 == Node::NullPtr)
1021 return t2;
1022 if (t2 == Node::NullPtr)
1023 return t1;
1024
1025 if (h1 >= h2)
1026 {
1027 // Extract max from t1 to use as pivot
1028 Node *pivot = extract_max(t1);
1029 if (t1 != Node::NullPtr)
1030 h1 = avl_height(t1);
1031 else
1032 h1 = 0;
1033
1034 return join_with_pivot(t1, h1, pivot, t2, h2);
1035 }
1036 // Extract min from t2 to use as pivot
1037 Node *pivot = extract_min(t2);
1038 if (t2 != Node::NullPtr)
1039 h2 = avl_height(t2);
1040 else
1041 h2 = 0;
1042
1043 return join_with_pivot(t1, h1, pivot, t2, h2);
1044 }
1045
1046 // Join t1 and t2 using pivot as the connecting node
1047 // All keys in t1 < pivot < all keys in t2
1048 static Node *join_with_pivot(Node *t1, const size_t h1, Node *pivot, Node *t2,
1049 const size_t h2) noexcept
1050 {
1051 if (h1 <= h2 + 1 and h2 <= h1 + 1)
1052 {
1053 // Heights are close enough, pivot becomes root
1054 LLINK(pivot) = t1;
1055 RLINK(pivot) = t2;
1056 DIFF(pivot) = static_cast<signed char>(static_cast<long>(h2) - static_cast<long>(h1));
1057 COUNT(pivot) = COUNT(t1) + COUNT(t2) + 1;
1058 return pivot;
1059 }
1060
1061 if (h1 > h2)
1062 {
1063 // t1 is taller, descend into right spine of t1
1064 Node *result = join_right(t1, h1, pivot, t2, h2);
1065 return result;
1066 }
1067 // t2 is taller, descend into left spine of t2
1068 Node *result = join_left(t1, h1, pivot, t2, h2);
1069 return result;
1070 }
1071
1072 // Join when h1 > h2 + 1: descend right spine of t1
1073 static Node *join_right(Node *t1, const size_t h1, Node *pivot, Node *t2, const size_t h2) noexcept
1074 {
1076 Node **pp = &t1;
1077 size_t curr_h = h1;
1078
1079 // Descend right spine until we find a subtree of appropriate height
1080 while (curr_h > h2 + 1 and *pp != Node::NullPtr)
1081 {
1082 stack.push(pp);
1083 if (DIFF(*pp) >= 0)
1084 curr_h--; // right child has height curr_h - 1
1085 else
1086 curr_h -= 2; // right child has height curr_h - 2 (since left is curr_h - 1)
1087
1088 pp = &RLINK(*pp);
1089 }
1090
1091 // Now *pp points to a subtree of height <= h2 + 1
1092 // Join pivot with *pp and t2
1093 LLINK(pivot) = *pp;
1094 RLINK(pivot) = t2;
1095 const size_t left_h = (*pp != Node::NullPtr) ? avl_height(*pp) : 0;
1096 DIFF(pivot) = static_cast<signed char>(static_cast<long>(h2) - static_cast<long>(left_h));
1097 COUNT(pivot) = COUNT(*pp) + COUNT(t2) + 1;
1098 *pp = pivot;
1099
1100 // Rebalance going up
1101 while (not stack.is_empty())
1102 {
1103 Node **parent_ptr = stack.pop();
1104 Node *parent = *parent_ptr;
1105 COUNT(parent) = COUNT(LLINK(parent)) + COUNT(RLINK(parent)) + 1;
1106
1107 const size_t new_right_h = avl_height(RLINK(parent));
1108 const size_t new_left_h = avl_height(LLINK(parent));
1109 DIFF(parent) = static_cast<signed char>(new_right_h) - static_cast<signed char>(new_left_h);
1110
1111 if (DIFF(parent) == 2)
1112 {
1113 if (Node *q = RLINK(parent); DIFF(q) >= 0)
1114 *parent_ptr = rotateLeft(parent);
1115 else
1116 *parent_ptr = doubleRotateLeft(parent);
1117 }
1118 }
1119
1120 return t1;
1121 }
1122
1123 // Join when h2 > h1 + 1: descend left spine of t2
1124 static Node *join_left(Node *t1, const size_t h1, Node *pivot, Node *t2, const size_t h2) noexcept
1125 {
1127 Node **pp = &t2;
1128 size_t curr_h = h2;
1129
1130 while (curr_h > h1 + 1 and *pp != Node::NullPtr)
1131 {
1132 stack.push(pp);
1133 if (DIFF(*pp) <= 0)
1134 curr_h--;
1135 else
1136 curr_h -= 2;
1137
1138 pp = &LLINK(*pp);
1139 }
1140
1141 RLINK(pivot) = *pp;
1142 LLINK(pivot) = t1;
1143 const size_t right_h = (*pp != Node::NullPtr) ? avl_height(*pp) : 0;
1144 DIFF(pivot) = static_cast<signed char>(static_cast<long>(right_h) - static_cast<long>(h1));
1145 COUNT(pivot) = COUNT(t1) + COUNT(*pp) + 1;
1146 *pp = pivot;
1147
1148 while (not stack.is_empty())
1149 {
1150 Node **parent_ptr = stack.pop();
1151 Node *parent = *parent_ptr;
1152 COUNT(parent) = COUNT(LLINK(parent)) + COUNT(RLINK(parent)) + 1;
1153
1154 const size_t new_right_h = avl_height(RLINK(parent));
1155 const size_t new_left_h = avl_height(LLINK(parent));
1156 DIFF(parent) = static_cast<signed char>(new_right_h) - static_cast<signed char>(new_left_h);
1157
1158 if (DIFF(parent) == -2)
1159 {
1160 if (Node *q = LLINK(parent); DIFF(q) <= 0)
1161 *parent_ptr = rotateRight(parent);
1162 else
1163 *parent_ptr = doubleRotateRight(parent);
1164 }
1165 }
1166
1167 return t2;
1168 }
1169
1170 // Split by key recursively
1171 // Returns the node containing key (or NullPtr if not found)
1172 // t1 gets all keys < key, t2 gets all keys > key
1173 // It compares on the way down and relinks on the way back, so a
1174 // comparison that throws leaves the tree as it was
1175 Node *split_key_rec(Node *p, const Key &key, Node *&t1, Node *&t2)
1176 noexcept(nothrow_compare_v<Compare, Key>)
1177 {
1178 if (p == Node::NullPtr)
1179 {
1180 t1 = t2 = Node::NullPtr;
1181 return Node::NullPtr;
1182 }
1183
1184 Node *found;
1185 if (cmp(key, KEY(p)))
1186 {
1187 // key < p->key, go left
1188 Node *left_t2;
1189 found = split_key_rec(LLINK(p), key, t1, left_t2);
1190
1191 // p and its right subtree go to t2
1192 LLINK(p) = Node::NullPtr;
1193 const size_t left_t2_h = (left_t2 != Node::NullPtr) ? avl_height(left_t2) : 0;
1194 const size_t right_h = (RLINK(p) != Node::NullPtr) ? avl_height(RLINK(p)) : 0;
1195
1196 Node *right_tree = RLINK(p);
1197 RLINK(p) = Node::NullPtr;
1198 COUNT(p) = 1;
1199 DIFF(p) = 0;
1200
1201 t2 = join_with_pivot(left_t2, left_t2_h, p, right_tree, right_h);
1202 }
1203 else if (cmp(KEY(p), key))
1204 {
1205 // key > p->key, go right
1206 Node *right_t1;
1207 found = split_key_rec(RLINK(p), key, right_t1, t2);
1208
1209 // p and its left subtree go to t1
1210 RLINK(p) = Node::NullPtr;
1211 const size_t right_t1_h = (right_t1 != Node::NullPtr) ? avl_height(right_t1) : 0;
1212 const size_t left_h = (LLINK(p) != Node::NullPtr) ? avl_height(LLINK(p)) : 0;
1213
1214 Node *left_tree = LLINK(p);
1215 LLINK(p) = Node::NullPtr;
1216 COUNT(p) = 1;
1217 DIFF(p) = 0;
1218
1219 t1 = join_with_pivot(left_tree, left_h, p, right_t1, right_t1_h);
1220 }
1221 else
1222 {
1223 // Found the key
1224 t1 = LLINK(p);
1225 t2 = RLINK(p);
1226 LLINK(p) = RLINK(p) = Node::NullPtr;
1227 COUNT(p) = 1;
1228 DIFF(p) = 0;
1229 found = p;
1230 }
1231
1232 return found;
1233 }
1234
1235 // Split by key for duplicates: keys <= key go to t1, keys > key go to t2
1236 // It compares on the way down and relinks on the way back, as
1237 // split_key_rec()
1238 void split_key_dup_rec(Node *p, const Key &key, Node *&t1, Node *&t2)
1239 noexcept(nothrow_compare_v<Compare, Key>)
1240 {
1241 if (p == Node::NullPtr)
1242 {
1243 t1 = t2 = Node::NullPtr;
1244 return;
1245 }
1246
1247 if (cmp(key, KEY(p)))
1248 {
1249 // key < p->key, p goes to t2
1250 Node *left_t2;
1251 split_key_dup_rec(LLINK(p), key, t1, left_t2);
1252
1253 LLINK(p) = Node::NullPtr;
1254 const size_t left_t2_h = (left_t2 != Node::NullPtr) ? avl_height(left_t2) : 0;
1255 const size_t right_h = (RLINK(p) != Node::NullPtr) ? avl_height(RLINK(p)) : 0;
1256
1257 Node *right_tree = RLINK(p);
1258 RLINK(p) = Node::NullPtr;
1259 COUNT(p) = 1;
1260 DIFF(p) = 0;
1261
1262 t2 = join_with_pivot(left_t2, left_t2_h, p, right_tree, right_h);
1263 }
1264 else
1265 {
1266 // key >= p->key, p goes to t1
1267 Node *right_t1;
1268 split_key_dup_rec(RLINK(p), key, right_t1, t2);
1269
1270 RLINK(p) = Node::NullPtr;
1271 const size_t right_t1_h = (right_t1 != Node::NullPtr) ? avl_height(right_t1) : 0;
1272 const size_t left_h = (LLINK(p) != Node::NullPtr) ? avl_height(LLINK(p)) : 0;
1273
1274 Node *left_tree = LLINK(p);
1275 LLINK(p) = Node::NullPtr;
1276 COUNT(p) = 1;
1277 DIFF(p) = 0;
1278
1279 t1 = join_with_pivot(left_tree, left_h, p, right_t1, right_t1_h);
1280 }
1281 }
1282
1283 // Split by position recursively
1284 // Nodes with position < pos go to t1, nodes with position >= pos go to t2
1285 void split_pos_rec(Node *p, size_t pos, Node *&t1, Node *&t2) noexcept
1286 {
1287 if (p == Node::NullPtr)
1288 {
1289 t1 = t2 = Node::NullPtr;
1290 return;
1291 }
1292
1293 if (const size_t left_count = COUNT(LLINK(p)); pos <= left_count)
1294 {
1295 // Split point is in left subtree or at p
1296 Node *left_t2;
1297 split_pos_rec(LLINK(p), pos, t1, left_t2);
1298
1299 // p goes to t2
1300 LLINK(p) = Node::NullPtr;
1301 const size_t left_t2_h = (left_t2 != Node::NullPtr) ? avl_height(left_t2) : 0;
1302 const size_t right_h = (RLINK(p) != Node::NullPtr) ? avl_height(RLINK(p)) : 0;
1303
1304 Node *right_tree = RLINK(p);
1305 RLINK(p) = Node::NullPtr;
1306 COUNT(p) = 1;
1307 DIFF(p) = 0;
1308
1309 t2 = join_with_pivot(left_t2, left_t2_h, p, right_tree, right_h);
1310 }
1311 else
1312 {
1313 // Split point is in right subtree
1314 Node *right_t1;
1315 split_pos_rec(RLINK(p), pos - left_count - 1, right_t1, t2);
1316
1317 // p goes to t1
1318 RLINK(p) = Node::NullPtr;
1319 const size_t right_t1_h = (right_t1 != Node::NullPtr) ? avl_height(right_t1) : 0;
1320 const size_t left_h = (LLINK(p) != Node::NullPtr) ? avl_height(LLINK(p)) : 0;
1321
1322 Node *left_tree = LLINK(p);
1323 LLINK(p) = Node::NullPtr;
1324 COUNT(p) = 1;
1325 DIFF(p) = 0;
1326
1327 t1 = join_with_pivot(left_tree, left_h, p, right_t1, right_t1_h);
1328 }
1329 }
1330
1331public:
1346 {
1347 if (t.root == Node::NullPtr)
1348 return;
1349
1350 if (root == Node::NullPtr)
1351 {
1352 root = t.root;
1353 t.root = Node::NullPtr;
1354 return;
1355 }
1356
1357 // Collect nodes from t in preorder and insert them one by one
1358 // This is O(m log(n+m)) but guarantees correctness
1360 stack.push(t.root);
1361 t.root = Node::NullPtr;
1362
1363 while (not stack.is_empty())
1364 {
1365 Node *p = stack.pop();
1366 Node *left = LLINK(p);
1367 Node *right = RLINK(p);
1368
1369 // Reset node and insert
1370 LLINK(p) = RLINK(p) = Node::NullPtr;
1371 COUNT(p) = 1;
1372 DIFF(p) = 0;
1373 (void) insert(p);
1374
1375 if (right != Node::NullPtr)
1376 stack.push(right);
1377 if (left != Node::NullPtr)
1378 stack.push(left);
1379 }
1380 }
1381
1402 Node *split_key(const Key &key, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
1403 {
1404 if (root == Node::NullPtr)
1405 return nullptr;
1406
1407 // Simple O(n) implementation: iterate through tree and partition
1408 Node *found = nullptr;
1410 stack.push(root);
1411 root = Node::NullPtr;
1412
1413 while (not stack.is_empty())
1414 {
1415 Node *p = stack.pop();
1416 Node *left = LLINK(p);
1417 Node *right = RLINK(p);
1418
1419 if (right != Node::NullPtr)
1420 stack.push(right);
1421 if (left != Node::NullPtr)
1422 stack.push(left);
1423
1424 LLINK(p) = RLINK(p) = Node::NullPtr;
1425 COUNT(p) = 1;
1426 DIFF(p) = 0;
1427
1428 if (cmp(KEY(p), key))
1429 (void) t1.insert(p);
1430 else if (cmp(key, KEY(p)))
1431 (void) t2.insert(p);
1432 else
1433 found = p; // Found the key
1434 }
1435
1436 return found;
1437 }
1438
1458 void split_key_dup(const Key &key, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
1459 {
1460 if (root == Node::NullPtr)
1461 return;
1462
1464 stack.push(root);
1465 root = Node::NullPtr;
1466
1467 while (not stack.is_empty())
1468 {
1469 Node *p = stack.pop();
1470 Node *left = LLINK(p);
1471 Node *right = RLINK(p);
1472
1473 if (right != Node::NullPtr)
1474 stack.push(right);
1475 if (left != Node::NullPtr)
1476 stack.push(left);
1477
1478 LLINK(p) = RLINK(p) = Node::NullPtr;
1479 COUNT(p) = 1;
1480 DIFF(p) = 0;
1481
1482 if (cmp(key, KEY(p)))
1483 (void) t2.insert(p);
1484 else
1485 (void) t1.insert_dup(p);
1486 }
1487 }
1488
1508 void split_pos(const size_t pos, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
1509 {
1510 if (root == Node::NullPtr)
1511 return;
1512
1513 if (pos == 0)
1514 {
1515 t2.root = root;
1516 root = Node::NullPtr;
1517 return;
1518 }
1519
1520 if (pos >= size())
1521 {
1522 t1.root = root;
1523 root = Node::NullPtr;
1524 return;
1525 }
1526
1527 // Simple O(n) implementation using inorder traversal
1528 size_t count = 0;
1530 Node *curr = root;
1531 root = Node::NullPtr;
1532
1533 // Inorder traversal
1534 while (curr != Node::NullPtr or not stack.is_empty())
1535 {
1536 while (curr != Node::NullPtr)
1537 {
1538 stack.push(curr);
1539 Node *left = LLINK(curr);
1540 LLINK(curr) = Node::NullPtr; // Disconnect
1541 curr = left;
1542 }
1543
1544 curr = stack.pop();
1545 Node *right = RLINK(curr);
1546 RLINK(curr) = Node::NullPtr;
1547 COUNT(curr) = 1;
1548 DIFF(curr) = 0;
1549
1550 if (count < pos)
1551 (void) t1.insert(curr);
1552 else
1553 (void) t2.insert(curr);
1554
1555 ++count;
1556 curr = right;
1557 }
1558 }
1559
1568 class Iterator : public BinTreeXt_Iterator<Gen_Avl_Tree_Rk, Node, Key, Compare>
1569 {
1571
1572 public:
1573 using Base::Base;
1574 using Base::operator =;
1575 }; // end class Iterator
1576};
1577
1589template <typename Key, class Compare = Aleph::less<Key>>
1591struct Avl_Tree_Rk : public Gen_Avl_Tree_Rk<AvlNodeRk, Key, Compare>
1592{
1594 using Base::Base;
1595};
1596
1608template <typename Key, class Compare = Aleph::less<Key>>
1610struct Avl_Tree_Rk_Vtl : public Gen_Avl_Tree_Rk<AvlNodeRkVtl, Key, Compare>
1611{
1613 using Base::Base;
1614};
1615} // end namespace Aleph
1616
1617#endif // TPL_AVLRK_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define AH_ERROR(...)
Print an error message (always enabled).
Definition ahDefs.H:270
Standard functor implementations and comparison objects.
AVL tree node with rank (subtree count).
#define DIFF(p)
Access the balance factor of node p.
Definition avlNode.H:95
@ KEY
Definition btreepic.C:169
long double h
Definition btreepic.C:154
Base iterator template for ranked binary search trees.
Functor encompassing basic operation for extended binary search trees.
long find_position(Node *r, const Key &key, Node *&p) noexcept(nothrow_compare_v< Cmp, Key >)
Find the inorder position of a key in an extended binary search tree.
Iterator over the nodes.
Definition tpl_avlRk.H:1569
AVL balanced binary search tree with rank (order statistics).
Definition tpl_avlRk.H:109
void restore_avl_after_deletion(bool left_deficit, Path &path) noexcept
Definition tpl_avlRk.H:470
Node * search_and_stack_avl(const Key &key, signed char &cmp_result, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
Definition tpl_avlRk.H:133
static Node * join_with_pivot(Node *t1, const size_t h1, Node *pivot, Node *t2, const size_t h2) noexcept
Definition tpl_avlRk.H:1048
void update_counters_after_insertion(const Path &path) noexcept
Definition tpl_avlRk.H:495
NodeType< Key > Node
Definition tpl_avlRk.H:111
static size_t avl_height(Node *p) noexcept
Definition tpl_avlRk.H:891
void restore_avl_after_insertion(Node *p, Path &path) noexcept
Definition tpl_avlRk.H:384
constexpr Node *& getRoot() noexcept
Return a modifiable reference to tree's root.
Definition tpl_avlRk.H:624
Gen_Avl_Tree_Rk(Gen_Avl_Tree_Rk &&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.
Definition tpl_avlRk.H:595
Node * swapWithSuccessor(Node *p, Node *&pp, Path &path) noexcept
Definition tpl_avlRk.H:419
static Node * join_exclusive_rec(Node *t1, size_t h1, Node *t2, size_t h2) noexcept
Definition tpl_avlRk.H:1018
void split_key_dup_rec(Node *p, const Key &key, Node *&t1, Node *&t2) noexcept(nothrow_compare_v< Compare, Key >)
Definition tpl_avlRk.H:1238
Node * select(const size_t i) const
Return the i-th node in order sense.
Definition tpl_avlRk.H:823
void swap(Gen_Avl_Tree_Rk &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swap in constant time all the items of this with the items of tree.
Definition tpl_avlRk.H:613
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove from tree the node containing key.
Definition tpl_avlRk.H:766
Gen_Avl_Tree_Rk() 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.
Definition tpl_avlRk.H:540
Node head_node
The type of node.
Definition tpl_avlRk.H:114
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search a node containing key.
Definition tpl_avlRk.H:657
static Node * join_left(Node *t1, const size_t h1, Node *pivot, Node *t2, const size_t h2) noexcept
Definition tpl_avlRk.H:1124
virtual ~Gen_Avl_Tree_Rk() noexcept=default
static Rotation_Type rotation_type(Node *p) noexcept
Definition tpl_avlRk.H:340
Gen_Avl_Tree_Rk(Compare cmf_fct) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with the comparison criteria cmf_fct.
Definition tpl_avlRk.H:557
std::pair< long, Node * > position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Compute the inorder position of a key.
Definition tpl_avlRk.H:838
Node * split_key_rec(Node *p, const Key &key, Node *&t1, Node *&t2) noexcept(nothrow_compare_v< Compare, Key >)
Definition tpl_avlRk.H:1175
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert the node p without testing for key duplicity.
Definition tpl_avlRk.H:737
static Node * restore_avl(Node *p, Node *pp) noexcept
Definition tpl_avlRk.H:360
static Node * rotateLeft(Node *p) noexcept
Definition tpl_avlRk.H:204
size_t size() const noexcept
Return the number of nodes in the tree.
Definition tpl_avlRk.H:636
void split_pos_rec(Node *p, size_t pos, Node *&t1, Node *&t2) noexcept
Definition tpl_avlRk.H:1285
Gen_Avl_Tree_Rk(const Gen_Avl_Tree_Rk &)=delete
Not copyable: the tree does not own its nodes, and a copy would hold the same ones.
constexpr Compare & key_comp() noexcept
The key type.
Definition tpl_avlRk.H:516
static Node * extract_max(Node *&p) noexcept
Definition tpl_avlRk.H:964
Node * search_dup_and_stack_avl(const Key &key, signed char &cmp_result, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
Definition tpl_avlRk.H:176
std::pair< long, Node * > find_position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find the inorder position of a key in the tree.
Definition tpl_avlRk.H:858
static Node * extract_min(Node *&p) noexcept
Definition tpl_avlRk.H:908
static Node * rotateRight(Node *p) noexcept
Definition tpl_avlRk.H:229
void join_exclusive(Gen_Avl_Tree_Rk &t) noexcept
Join this tree exclusively with another tree.
Definition tpl_avlRk.H:1345
constexpr bool is_empty() const noexcept
Return true if tree is empty.
Definition tpl_avlRk.H:642
void update_counters_after_deletion(const Path &path) noexcept
Definition tpl_avlRk.H:505
static Node * doubleRotateLeft(Node *p) noexcept
Definition tpl_avlRk.H:254
constexpr Compare & get_compare() noexcept
See key_comp().
Definition tpl_avlRk.H:522
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search or insert a key.
Definition tpl_avlRk.H:706
void split_key_dup(const Key &key, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
Split tree by key including duplicates.
Definition tpl_avlRk.H:1458
static Node * doubleRotateRight(Node *p) noexcept
Definition tpl_avlRk.H:294
bool verify() const
Return true if the tree is a valid AVL tree with correct counters and its keys are in order.
Definition tpl_avlRk.H:870
Node * split_key(const Key &key, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
Split tree by key.
Definition tpl_avlRk.H:1402
Gen_Avl_Tree_Rk & operator=(const Gen_Avl_Tree_Rk &)=delete
Not copyable, for the same reason as the copy constructor.
void split_pos(const size_t pos, Gen_Avl_Tree_Rk &t1, Gen_Avl_Tree_Rk &t2) noexcept
Split tree by inorder position.
Definition tpl_avlRk.H:1508
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert the node pointed by p in the tree.
Definition tpl_avlRk.H:673
constexpr Node * getRoot() const noexcept
Return a pointer to the tree's root.
Definition tpl_avlRk.H:630
static bool at_head(const Path &path) noexcept
true when path holds only the head node.
Definition tpl_avlRk.H:126
static Node * join_right(Node *t1, const size_t h1, Node *pivot, Node *t2, const size_t h2) noexcept
Definition tpl_avlRk.H:1073
Stack of fixed capacity whose storage is inside the object.
T pop() noexcept
Pop the top; the stack must not be empty.
T & push(const T &data) noexcept
Push a copy of data.
T & top() noexcept
Return a modifiable reference to the top; the stack must not be empty.
bool is_empty() const noexcept
Return true if the stack is empty.
Strict weak ordering constraint for BST comparators.
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended binary tree.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
auto & COUNT(Node *p) noexcept
Return the number of nodes of the tree fron p is root.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
long inorder_position(Node *r, const Key &key, Node *&p) noexcept(nothrow_compare_v< Cmp, Key >)
Compute the inorder position of a key.
Main namespace for Aleph-w library functions.
bool is_avl_rk(Node *p)
Verify if tree rooted at p is a valid AVL tree with correct counters.
Definition avlNodeRk.H:124
and
Check uniqueness with explicit hash + equality functors.
@ CmpGreater
First argument is greater than second.
@ CmpLess
First argument is less than second.
@ CmpEqual
Arguments are equal.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
Ranked AVL tree with nodes with a virtual destructor.
Definition tpl_avlRk.H:1611
Ranked AVL tree with nodes without a virtual destructor.
Definition tpl_avlRk.H:1592
#define RLINK(i, n)
#define LLINK(i, n)
Gsl_Compatible_Rng r
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.
Extended binary node with subtree count.
Binary tree operations (split, join, rotate).