Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_binTreeOps.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
42# ifndef TPL_BINTREEOPS_H
43# define TPL_BINTREEOPS_H
44
45# include <tpl_binNodeUtils.H>
46# include <tpl_binNodeXt.H>
47# include <ah-concepts.H>
48
49namespace Aleph
50{
63 template <class Node, class Cmp = Aleph::less<typename Node::key_type>>
64 requires StrictWeakOrder<Cmp, typename Node::key_type>
66 {
67 protected:
69
70 public:
72 Cmp &key_comp() noexcept { return cmp; }
73
75 Cmp &get_compare() noexcept { return cmp; }
76
77 using Key = typename Node::key_type;
78
79 using key_type = typename Node::key_type;
80
89 noexcept(std::is_nothrow_constructible_v<Cmp, Cmp &>) : cmp(cmp_)
90 { /* empty */
91 }
92
104 [[nodiscard]] Node * search(Node *root, const Key & key)
105 noexcept(nothrow_compare_v<Cmp, Key>)
106 {
107 return searchInBinTree(root, key, cmp);
108 }
109
123 [[nodiscard]] Node * search_parent(Node *root, const Key & key, Node *& parent)
124 noexcept(nothrow_compare_v<Cmp, Key>)
125 {
126 return Aleph::search_parent<Node, Cmp>(root, key, parent, cmp);
127 }
128
150 [[nodiscard]] Node * search_rank_parent(Node *root, const Key & key)
151 noexcept(nothrow_compare_v<Cmp, Key>)
152 {
153 return Aleph::search_rank_parent<Node, Cmp>(root, key, cmp);
154 }
155
170 [[nodiscard]] Node * insert(Node *& root, Node *p)
171 noexcept(nothrow_compare_v<Cmp, Key>)
172 {
173 if (root == Node::NullPtr)
174 return root = p;
175
176 if (cmp(KEY(p), KEY(root))) // p < root
177 {
178 if (LLINK(root) != Node::NullPtr) [[likely]]
179 __builtin_prefetch(LLINK(root));
180 return insert(LLINK(root), p);
181 }
182 if (cmp(KEY(root), KEY(p))) // p > root
183 {
184 if (RLINK(root) != Node::NullPtr) [[likely]]
185 __builtin_prefetch(RLINK(root));
186 return insert(RLINK(root), p);
187 }
188
189 return Node::NullPtr; // Repeated key ==> no insertion
190 }
191
207 noexcept(nothrow_compare_v<Cmp, Key>)
208 {
209 if (root == Node::NullPtr)
210 return root = p;
211
212 if (cmp(KEY(p), KEY(root))) // p < root
213 {
214 if (LLINK(root) != Node::NullPtr) [[likely]]
215 __builtin_prefetch(LLINK(root));
216 return insert_dup(LLINK(root), p);
217 }
218
219 if (RLINK(root) != Node::NullPtr) [[likely]]
220 __builtin_prefetch(RLINK(root));
221 return insert_dup(RLINK(root), p);
222 }
223
240 noexcept(nothrow_compare_v<Cmp, Key>)
241 {
242 if (r == Node::NullPtr)
243 return r = p;
244
245 if (cmp(KEY(p), KEY(r))) // p < root
246 {
247 if (LLINK(r) != Node::NullPtr) [[likely]]
248 __builtin_prefetch(LLINK(r));
249 return search_or_insert(LLINK(r), p);
250 }
251 if (cmp(KEY(r), KEY(p))) // p > root
252 {
253 if (RLINK(r) != Node::NullPtr) [[likely]]
254 __builtin_prefetch(RLINK(r));
255 return search_or_insert(RLINK(r), p);
256 }
257
258 return r; // key found
259 }
260
261 private:
262 // It compares on the way down and links on the way back, so a
263 // comparison that throws leaves the tree as it was
264 bool split_key_rec_helper(Node *root, const Key & key, Node *& ts, Node *& tg)
265 noexcept(nothrow_compare_v<Cmp, Key>)
266 {
267 if (root == Node::NullPtr)
268 {
269 ts = tg = Node::NullPtr;
270 return true;
271 }
272
273 if (cmp(key, KEY(root))) // ¿key < KEY(root)?
274 {
275 if (split_key_rec_helper(LLINK(root), key, ts, LLINK(root)))
276 {
277 tg = root;
278 return true;
279 }
280 return false;
281 }
282
283 if (cmp(KEY(root), key)) // ¿key > KEY(root)?
284 if (split_key_rec_helper(RLINK(root), key, RLINK(root), tg))
285 {
286 ts = root;
287 return true;
288 }
289
290 return false;
291 }
292
293 public:
314 bool split_key_rec(Node *& root, const Key & key, Node *& ts, Node *& tg)
315 noexcept(nothrow_compare_v<Cmp, Key>)
316 {
317 const bool ret = split_key_rec_helper(root, key, ts, tg);
318 if (ret)
319 root = Node::NullPtr;
320 return ret;
321 }
322
323 private:
324 // It compares on the way down and links on the way back, so a
325 // comparison that throws leaves the tree as it was
326 void split_key_dup_rec_helper(Node *root, const typename Node::key_type & key,
327 Node *& ts, Node *& tg)
328 noexcept(nothrow_compare_v<Cmp, Key>)
329 {
330 if (root == Node::NullPtr)
331 { // Key is not in tree ==> split will succeed
332 ts = tg = Node::NullPtr;
333 return;
334 }
335
336 // root goes to the tree of its side, over the split of its child that
337 // can hold keys of both sides
338 if (cmp(KEY(root), key)) // KEY(root) < key: root goes to ts
339 {
341 ts = root;
342 }
343 else // key <= KEY(root): root goes to tg
344 {
346 tg = root;
347 }
348 }
349
350 public:
367 void split_key_dup_rec(Node *& root, const typename Node::key_type & key,
368 Node *& ts, Node *& tg)
369 noexcept(nothrow_compare_v<Cmp, Key>)
370 {
371 split_key_dup_rec_helper(root, key, ts, tg);
372 root = Node::NullPtr;
373 }
374
386 [[nodiscard]] Node * remove(Node *& root, const Key & key)
387 noexcept(nothrow_compare_v<Cmp, Key>)
388 {
389 if (root == Node::NullPtr)
390 return Node::NullPtr;
391
392 if (cmp(key, KEY(root))) // ¿key < KEY(root)?
393 return remove(LLINK(root), key);
394 if (cmp(KEY(root), key)) // ¿key > KEY(root)?
395 return remove(RLINK(root), key);
396
397 Node *ret_val = root;
399
400 ret_val->reset();
401
402 return ret_val;
403 }
404
421 noexcept(nothrow_compare_v<Cmp, Key>)
422 {
423 Node *l = Node::NullPtr, *r = Node::NullPtr;
424
425 if (not split_key_rec(root, KEY(p), l, r))
426 return Node::NullPtr;
427
428 LLINK(p) = l;
429 RLINK(p) = r;
430 root = p;
431
432 return root;
433 }
434
447 noexcept(nothrow_compare_v<Cmp, Key>)
448 {
449 split_key_dup_rec(root, KEY(p), LLINK(p), RLINK(p));
450
451 return root = p;
452 }
453
469 [[nodiscard]] Node * join_preorder(Node *t1, Node *t2, Node *& dup) noexcept
470 {
471 if (t2 == Node::NullPtr)
472 return t1;
473
474 Node *l = LLINK(t2);
475 Node *r = RLINK(t2);
476
477 if (insert(t1, t2) == Node::NullPtr)
478 insert(dup, t2);
479
480 join_preorder(t1, l, dup);
481 join_preorder(t1, r, dup);
482
483 return t1;
484 }
485
502 [[nodiscard]] Node * join(Node *t1, Node *t2, Node *& dup) noexcept
503 {
504 if (t1 == Node::NullPtr)
505 return t2;
506
507 if (t2 == Node::NullPtr)
508 return t1;
509
510 Node *l = LLINK(t1);
511 Node *r = RLINK(t1);
512
513 t1->reset();
514
515 while (insert_root(t2, t1) == Node::NullPtr)
516 {
517 Node *p = remove(t1, KEY(t1));
518
519 assert(p != Node::NullPtr);
520
521 insert(dup, p); // Insert the removed node, not t1
522 }
523
524 LLINK(t2) = join(l, LLINK(t2), dup);
525 RLINK(t2) = join(r, RLINK(t2), dup);
526
527 return t2;
528 }
529
546 void split_key(Node *root, const Key & key, Node *& l, Node *& r) noexcept
547 {
548 if (root == Node::NullPtr)
549 {
550 l = r = Node::NullPtr;
551 return;
552 }
553
554 Node **current_parent = nullptr;
555 Node **pending_child = nullptr;
556 bool current_is_right;
557 if (cmp(key, KEY(root)))
558 {
559 r = root;
560 pending_child = &l;
561 current_is_right = true;
562 }
563 else
564 {
565 l = root;
566 pending_child = &r;
567 current_is_right = false;
568 }
569
570 Node *current = root;
571 while (current != Node::NullPtr)
572 {
573 if (cmp(key, KEY(current)))
574 { /* current must be in right side */
575 if (not current_is_right)
576 {
577 current_is_right = not current_is_right;
578 *pending_child = *current_parent; /* change of side */
579 pending_child = current_parent;
580 }
581
582 current_parent = &LLINK(current);
583 }
584 else
585 { /* current must be in left side */
586 if (current_is_right)
587 {
588 current_is_right = not current_is_right;
589 *pending_child = *current_parent; /* change of side */
590 pending_child = current_parent;
591 }
592 current_parent = &RLINK(current);
593 }
594
595 current = *current_parent;
596 }
597
598 *pending_child = Node::NullPtr;
599 root = Node::NullPtr;
600 }
601
617 noexcept(nothrow_compare_v<Cmp, Key>)
618 {
619 if (root == Node::NullPtr)
620 return p; /* insertion in an empty tree */
621
622 if (cmp(KEY(p), KEY(root)))
623 { /* insert in the left subtree */
624 Node *left_branch = insert_root_rec(LLINK(root), p);
625 if (left_branch == Node::NullPtr)
626 return Node::NullPtr;
627
628 LLINK(root) = left_branch;
630 }
631 else if (cmp(KEY(root), KEY(p)))
632 { /* insert in the right subtree */
633 Node *right_branch = insert_root_rec(RLINK(root), p);
634 if (right_branch == Node::NullPtr)
635 return Node::NullPtr;
636
637 RLINK(root) = right_branch;
639 }
640 else
641 return Node::NullPtr; /* duplicated key */
642
643 return root;
644 }
645
659 noexcept(nothrow_compare_v<Cmp, Key>)
660 {
661 if (root == Node::NullPtr)
662 return p; // insertion in an empty tree
663
664 if (cmp(KEY(p), KEY(root)))
665 { // insert in the left subtree
666 Node *left_branch = search_or_insert_root_rec(LLINK(root), p);
667 if (left_branch == p) // Was there insertion?
668 {
669 LLINK(root) = left_branch;
671
672 return p;
673 }
674
675 return left_branch; // no la hubo
676 }
677 if (cmp(KEY(root), KEY(p)))
678 { // insert in the right subtree
679 Node *right_branch = search_or_insert_root_rec(RLINK(root), p);
680 if (right_branch == p) //Was there insertion?
681 {
682 RLINK(root) = right_branch;
684
685 return p;
686 }
687
688 return right_branch; // no la hubo
689 }
690
691 return root;
692 }
693 };
694
695
705 template <class Node,
707 class BinTreeXt_Operation : public BinTree_Operation<Node, Cmp>
708 {
709 public:
710 using Key = typename Node::key_type;
711
720 noexcept(std::is_nothrow_constructible_v<Cmp, Cmp &>)
722 {
723 // empty
724 }
725
740 long inorder_position(Node *r, const Key & key, Node *& p)
741 noexcept(nothrow_compare_v<Cmp, Key>)
742 {
743 assert(COUNT(Node::NullPtr) == 0);
744
745 if (r == Node::NullPtr)
746 return -1;
747
748 if (this->cmp(key, KEY(r)))
749 {
750 if (LLINK(r) != Node::NullPtr) [[likely]]
751 __builtin_prefetch(LLINK(r));
752 return inorder_position(LLINK(r), key, p);
753 }
754 if (this->cmp(KEY(r), key))
755 {
756 if (RLINK(r) != Node::NullPtr) [[likely]]
757 __builtin_prefetch(RLINK(r));
758 long ret = inorder_position(RLINK(r), key, p);
759 if (ret != -1)
760 return ret + COUNT(LLINK(r)) + 1;
761 return ret;
762 }
763 p = r;
764 return COUNT(LLINK(r));
765 }
766
794 long find_position(Node *r, const Key & key, Node *& p)
795 noexcept(nothrow_compare_v<Cmp, Key>)
796 {
797 assert(COUNT(Node::NullPtr) == 0);
798
799 Node *parent = nullptr;
800 long pos = COUNT(LLINK(r));
801
802 while (r != Node::NullPtr)
803 if (const auto & kr = KEY(r); this->cmp(key, kr)) // key < KEY(r): go left
804 {
805 Node *next = LLINK(r);
806 __builtin_prefetch(next, 0, 3);
807
808 parent = r;
809 r = next;
810
811 // After moving left: new position is COUNT(LLINK(new_r))
812 // which equals old_pos - COUNT(RLINK(new_r)) - 1
813 pos -= (COUNT(RLINK(r)) + 1);
814 }
815 else if (this->cmp(kr, key)) // KEY(r) < key: go right
816 {
817 Node *next = RLINK(r);
818 __builtin_prefetch(next, 0, 3);
819
820 parent = r;
821 r = next;
822
823 // After moving right: add parent + parent's left subtree
824 pos += (COUNT(LLINK(r)) + 1);
825 }
826 else
827 {
828 p = r;
829 return pos;
830 }
831
832 p = parent;
833 return pos;
834 }
835
836
854 bool split_key_rec(Node *root, const Key & key, Node *& l, Node *& r)
855 noexcept(nothrow_compare_v<Cmp, Key>)
856 {
857 if (root == Node::NullPtr)
858 {
859 l = r = Node::NullPtr;
860 return true;
861 }
862
863 if (this->cmp(key, KEY(root)))
864 {
865 if (not split_key_rec(LLINK(root), key, l, LLINK(root)))
866 return false;
867
868 r = root;
869 COUNT(r) -= COUNT(l);
870 }
871 else if (this->cmp(KEY(root), key))
872 {
873 if (not split_key_rec(RLINK(root), key, RLINK(root), r))
874 return false;
875
876 l = root;
877 COUNT(l) -= COUNT(r);
878 }
879 else
880 return false; // Duplicate Key
881
882 return true;
883 }
884
901 void split_key_dup_rec(Node *root, const Key & key, Node *& l, Node *& r)
902 noexcept(nothrow_compare_v<Cmp, Key>)
903 {
904 if (root == Node::NullPtr)
905 {
906 l = r = Node::NullPtr;
907 return;
908 }
909
910 if (this->cmp(key, KEY(root)))
911 {
913 r = root;
914 COUNT(r) -= COUNT(l);
915 }
916 else
917 {
919 l = root;
920 COUNT(l) -= COUNT(r);
921 }
922 }
923
943 noexcept(nothrow_compare_v<Cmp, Key>)
944 {
945 if (root == Node::NullPtr)
946 return p;
947
948 if (not split_key_rec(root, KEY(p), LLINK(p), RLINK(p)))
949 return Node::NullPtr;
950
951 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
952 root = p;
953
954 return p;
955 }
956
973 noexcept(nothrow_compare_v<Cmp, Key>)
974 {
975 if (root == Node::NullPtr)
976 return p;
977
978 split_key_dup_rec(root, KEY(p), LLINK(p), RLINK(p));
979
980 COUNT(p) = COUNT(LLINK(p)) + COUNT(RLINK(p)) + 1;
981
982 return root = p;
983 }
984 };
985} // end namespace Aleph
986
987# endif // TPL_BINTREEOPS_H
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
WeightedDigraph::Node Node
@ KEY
Definition btreepic.C:169
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.
bool split_key_rec(Node *root, const Key &key, Node *&l, Node *&r) noexcept(nothrow_compare_v< Cmp, Key >)
Split an extended binary search tree according to a key.
BinTreeXt_Operation(Cmp cmp=Cmp()) noexcept(std::is_nothrow_constructible_v< Cmp, Cmp & >)
Initialize the functor with comparison criteria cmp.
Node * insert_dup_root(Node *&root, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Insert a node as root of an extended binary search tree.
void split_key_dup_rec(Node *root, const Key &key, Node *&l, Node *&r) noexcept(nothrow_compare_v< Cmp, Key >)
Split an extended binary search tree according to a key which can be in the tree.
typename Node::key_type Key
Functor encompassing basic operation for binary search trees.
Node * search_or_insert_root_rec(Node *root, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Search and eventually insert p as root in a binary search tree.
typename Node::key_type key_type
The type of key.
BinTree_Operation(Cmp cmp_=Cmp()) noexcept(std::is_nothrow_constructible_v< Cmp, Cmp & >)
The type of key.
void split_key_dup_rec_helper(Node *root, const typename Node::key_type &key, Node *&ts, Node *&tg) noexcept(nothrow_compare_v< Cmp, Key >)
void split_key(Node *root, const Key &key, Node *&l, Node *&r) noexcept
Split a binary search tree according to a key.
Node * insert_root(Node *&root, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Insert the node p as the root of a binary search tree.
Node * join(Node *t1, Node *t2, Node *&dup) noexcept
Fast union of two binary search trees.
Node * insert(Node *&root, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Insert a node p in a binary search tree.
Node * search_or_insert(Node *&r, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Search or insert a node in a binary search tree.
typename Node::key_type Key
Node * insert_dup(Node *&root, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Insert a node p in a binary search tree.
Node * search(Node *root, const Key &key) noexcept(nothrow_compare_v< Cmp, Key >)
Search a node with a specific key.
Node * join_preorder(Node *t1, Node *t2, Node *&dup) noexcept
Union of two binary search trees.
Node * search_parent(Node *root, const Key &key, Node *&parent) noexcept(nothrow_compare_v< Cmp, Key >)
Search a key and find its node and parent.
Cmp & get_compare() noexcept
See key_comp().
void split_key_dup_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg) noexcept(nothrow_compare_v< Cmp, Key >)
Split a tree according to a key value.
bool split_key_rec_helper(Node *root, const Key &key, Node *&ts, Node *&tg) noexcept(nothrow_compare_v< Cmp, Key >)
Node * insert_root_rec(Node *root, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Insert a node as root in a binary search tree.
Cmp & key_comp() noexcept
Return the comparison criteria.
bool split_key_rec(Node *&root, const Key &key, Node *&ts, Node *&tg) noexcept(nothrow_compare_v< Cmp, Key >)
Split recursively according to a key.
Node * insert_dup_root(Node *&root, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Insert node p as root of a binary search tree.
Node * remove(Node *&root, const Key &key) noexcept(nothrow_compare_v< Cmp, Key >)
Remove a key from a binary search tree.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
Node * rotate_to_left(Node *p) noexcept
Rotate to the left the tree with root p
Node * search_rank_parent(Node *root, const Key &key) noexcept(nothrow_compare_v< Cmp, Key >)
Rank search of a key in a binary search tree.
Node * rotate_to_right(Node *p) noexcept
Rotate to the right the tree with root p
auto & COUNT(Node *p) noexcept
Return the number of nodes of the tree fron p is root.
Node * insert_root(Node *&root, Node *p) noexcept(nothrow_compare_v< Cmp, Key >)
Insert a node p as root of an extended binary search tree.
Node * join_exclusive(Node *&ts, Node *&tg) noexcept
Exclusive join of two binary trees.
long inorder_position(Node *r, const Key &key, Node *&p) noexcept(nothrow_compare_v< Cmp, Key >)
Compute the inorder position of a key.
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.
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
STL namespace.
#define RLINK(i, n)
#define LLINK(i, n)
Gsl_Compatible_Rng r
Utility functions for binary tree operations.
Extended binary node with subtree count.
DynList< int > l