42#ifndef TPL_BINNODEUTILS_H
43#define TPL_BINNODEUTILS_H
57# include <type_traits>
80template <
class Fct,
class... Args>
82 std::is_nothrow_invocable_r_v<bool, Fct, Args...>
and
83 requires { {
static_cast<bool>(std::declval<Fct>()(std::declval<Args>()...)) }
noexcept; };
92template <
class Cmp,
class Key>
94 nothrow_condition_v<Cmp, const Key &, const Key &>
and
95 nothrow_condition_v<Cmp, const Key &, Key &>
and
96 nothrow_condition_v<Cmp, Key &, const Key &>
and
97 nothrow_condition_v<Cmp, Key &, Key &>;
119template <
class Compare,
class Key>
121 nothrow_compare_keys_v<Compare &, Key>
and
122 nothrow_compare_keys_v<const Compare &, Key>;
134template <
class Compare,
class Key>
136 nothrow_compare_v<Compare, Key>
and
137 std::is_nothrow_constructible_v<Compare, Compare &>
and
138 std::is_nothrow_constructible_v<Compare, const Compare &>;
155template <BinNodeLike Node>
158 assert((Node::NullPtr ==
nullptr or
root !=
nullptr)
and
159 "nullptr is not an empty tree for a node type with a sentinel; use Node::NullPtr");
161template <BinNodeLike Node>
163 void (*visitFct)(
Node *,
int,
int))
165 if (node == Node::NullPtr)
170 (*visitFct)(node, level, position);
198template <BinNodeLike Node>
207template <BinNodeLike Node>
209 void (*visitFct)(
Node *,
int,
int))
211 if (p == Node::NullPtr)
214 (*visitFct)(p, level, position);
243template <BinNodeLike Node>
252template <BinNodeLike Node>
254 void (*visitFct)(
Node *,
int,
int))
256 if (node == Node::NullPtr)
262 (*visitFct)(node, level, position);
288template <BinNodeLike Node>
318 if (
root == Node::NullPtr)
355template <BinNodeLike Node,
class Op>
383 if (
root == Node::NullPtr)
420template <BinNodeLike Node,
class Op>
447 if (
root == Node::NullPtr)
484template <BinNodeLike Node,
class Op>
490template <BinNodeLike Node>
493 if (
root == Node::NullPtr)
501template <BinNodeLike Node>
504 if (
root == Node::NullPtr)
512template <BinNodeLike Node>
515 if (
root == Node::NullPtr)
530template <BinNodeLike Node>
545template <BinNodeLike Node>
560template <BinNodeLike Node>
574template <BinNodeLike Node>
578 if (
root == Node::NullPtr)
585template <BinNodeLike Node>
612template <BinNodeLike Node>
615 if (
root == Node::NullPtr)
628 for (; p != Node::NullPtr; p =
LLINK(p), ++depth)
630 pending.
push_back(std::pair<Node *, size_t>(
r, depth + 1));
632 height = std::max(height, depth - 1);
649template <BinNodeLike Node>
652 if (
root == Node::NullPtr)
655 if (levels == 0) [[unlikely]]
664 return 1 + std::max(left_height, right_height);
680template <BinNodeLike Node>
706template <BinNodeLike Node,
class Dispose>
709 if (
root == Node::NullPtr)
712 Node *parent = Node::NullPtr;
721 if (child == Node::NullPtr)
724 if (child == Node::NullPtr)
726 RLINK(p) = Node::NullPtr;
738 if (parent == Node::NullPtr)
744 LLINK(p) = Node::NullPtr;
745 if (
r == Node::NullPtr)
748 RLINK(p) = Node::NullPtr;
773template <BinNodeLike Node,
class Dispose>
777 if (
root == Node::NullPtr)
780 if (levels == 0) [[unlikely]]
807template <BinNodeLike Node>
812 const auto free_node = [] (
Node *p)
noexcept {
delete p; };
814 root = Node::NullPtr;
834template <BinNodeLike Node>
839 using Key =
typename Node::key_type;
841 const auto destroy_key = [] (
Node *p)
noexcept { p->get_key().~Key(); };
843 root = Node::NullPtr;
858template <BinNodeLike Node>
862 if (
root == Node::NullPtr)
863 return Node::NullPtr;
865 Node *tgt_root = Node::NullPtr;
872 pending.
push_back(std::pair<Node *, Node **>(
root, &tgt_root));
882 for (; src != Node::NullPtr; src =
LLINK(src))
911template <BinNodeLike Node>
918 if (t1 == Node::NullPtr or t2 == Node::NullPtr)
935template <BinNodeLike Node,
class Equal>
941 if (t1 == Node::NullPtr or t2 == Node::NullPtr)
944 if (not op(
KEY(t1),
KEY(t2)))
951template <BinNodeLike Node,
class Equal = std::equal_to<
typename Node::key_type>>
973template <BinNodeLike Node>
977 if (
root == Node::NullPtr)
981 queue.
put(std::pair<Node *, bool>(
root,
bool()));
983 for (
int pos = 0; not queue.
is_empty(); pos++)
985 std::pair<Node *, bool> pr = queue.
get();
988 (*visitFct)(p, pos, pr.second);
990 if (
LLINK(p) != Node::NullPtr)
991 queue.
put(std::pair<Node *, bool>(
LLINK(p),
true));
993 if (
RLINK(p) != Node::NullPtr)
994 queue.
put(std::pair<Node *, bool>(
RLINK(p),
false));
1013template <BinNodeLike Node,
class Operation>
1016 if (
root == Node::NullPtr)
1024 if (not operation(p))
1027 if (
LLINK(p) != Node::NullPtr)
1030 if (
RLINK(p) != Node::NullPtr)
1036template <BinNodeLike Node,
class Operation>
1039 return level_traverse<Node, Operation>(
root, operation);
1057template <
template <
class>
class Node,
typename Key>
1067 assert(r_p - l_p == r_i - l_i);
1077 for (
int j = l_i; j <= r_i; ++j)
1085 ah_domain_error_if(i < 0) <<
"build_tree: root key not found in inorder array (corrupted input)";
1087 assert(i <= r_i - l_i);
1094template <
template <
class>
class Node,
typename Key>
1098 assert(rp - lp == ri - li);
1106 for (; i <= ri; ++i)
1107 if (in[i] == post[rp])
1112 <<
"build_postorder: root key not found in inorder array (corrupted input)";
1116 LLINK(
root) = build_postorder<Node, Key>(post, lp, lp + (i - li) - 1, in, li, i - 1);
1117 RLINK(
root) = build_postorder<Node, Key>(post, rp - (ri - i), rp - 1, in, i + 1, ri);
1121template <BinNodeLike Node>
1125 if (
root == Node::NullPtr)
1128 if (current_level == level)
1146template <BinNodeLike Node>
1174template <BinNodeLike Node>
1177 if (
root == Node::NullPtr)
1181 while (p != Node::NullPtr)
1184 if (q == Node::NullPtr)
1193 while (q !=
r and RLINK(q) != Node::NullPtr)
1205 RLINK(q) = Node::NullPtr;
1231template <BinNodeLike Node>
1234 if (node == Node::NullPtr)
1237 Node *p = node, *
r = Node::NullPtr;
1238 while (p != Node::NullPtr)
1242 if (q == Node::NullPtr)
1251 while (q !=
r and RLINK(q) != Node::NullPtr)
1262 RLINK(q) = Node::NullPtr;
1268template <BinNodeLike Node>
1271 if (p == Node::NullPtr)
1285template <BinNodeLike Node>
1305template <BinNodeLike Node>
1308 if (
root == Node::NullPtr)
1332template <BinNodeLike Node>
1347template <BinNodeLike Node>
1351 const size_t n =
bits.size();
1354 for (
size_t i = 0; i < n; ++i)
1355 str.push_back(
bits(i) ?
'b' :
'a');
1360template <BinNodeLike Node>
1365 <<
"bits_to_tree_helper: bit array index out of bounds (malformed input)";
1367 if (
const int bit = array.
read_bit(i++); bit == 1)
1368 return Node::NullPtr;
1371 Node *left = Node::NullPtr;
1372 Node *right = Node::NullPtr;
1375 left = bits_to_tree_helper<Node>(array, i);
1376 right = bits_to_tree_helper<Node>(array, i);
1405template <BinNodeLike Node>
1408 return bits_to_tree_helper<Node>(array, idx);
1428template <BinNodeLike Node>
1431 if (
root == Node::NullPtr)
1450template <BinNodeLike Node>
1453 if (
root == Node::NullPtr)
1456 input >>
root->get_key();
1477template <BinNodeLike Node>
1497template <BinNodeLike Node>
1515template <BinNodeLike Node,
class Get_Key>
1518 if (
root == Node::NullPtr)
1521 const std::string str = Get_Key()(
root);
1523 for (
const char ch : str)
1546 put_tree_keys_in_array<Node, Get_Key>(
LLINK(
root),
out);
1547 put_tree_keys_in_array<Node, Get_Key>(
RLINK(
root),
out);
1550template <BinNodeLike Node,
class Load_Key>
1553 if (
root == Node::NullPtr)
1559 load_tree_keys_from_array<Node, Load_Key>(
LLINK(
root),
keys, idx);
1560 load_tree_keys_from_array<Node, Load_Key>(
RLINK(
root),
keys, idx);
1591template <BinNodeLike Node,
class Get_Key>
1597 prefix.save_in_array_of_chars(array_name +
"_cdp",
output);
1598 output <<
"const char * " << array_name <<
"_k[] = { " <<
'\n';
1599 put_tree_keys_in_array<Node, Get_Key>(
root,
output);
1600 output <<
"nullptr };" <<
'\n';
1632template <BinNodeLike Node,
class Load_Key>
1640 load_tree_keys_from_array<Node, Load_Key>(
root,
keys, i);
1644template <BinNodeLike Node,
class Compare>
1646 const typename Node::key_type *max_key,
const Compare &
cmp)
1648 if (p == Node::NullPtr)
1651 const auto &key =
KEY(p);
1657 return check_bst_range<Node, Compare>(
LLINK(p), min_key, &key,
cmp)
and
1658 check_bst_range<Node, Compare>(
RLINK(p), &key, max_key,
cmp);
1670template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
1674 return check_bst_range<Node, Compare>(p,
nullptr,
nullptr,
cmp);
1688template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
1690 const Compare &
cmp = Compare())
1693 return Node::NullPtr;
1700 int first_greater =
l + 1;
1732template <
typename T,
class Compare = Aleph::less<T>>
1759template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
1761 const Compare &
cmp = Compare())
1765 Node *candidate =
nullptr;
1767 while (
root != Node::NullPtr)
1782 if (candidate !=
nullptr and not
static_cast<bool>(
cmp(
KEY(candidate), key))) [[unlikely]]
1785 return Node::NullPtr;
1796template <BinNodeLike Node>
1800 assert(
root != Node::NullPtr &&
"find_min called on empty tree");
1815template <BinNodeLike Node>
1819 assert(
root != Node::NullPtr &&
"find_max called on empty tree");
1834template <BinNodeLike Node>
1837 assert(p != Node::NullPtr);
1838 assert(
RLINK(p) != Node::NullPtr);
1842 while (
LLINK(p) != Node::NullPtr)
1859template <BinNodeLike Node>
1862 assert(p != Node::NullPtr);
1863 assert(
LLINK(p) != Node::NullPtr);
1868 while (
RLINK(p) != Node::NullPtr)
1893template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
1895 const Compare &
cmp = Compare())
1900 assert(
root != Node::NullPtr);
1948template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
1950 const Compare &
cmp = Compare())
1954 assert(
root != Node::NullPtr);
1957 if (
const auto &root_key =
KEY(
root);
cmp(key, root_key))
1964 else if (
cmp(root_key, key))
1987template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
1991 if (
r == Node::NullPtr) [[unlikely]]
1994 const auto &pk =
KEY(p);
1995 const auto &rk =
KEY(
r);
1998 return insert_in_bst<Node, Compare>(
LLINK(
r), p,
cmp);
2000 return insert_in_bst<Node, Compare>(
RLINK(
r), p,
cmp);
2002 [[unlikely]]
return Node::NullPtr;
2017template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
2021 if (
root == Node::NullPtr) [[unlikely]]
2024 const auto &pk =
KEY(p);
2047template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
2051 if (
r == Node::NullPtr) [[unlikely]]
2054 const auto &pk =
KEY(p);
2055 const auto &rk =
KEY(
r);
2058 return search_or_insert_in_bst<Node, Compare>(
LLINK(
r), p,
cmp);
2060 return search_or_insert_in_bst<Node, Compare>(
RLINK(
r), p,
cmp);
2062 [[unlikely]]
return r;
2080template <BinNodeLike Node,
class Compare>
2083 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
2085 if (
root == Node::NullPtr)
2087 ts = tg = Node::NullPtr;
2134template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
2136 const Compare &
cmp = Compare())
2142 root = Node::NullPtr;
2161template <BinNodeLike Node,
class Compare>
2164 noexcept(nothrow_compare_v<Compare, typename Node::key_type>)
2166 if (
root == Node::NullPtr)
2168 ts = tg = Node::NullPtr;
2202template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
2204 const Compare &
cmp = Compare())
2209 root = Node::NullPtr;
2225template <BinNodeLike Node>
2228 if (ts == Node::NullPtr)
2231 if (tg == Node::NullPtr)
2238 ts = tg = Node::NullPtr;
2254template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
2256 const Compare &
cmp = Compare()) noexcept
2259 if (
root == Node::NullPtr)
2260 return Node::NullPtr;
2289template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
2293 Node *
l = Node::NullPtr, *
r = Node::NullPtr;
2296 return Node::NullPtr;
2315template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
2336template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
2339 if (t2 == Node::NullPtr)
2370template <BinNodeLike Node,
class Compare = Aleph::less<
typename Node::key_type>>
2373 if (t1 == Node::NullPtr)
2376 if (t2 == Node::NullPtr)
2388 assert(p != Node::NullPtr);
2404template <BinNodeLike Node>
2407 assert(p != Node::NullPtr);
2423template <BinNodeLike Node>
2426 assert(p != Node::NullPtr);
2427 assert(pp != Node::NullPtr);
2447template <BinNodeLike Node>
2450 assert(p != Node::NullPtr);
2465template <BinNodeLike Node>
2468 assert(p != Node::NullPtr);
2469 assert(pp != Node::NullPtr);
2499template <BinNodeLike Node,
class Key,
class Compare = Aleph::less<
typename Node::key_type>>
2501 const Compare &
cmp = Compare()) noexcept
2504 assert(
l == Node::NullPtr
and r == Node::NullPtr);
2505 if (
root == Node::NullPtr)
2507 l =
r = Node::NullPtr;
2511 Node **current_parent =
nullptr;
2512 Node **pending_child =
nullptr;
2513 bool current_is_right =
true;
2523 current_is_right =
false;
2527 while (current != Node::NullPtr)
2529 if (
cmp(key,
KEY(current)))
2531 if (not current_is_right)
2533 current_is_right = not current_is_right;
2534 *pending_child = *current_parent;
2535 pending_child = current_parent;
2537 current_parent = &
LLINK(current);
2541 if (current_is_right)
2543 current_is_right = not current_is_right;
2544 *pending_child = *current_parent;
2545 pending_child = current_parent;
2547 current_parent = &
RLINK(current);
2549 current = *current_parent;
2551 *pending_child = Node::NullPtr;
2552 root = Node::NullPtr;
2564template <BinNodeLike Node>
2571 assert(p != Node::NullPtr
and q != Node::NullPtr
and pp != Node::NullPtr
and pq != Node::NullPtr);
2574 assert(
LLINK(q) == Node::NullPtr);
2583 LLINK(p) = Node::NullPtr;
2616template <BinNodeLike Node>
2623 assert((p != Node::NullPtr)
and (q != Node::NullPtr)
and (pp != Node::NullPtr)
and
2624 (pq != Node::NullPtr));
2625 assert((
RLINK(pp) == p) or (
LLINK(pp) == p));
2626 assert((
RLINK(pq) == q) or (
LLINK(pq) == q));
2627 assert(
RLINK(q) == Node::NullPtr);
2636 RLINK(p) = Node::NullPtr;
2672template <BinNodeLike
Node,
class Key =
typename Node::key_type,
2677 if (
root == Node::NullPtr)
2683 if (left_branch == Node::NullPtr)
2684 return Node::NullPtr;
2692 if (right_branch == Node::NullPtr)
2693 return Node::NullPtr;
2699 return Node::NullPtr;
2714template <BinNodeLike
Node,
class Key =
typename Node::key_type,
2719 if (
root == Node::NullPtr)
2725 if (left_branch == p)
2737 if (right_branch == p)
2744 return right_branch;
2757template <
class Node>
2768 static constexpr size_t Inline_Depth = Node::MaxHeight < 128 ? Node::MaxHeight : 128;
2775 std::swap(
root, it.root);
2776 std::swap(
curr, it.curr);
2810 if (
RLINK(p) != Node::NullPtr)
2813 if (
LLINK(p) != Node::NullPtr)
2824 if (
root == Node::NullPtr)
2826 curr = Node::NullPtr;
2836 curr = Node::NullPtr;
2860 return curr != Node::NullPtr;
2884 if (
l != Node::NullPtr)
2887 if (
r != Node::NullPtr)
2892 if (
r != Node::NullPtr)
2899 curr = Node::NullPtr;
2935template <BinNodeLike Node,
class Op>
2939 if (not op(it.get_curr()))
2962template <BinNodeLike Node,
class Op>
2976template <
class Node>
2989 static constexpr size_t Inline_Depth = Node::MaxHeight < 128 ? Node::MaxHeight : 128;
2994 while (
LLINK(
r) != Node::NullPtr)
3004 while (
RLINK(
r) != Node::NullPtr)
3012 if (
root != Node::NullPtr)
3031 std::swap(
root, it.root);
3032 std::swap(
curr, it.curr);
3033 std::swap(
pos, it.pos);
3072 if (
root == Node::NullPtr)
3074 curr = Node::NullPtr;
3086 curr = Node::NullPtr;
3111 return curr != Node::NullPtr;
3144 if (
curr != Node::NullPtr)
3151 curr = Node::NullPtr;
3188template <BinNodeLike Node,
class Op>
3192 if (not op(it.get_curr()))
3201template <BinNodeLike Node,
class Op>
3225template <BinNodeLike Node,
class Op>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Standard functor implementations and comparison objects.
WeightedDigraph::Node Node
Space-efficient bit array implementation.
EepicNode< long > * build_tree()
size_t size_t int32_t * out
Inorder iterator on the nodes of a binary tree.
BinNodeInfixIterator()=default
Node * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
void next()
Move the iterator one position forward.
void reset_last() noexcept
Reset the iterator to the last node inorder.
Node * get_curr() const
Return the current node. Throw overflow_error if there is no current.
BinNodeInfixIterator(const BinNodeInfixIterator &it)
bool is_last() const noexcept
bool has_curr() const noexcept
Return true the iterator has current node.
void next_ne()
Move forward without checking the position; it may throw bad_alloc, as the constructor does.
bool is_in_first() const noexcept
Return true if the iterator is on the first node.
SmallVector< Node *, Inline_Depth > s
BinNodeInfixIterator(BinNodeInfixIterator &&it) noexcept
BinNodeInfixIterator & operator=(const BinNodeInfixIterator &it)
Node * advance_to_min(Node *r)
static constexpr size_t Inline_Depth
static Node * advance_to_max(Node *r) noexcept
void reset_first()
Reset the iterator to the first node inorder; it may throw bad_alloc, as the constructor does.
size_t get_pos() const
Return the current position of iterator. Only valid if has_curr() == true.
void swap(BinNodeInfixIterator &it) noexcept
BinNodeInfixIterator(Node *r)
Initialize an iterator on the first node inorder.
Preorder iterator on the nodes of a binary tree.
static constexpr size_t Inline_Depth
Node * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
bool has_curr() const noexcept
Return true if iterator has current node.
BinNodePrefixIterator(BinNodePrefixIterator &&it) noexcept
Node * get_curr() const
Return a pointer to current node.
void reset_first() noexcept
Reset the iterator to the first node in preorder sense.
void next_ne()
Move the iterator one position forward without checking it.
void reset_last() noexcept
Reset the iterator to the last node in preorder.
BinNodePrefixIterator(Node *r) noexcept
Initialize an iterator on the first node in preorder for the tree with root r
void next()
Move the iterator one position forward.
void swap(BinNodePrefixIterator &it) noexcept
Swap thiswith it
BinNodePrefixIterator & operator=(const BinNodePrefixIterator &it)
void end() noexcept
Put the iterator in end state.
BinNodePrefixIterator()=default
SmallVector< Node *, Inline_Depth > s
BinNodePrefixIterator(const BinNodePrefixIterator &it)
static Node * last(Node *p) noexcept
Contiguous array of bits.
int read_bit(const size_t i) const
Read bit i.
void load_from_array_of_chars(const unsigned char str[], const size_t num_bits)
Reads an array of bits saved in a character array.
void load(std::istream &input)
Loads an array of bits saved with save().
void push(const unsigned int value)
Inserts the value at the end of the array.
constexpr size_t size() const noexcept
Returns the dimension of the bit array.
Dynamic doubly linked list with O(1) size and bidirectional access.
T & append(const T &item)
Append a copied item at the end of the list.
Dynamic queue of elements of generic type T based on single linked list.
T & put(const T &data)
The type of element.
T get()
Remove the oldest item of the queue.
bool is_empty() const noexcept
Return true if this is empty.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Generic inorder traversal of a binary tree.
void traverse(Node *root, Op &&op) const
Invoke to traversal from root node.
static void for_each_inorder(Node *root, Op &&op)
void operator()(Node *root, Op &op) const
See traverse(Node * root, Op && op).
Generic postorder traversal of a binary tree.
void operator()(Node *root, Op &op) const
See traverse(Node * root, Op && op).
static void postorder(Node *root, Op &&op)
void traverse(Node *root, Op &&op) const
Invoke the traversal.
Generic preorder traversal of a binary tree.
static void preorder(Node *root, Op &&op)
void traverse(Node *root, Op &&op) const
Invoke the traversal.
void operator()(Node *root, Op &op) const
See traverse(Node * root, Op && op).
Contiguous dynamic array with N elements of inline storage.
T & push_back(const T &item)
STL-style alias of append(const T &).
T & append(const T &item)
Append a copy of item.
void empty() noexcept
Destroy all elements (Aleph convention).
void swap(SmallVector &v) noexcept(std::is_nothrow_move_constructible_v< T >)
Swap contents with v.
T remove_last()
Remove and return the last element.
bool is_empty() const noexcept
Return true if no elements are stored. O(1).
__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)
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
bool prefix_traverse(Node *root, Op op)
Traverse a tree in preorder via its iterator and performs a conditioned operation on each item.
int postOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in postorder a binary tree.
constexpr bool nothrow_compare_copy_v
Whether Compare compares two keys of type Key without throwing, as nothrow_compare_v,...
Node * rotate_to_left(Node *p) noexcept
Rotate to the left the tree with root p
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
void split_key_dup_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Split a tree according to a key value.
void save_tree_in_array_of_chars(Node *root, const std::string &array_name, std::ostream &output)
Generate C++ array declarations for a binary tree.
Node * search_rank_parent(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Rank search of a key in a binary search tree.
bool split_key_rec_helper(Node *root, const typename Node::key_type &key, Node *&ts, Node *&tg, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
The recursion of split_key_rec().
void save_tree_keys_in_prefix(Node *root, std::ostream &output)
Store in output stream the tree keys in preorder.
Node * search_parent(Node *root, const typename Node::key_type &key, Node *&parent, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Search a key and find its node and parent.
bool split_key_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Split recursively according to a key.
void load_tree_keys_in_prefix(Node *root, std::istream &input)
Load the keys stored in preorder from an input stream.
bool level_traverse(Node *root, Operation &operation)
Level traverse a tree and execute an operation.
DynDlist< Node * > compute_nodes_in_level(Node *root, const int &level)
Count the number of nodes in a specific tree level.
Node * join_preorder(Node *t1, Node *t2, Node *&dup, const Compare &cmp=Compare()) noexcept
Union of two binary search trees.
void swap_node_with_successor(Node *p, Node *&pp, Node *q, Node *&pq) noexcept
Swap a node with its successor inorder.
Node * remove_from_bst(Node *&root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept
Remove a key from a binary search tree.
void for_each_in_order(Node *root, Op &&op)
Execute an operation in order sense for each node of tree.
Node * find_predecessor(Node *p, Node *&pp) noexcept
Find the inorder predecessor of p
Node * load_tree_from_array(const unsigned char bits[], const size_t &num_bits, const char *keys[])
Build a binary tree from two arrays.
Node * find_min(Node *root) noexcept
Return the minimum key contained in a binary search tree.
bool traverse(Node *root, Op op)
See infix_traverse().
Node * rotate_to_right(Node *p) noexcept
Rotate to the right the tree with root p
Node * find_successor(Node *p, Node *&pp) noexcept
Find the inorder successor of p
Node * copyRec(Node *root)
Copy a tree.
void for_each_postorder(Node *root, Op &&op)
Execute an operation in postorder sense for each node of tree.
Node * load_tree(std::istream &input)
Load and build a binary tree from a stream.
size_t compute_height_bounded(Node *root, const size_t levels) noexcept
Compute the height of a tree by a recursion of bounded depth.
constexpr bool nothrow_condition_v
Whether calling Fct with Args, and taking its result as a bool in either way, cannot throw.
void for_each_preorder(Node *root, Op &&op)
Execute an operation in preorder sense for each node of tree.
Node * search_or_insert_in_bst(Node *&r, Node *p, const Compare &cmp=Compare()) noexcept
Search or insert a node in a binary search tree.
void save_tree(Node *root, std::ostream &output)
Store a binary tree in a stream.
void dispose_tree_by_link_inversion(Node *root, Dispose &dispose) noexcept
Dispose of every node of a tree in postorder with additional memory.
ThreeWayCmp three_way_compare(const T &a, const T &b, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, T >)
Three-way comparison using a binary comparator.
constexpr bool nothrow_compare_keys_v
Whether the calls of Cmp that compare two keys of type Key, constant or not, give a bool without thro...
size_t compute_height_with_stack(Node *root) noexcept
Compute the height of a tree with an explicit stack of pending subtrees.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
constexpr size_t Bin_Tree_Depth_Bound
Depth up to which the traversals below work inside the call stack.
Node * bits_to_tree(const BitArray &array, int idx=0)
Build a binary tree given its bits code.
void split_key_dup_rec_helper(Node *root, const typename Node::key_type &key, Node *&ts, Node *&tg, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
The recursion of split_key_dup_rec().
Node * insert_in_bst(Node *&r, Node *p, const Compare &cmp=Compare()) noexcept
Insert a node p in a binary search tree.
void tree_to_bits(Node *root, BitArray &array)
Compute a bit code for the binary tree.
size_t internal_path_length(Node *p) noexcept
Compute the internal path length.
void preOrderThreaded(Node *node, void(*visitFct)(Node *))
Traverse preorder a binary tree without recursion and without stack.
void inOrderThreaded(Node *root, void(*visitFct)(Node *))
Traverse inorder a binary tree without recursion and without stack.
Node * preorder_to_bst(DynArray< typename Node::key_type > &preorder, int l, int r, const Compare &cmp=Compare())
Build a binary search tree from its preorder traversal.
void dispose_tree_nodes(Node *root, Dispose &dispose, const size_t levels=Bin_Tree_Depth_Bound) noexcept
Dispose of every node of a tree in postorder.
void levelOrder(Node *root, void(*visitFct)(Node *, int, bool))
Traverse a binary tree by levels.
int inOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively inorder a binary tree.
Node * join_exclusive(Node *&ts, Node *&tg) noexcept
Exclusive join of two binary trees.
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
Node * search_or_insert_root_rec(Node *root, Node *p, const Compare &cmp=Compare()) noexcept
Search and eventually insert p as root in a binary search tree.
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.
Node * insert_dup_in_bst(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert a node p in a binary search tree.
Node * insert_root(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert the node p as root of a binary search tree.
Node * insert_dup_root(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert node p as root of a binary search tree.
void swap_node_with_predecessor(Node *p, Node *&pp, Node *q, Node *&pq) noexcept
Swap a node with its predecessor inorder.
size_t compute_cardinality_rec(Node *root) noexcept
Count the number of nodes of a binary tree.
Node * insert_root_rec(Node *root, Node *p, const Compare &cmp=Compare()) noexcept
Insert a node as root in a binary search tree.
void assert_valid_tree_root(const Node *root) noexcept
Debug-only check that root is a valid tree for Node.
constexpr bool nothrow_compare_v
Whether Compare compares two keys of type Key without throwing, as the operations of the trees call i...
bool infix_traverse(Node *root, Op op)
Traverse a tree in inorder via its iterator and performs a conditioned operation on each item.
size_t computeHeightRec(Node *root) noexcept
Compute the height of root
Node * find_max(Node *root) noexcept
Return the maximum key contained in a binary search tree.
void split_key(Node *&root, const Key &key, Node *&l, Node *&r, const Compare &cmp=Compare()) noexcept
Split a binary search tree according to a key.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
static void infix(Node *root, DynList< Node * > &acc)
static void suffix(Node *root, DynList< Node * > &acc)
void infix_for_each(Node *root, Op op)
Traverse all the container and performs an operation on each element.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
void inorder_rec_helper(Node *node, const int &level, int &position, void(*visitFct)(Node *, int, int))
void load_tree_keys_from_array(Node *root, const char *keys[], int &idx)
std::decay_t< typename HeadC::Item_Type > T
bool areEquivalents(Node *t1, Node *t2, Equal &op) noexcept
Return true if trees are equivalents.
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
void preorder_rec_helper(Node *p, const int &level, int &position, void(*visitFct)(Node *, int, int))
static void prefix(Node *root, DynList< Node * > &acc)
and
Check uniqueness with explicit hash + equality functors.
void postorder_rec_helper(Node *node, const int &level, int &position, void(*visitFct)(Node *, int, int))
bool less_or_equal_than(const T &op1, const T &op2, Compare &cmp)
Determines if op1 is less than or equal to op2 using a comparison operator.
void put_tree_keys_in_array(Node *root, std::ostream &out)
Node< Key > * build_postorder(const DynArray< Key > &post, long lp, long rp, const DynArray< Key > &in, long li, long ri)
ThreeWayCmp
Constants for three-way comparison results.
@ CmpGreater
First argument is greater than second.
@ CmpLess
First argument is less than second.
@ CmpEqual
Arguments are equal.
Node * bits_to_tree_helper(const BitArray &array, int &i)
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
size_t internal_path_length_helper(Node *p, const size_t &level) noexcept
bool areSimilar(Node *t1, Node *t2) noexcept
Return true if both trees are similar.
bool check_bst_range(Node *p, const typename Node::key_type *min_key, const typename Node::key_type *max_key, const Compare &cmp)
void compute_nodes_in_level_helper(Node *root, long level, const long current_level, DynDlist< Node * > &level_list)
void callKeyDestructorsRec(Node *&root) noexcept
Traverses the tree and calls key's destructors.
void prefix_for_each(Node *root, Op op)
Traverse in preorder all the container and performs an operation on each element.
DynArray< int > postorder
Circular queue implementations backed by arrays.
Stack implementations backed by dynamic or fixed arrays.
Basic binary tree node definitions.
Dynamic doubly linked list implementation.
Dynamic queue implementation based on linked lists.
Dynamic array with inline storage (Aleph::SmallVector).