50# ifndef TPL_TDRBTREERK_H
51# define TPL_TDRBTREERK_H
96template <
template <
class>
class NodeType,
typename Key,
98 requires StrictWeakOrder<Compare, Key>
107 "PATH_STACK_CAPACITY too small for red-black worst-case height");
119 bool less(
const Key& a,
const Key& b)
const
120 noexcept(nothrow_compare_v<Compare, Key>)
127 bool equals(
const Key& a,
const Key& b)
const
128 noexcept(nothrow_compare_v<Compare, Key>)
130 return not
static_cast<bool>(
cmp(a, b))
and not
static_cast<bool>(
cmp(b, a));
136 assert(p != Node::NullPtr);
137 assert(pp != Node::NullPtr);
158 assert(p != Node::NullPtr);
159 assert(pp != Node::NullPtr);
192 assert(
LLINK(fffp) == ffp or
RLINK(fffp) == ffp);
226 assert(p != Node::NullPtr);
241 noexcept(nothrow_compare_v<Compare, Key>)
243 assert(q != Node::NullPtr);
244 assert(
root != Node::NullPtr);
249 size_t stack_len = 0;
254 Node *fffp = Node::NullPtr;
257 stack[stack_len++] = p;
270 assert(fffp != Node::NullPtr);
277 if (
LLINK(p) == Node::NullPtr)
283 if (
RLINK(p) == Node::NullPtr)
292 stack[stack_len++] = p;
316 noexcept(nothrow_compare_v<Compare, Key>)
318 assert(q != Node::NullPtr);
319 assert(
root != Node::NullPtr);
323 size_t stack_len = 0;
328 Node *fffp = Node::NullPtr;
331 stack[stack_len++] = p;
341 assert(fffp != Node::NullPtr);
348 if (
LLINK(p) == Node::NullPtr)
354 if (
RLINK(p) == Node::NullPtr)
363 stack[stack_len++] = p;
383 if (p == Node::NullPtr)
388 COUNT(p) = left_count + right_count + 1;
400 Node* p = stack[--len];
409 assert(fp != Node::NullPtr);
410 assert(ffp != Node::NullPtr);
411 assert(
LLINK(fp) != Node::NullPtr);
465 assert(fp != Node::NullPtr);
466 assert(ffp != Node::NullPtr);
467 assert(
RLINK(fp) != Node::NullPtr);
521 assert(p != Node::NullPtr);
522 assert(
RLINK(p) != Node::NullPtr);
524 size_t original_count =
COUNT(p);
530 while (
LLINK(succ) != Node::NullPtr)
543 LLINK(p) = Node::NullPtr;
545 if (
RLINK(p) == succ)
569 COUNT(succ) = original_count;
575 assert(p != Node::NullPtr);
576 assert(
LLINK(p) != Node::NullPtr);
578 size_t original_count =
COUNT(p);
597 RLINK(p) = Node::NullPtr;
640 Node** stack,
size_t& stack_len)
641 noexcept(nothrow_compare_v<Compare, Key>)
650 stack[stack_len++] = p;
662 if (
LLINK(p) == Node::NullPtr)
668 if (
RLINK(p) == Node::NullPtr)
673 stack[stack_len++] = p;
680 assert(p != Node::NullPtr);
681 assert(fp != Node::NullPtr);
683 while (
LLINK(p) != Node::NullPtr or
RLINK(p) != Node::NullPtr)
685 if (
RLINK(p) != Node::NullPtr)
687 else if (
LLINK(p) != Node::NullPtr)
692 LLINK(fp) = Node::NullPtr;
694 RLINK(fp) = Node::NullPtr;
702 COUNT(Node::NullPtr) = 0;
715 noexcept(
std::is_nothrow_default_constructible_v<
Node>
and
716 std::is_nothrow_default_constructible_v<Compare>)
729 noexcept(std::is_nothrow_default_constructible_v<Node>
and
730 std::is_nothrow_copy_constructible_v<Compare>)
754 noexcept(bin_node_detail::nothrow_header_from_v<Node>
and
755 bin_node_detail::nothrow_compare_taken_v<Compare>)
759 cmp(bin_node_detail::compare_to_take(other.
cmp))
763 other.root = Node::NullPtr;
774 noexcept(std::is_nothrow_move_assignable_v<Compare>)
779 cmp = std::move(other.cmp);
781 other.root = Node::NullPtr;
792 root = Node::NullPtr;
806 std::swap(
cmp, other.cmp);
807 std::swap(
root, other.root);
834 assert(p != Node::NullPtr);
838 if (
root == Node::NullPtr)
857 assert(p != Node::NullPtr);
861 if (
root == Node::NullPtr)
880 assert(p != Node::NullPtr);
884 if (
root == Node::NullPtr)
891 if (found !=
nullptr)
905 Node*
search(
const Key& key)
const noexcept(nothrow_compare_v<Compare, Key>)
908 while (p != Node::NullPtr)
929 Node*
remove(
const Key& key)
noexcept(nothrow_compare_v<Compare, Key>)
931 if (
root == Node::NullPtr)
935 size_t stack_len = 0;
947 if (
root != Node::NullPtr)
981 std::pair<long, Node*>
position(
const Key & key)
const
982 noexcept(nothrow_compare_copy_v<Compare, Key>)
984 std::pair<long, Node*> ret_val;
999 noexcept(nothrow_compare_copy_v<Compare, Key>)
1001 std::pair<long, Node*>
r(-2,
nullptr);
1031 if (
root == Node::NullPtr)
1037 root = Node::NullPtr;
1044 root = Node::NullPtr;
1051 root = Node::NullPtr;
1057 size_t &
count)
noexcept
1059 if (p == Node::NullPtr)
1096template <
typename Key,
class Compare = Aleph::less<Key>>
1108template <
typename Key,
class Compare = Aleph::less<Key>>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Inorder iterator on the nodes of a binary tree.
Functor encompassing basic operation for extended binary search trees.
Top-down red-black tree with rank support (select/position).
bool less(const Key &a, const Key &b) const noexcept(nothrow_compare_v< Compare, Key >)
Returns true if a < b according to comparator; it throws what the comparator throws.
Node * select(size_t i) const
Select i-th node in order.
static void findPredAndSwap(Node *p, Node *&fp) noexcept
Find predecessor and swap for deletion.
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node allowing duplicates.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node.
GenTdRbTreeRk(const GenTdRbTreeRk &)=delete
Node * getRoot() const noexcept
Node * remove_pos(size_t i)
Remove node at position i.
static Node * rotate_to_left_rk(Node *p, Node *pp) noexcept
Rotate to left with counter update.
const Compare & get_compare() const noexcept
static Node * gotoRightAndColorRed(Node *fp, Node *&ffp) noexcept
Descend to right child for deletion, ensuring red.
std::pair< long, Node * > find_position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find position of a key (even if not in tree).
std::pair< long, Node * > position(const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >)
Find position of a key.
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search or insert.
Node headParent
Sentinel grandparent node.
void restoreRedCondition(Node *p, Node *&fp, Node *ffp, Node *fffp) noexcept
Restore red-black property after color flip.
static void flipColors(Node *p) noexcept
Flip colors with counter preservation.
Node *& getRoot() noexcept
Get root pointer.
GenTdRbTreeRk & operator=(GenTdRbTreeRk &&other) noexcept(std::is_nothrow_move_assignable_v< Compare >)
Takes the nodes and the comparison criteria of other, which is left empty.
static size_t updateCountRec(Node *p) noexcept
Recursively update counts in subtree - O(n)
static void updateCountsFromStack(Node **stack, size_t len) noexcept
Update counts for nodes in stack (bottom-up) - O(log n) After rotations, nodes may have moved but the...
static constexpr size_t PATH_STACK_CAPACITY
Node * searchFlipColorsAndInsert(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Search for insertion point, flipping colors proactively.
GenTdRbTreeRk() noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare >)
Builds an empty tree with a comparison criteria built by default.
void colorRootAsRed() noexcept
Color root red if safe.
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove node with given key.
Node headNode
Sentinel header node (parent of root)
Node * searchFlipColorsAndInsertDup(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Search for insertion point allowing duplicates.
static Node * rotate_to_right_rk(Node *p, Node *pp) noexcept
Rotate to right with counter update.
size_t size() const noexcept
Get number of nodes O(1)
void split_pos(size_t pos, GenTdRbTreeRk &t1, GenTdRbTreeRk &t2) noexcept
Split tree by position.
void reset() noexcept
Reset tree (does not free nodes)
Compare & get_compare() noexcept
Get comparator.
bool equals(const Key &a, const Key &b) const noexcept(nothrow_compare_v< Compare, Key >)
Returns true if a == b (neither a < b nor b < a); it throws what the comparator throws.
Compare cmp
Comparison functor.
static void findSuccAndSwap(Node *p, Node *&fp) noexcept
Find successor and swap for deletion.
Node *& root
Reference to root (right child of head)
static Node * gotoLeftAndColorRed(Node *fp, Node *&ffp) noexcept
Descend to left child for deletion, ensuring red.
Node * head
Pointer to header.
void init() noexcept
Initialize header nodes.
GenTdRbTreeRk & operator=(const GenTdRbTreeRk &)=delete
GenTdRbTreeRk(const Compare &__cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_copy_constructible_v< Compare >)
Builds an empty tree with the comparison criteria __cmp.
GenTdRbTreeRk(GenTdRbTreeRk &&other) 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 other, which is left empty and can take nodes again.
Node * searchAndColorRed(const Key &key, Node *&fp, Node **stack, size_t &stack_len) noexcept(nothrow_compare_v< Compare, Key >)
Search for key while coloring nodes red for deletion.
void swap(GenTdRbTreeRk &other) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and other.
bool verify() const
Verify tree properties. It throws what the comparator throws.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search for a key.
bool is_empty() const noexcept
Check if empty.
Node * fHead
Pointer to header's parent.
void split_pos_inorder(Node *p, size_t pos, GenTdRbTreeRk &t1, GenTdRbTreeRk &t2, size_t &count) noexcept
virtual ~GenTdRbTreeRk()=default
static void removeAndRendLeafRed(Node *p, Node *fp) noexcept
Remove node, making it a red leaf.
Top-down red-black tree with rank (virtual destructor).
Top-down red-black tree with rank (no virtual destructor).
bool check_rank_tree(Node *root) noexcept
Return true if root is a valid extended binary tree.
auto & COUNT(Node *p) noexcept
Return the number of nodes of the tree fron p is root.
long inorder_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Compute the inorder position of a key.
Node * select(Node *r, const size_t pos)
Iterative selection of a node according to inorder position.
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
bool is_red_black_bst_rk(Node *node, Compare &cmp)
Verify that tree rooted at node is a valid red-black BST with counters.
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Red-Black tree node with rank (subtree count).
#define BLACK
Black color constant.
#define RED
Red color constant (newly inserted nodes are red)
Iterator(GenTdRbTreeRk &t)
Iterator(const GenTdRbTreeRk &t)
Utility functions for binary tree operations.
Extended binary node with subtree count.
Basic binary tree node definitions.
Binary tree operations (split, join, rotate).