42# ifndef TPL_BINTREEOPS_H
43# define TPL_BINTREEOPS_H
63 template <
class Node,
class Cmp = Aleph::less<
typename Node::key_type>>
64 requires StrictWeakOrder<Cmp, typename Node::key_type>
77 using Key =
typename Node::key_type;
89 noexcept(
std::is_nothrow_constructible_v<
Cmp,
Cmp &>) :
cmp(cmp_)
105 noexcept(nothrow_compare_v<Cmp, Key>)
124 noexcept(nothrow_compare_v<Cmp, Key>)
126 return Aleph::search_parent<Node, Cmp>(
root, key, parent,
cmp);
151 noexcept(nothrow_compare_v<Cmp, Key>)
153 return Aleph::search_rank_parent<Node, Cmp>(
root, key,
cmp);
171 noexcept(nothrow_compare_v<Cmp, Key>)
173 if (
root == Node::NullPtr)
178 if (
LLINK(
root) != Node::NullPtr) [[likely]]
184 if (
RLINK(
root) != Node::NullPtr) [[likely]]
189 return Node::NullPtr;
207 noexcept(nothrow_compare_v<Cmp, Key>)
209 if (
root == Node::NullPtr)
214 if (
LLINK(
root) != Node::NullPtr) [[likely]]
219 if (
RLINK(
root) != Node::NullPtr) [[likely]]
240 noexcept(nothrow_compare_v<Cmp, Key>)
242 if (
r == Node::NullPtr)
247 if (
LLINK(
r) != Node::NullPtr) [[likely]]
248 __builtin_prefetch(
LLINK(
r));
253 if (
RLINK(
r) != Node::NullPtr) [[likely]]
254 __builtin_prefetch(
RLINK(
r));
265 noexcept(nothrow_compare_v<Cmp, Key>)
267 if (
root == Node::NullPtr)
269 ts = tg = Node::NullPtr;
315 noexcept(nothrow_compare_v<Cmp, Key>)
319 root = Node::NullPtr;
328 noexcept(nothrow_compare_v<Cmp, Key>)
330 if (
root == Node::NullPtr)
332 ts = tg = Node::NullPtr;
369 noexcept(nothrow_compare_v<Cmp, Key>)
372 root = Node::NullPtr;
387 noexcept(nothrow_compare_v<Cmp, Key>)
389 if (
root == Node::NullPtr)
390 return Node::NullPtr;
421 noexcept(nothrow_compare_v<Cmp, Key>)
423 Node *
l = Node::NullPtr, *
r = Node::NullPtr;
426 return Node::NullPtr;
447 noexcept(nothrow_compare_v<Cmp, Key>)
471 if (t2 == Node::NullPtr)
477 if (
insert(t1, t2) == Node::NullPtr)
504 if (t1 == Node::NullPtr)
507 if (t2 == Node::NullPtr)
519 assert(p != Node::NullPtr);
548 if (
root == Node::NullPtr)
550 l =
r = Node::NullPtr;
554 Node **current_parent =
nullptr;
555 Node **pending_child =
nullptr;
556 bool current_is_right;
561 current_is_right =
true;
567 current_is_right =
false;
571 while (current != Node::NullPtr)
573 if (
cmp(key,
KEY(current)))
575 if (not current_is_right)
577 current_is_right = not current_is_right;
578 *pending_child = *current_parent;
579 pending_child = current_parent;
582 current_parent = &
LLINK(current);
586 if (current_is_right)
588 current_is_right = not current_is_right;
589 *pending_child = *current_parent;
590 pending_child = current_parent;
592 current_parent = &
RLINK(current);
595 current = *current_parent;
598 *pending_child = Node::NullPtr;
599 root = Node::NullPtr;
617 noexcept(nothrow_compare_v<Cmp, Key>)
619 if (
root == Node::NullPtr)
625 if (left_branch == Node::NullPtr)
626 return Node::NullPtr;
634 if (right_branch == Node::NullPtr)
635 return Node::NullPtr;
641 return Node::NullPtr;
659 noexcept(nothrow_compare_v<Cmp, Key>)
661 if (
root == Node::NullPtr)
667 if (left_branch == p)
680 if (right_branch == p)
705 template <
class Node,
710 using Key =
typename Node::key_type;
720 noexcept(
std::is_nothrow_constructible_v<
Cmp,
Cmp &>)
741 noexcept(nothrow_compare_v<Cmp, Key>)
743 assert(
COUNT(Node::NullPtr) == 0);
745 if (
r == Node::NullPtr)
750 if (
LLINK(
r) != Node::NullPtr) [[likely]]
751 __builtin_prefetch(
LLINK(
r));
756 if (
RLINK(
r) != Node::NullPtr) [[likely]]
757 __builtin_prefetch(
RLINK(
r));
795 noexcept(nothrow_compare_v<Cmp, Key>)
797 assert(
COUNT(Node::NullPtr) == 0);
799 Node *parent =
nullptr;
802 while (
r != Node::NullPtr)
803 if (
const auto & kr =
KEY(
r); this->
cmp(key, kr))
806 __builtin_prefetch(
next, 0, 3);
815 else if (this->
cmp(kr, key))
818 __builtin_prefetch(
next, 0, 3);
855 noexcept(nothrow_compare_v<Cmp, Key>)
857 if (
root == Node::NullPtr)
859 l =
r = Node::NullPtr;
902 noexcept(nothrow_compare_v<Cmp, Key>)
904 if (
root == Node::NullPtr)
906 l =
r = Node::NullPtr;
943 noexcept(nothrow_compare_v<Cmp, Key>)
945 if (
root == Node::NullPtr)
949 return Node::NullPtr;
973 noexcept(nothrow_compare_v<Cmp, Key>)
975 if (
root == Node::NullPtr)
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
WeightedDigraph::Node Node
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)
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).
Utility functions for binary tree operations.
Extended binary node with subtree count.