50# ifndef TPL_TDRBTREERK_H
51# define TPL_TDRBTREERK_H
96template <
template <
class>
class NodeType,
typename Key,
107 "PATH_STACK_CAPACITY too small for red-black worst-case height");
118 bool less(
const Key& a,
const Key& b)
const noexcept
124 bool equals(
const Key& a,
const Key& b)
const noexcept
132 assert(p != Node::NullPtr);
154 assert(p != Node::NullPtr);
222 assert(p != Node::NullPtr);
235 assert(q != Node::NullPtr);
269 if (
LLINK(p) == Node::NullPtr)
275 if (
RLINK(p) == Node::NullPtr)
308 assert(q != Node::NullPtr);
338 if (
LLINK(p) == Node::NullPtr)
344 if (
RLINK(p) == Node::NullPtr)
373 if (p == Node::NullPtr)
390 Node* p = stack[--len];
399 assert(fp != Node::NullPtr);
455 assert(fp != Node::NullPtr);
511 assert(p != Node::NullPtr);
520 while (
LLINK(succ) != Node::NullPtr)
533 LLINK(p) = Node::NullPtr;
535 if (
RLINK(p) == succ)
565 assert(p != Node::NullPtr);
587 RLINK(p) = Node::NullPtr;
649 if (
LLINK(p) == Node::NullPtr)
655 if (
RLINK(p) == Node::NullPtr)
667 assert(p != Node::NullPtr);
668 assert(fp != Node::NullPtr);
670 while (
LLINK(p) != Node::NullPtr
or RLINK(p) != Node::NullPtr)
672 if (
RLINK(p) != Node::NullPtr)
674 else if (
LLINK(p) != Node::NullPtr)
679 LLINK(fp) = Node::NullPtr;
681 RLINK(fp) = Node::NullPtr;
689 COUNT(Node::NullPtr) = 0;
716 other.root = Node::NullPtr;
726 other.root = Node::NullPtr;
737 root = Node::NullPtr;
764 assert(p != Node::NullPtr);
768 if (
root == Node::NullPtr)
782 assert(p != Node::NullPtr);
786 if (
root == Node::NullPtr)
800 assert(p != Node::NullPtr);
804 if (
root == Node::NullPtr)
811 if (found !=
nullptr)
823 while (p != Node::NullPtr)
839 if (
root == Node::NullPtr)
855 if (
root != Node::NullPtr)
884 std::pair<long, Node*>
position(
const Key & key)
const noexcept
886 std::pair<long, Node*>
ret_val;
897 std::pair<long, Node*>
r(-2,
nullptr);
922 if (
root == Node::NullPtr)
928 root = Node::NullPtr;
935 root = Node::NullPtr;
942 root = Node::NullPtr;
948 size_t &
count)
noexcept
950 if (p == Node::NullPtr)
987template <
typename Key,
class Compare = Aleph::less<Key>>
999template <
typename Key,
class Compare = Aleph::less<Key>>
C++20 concepts for constraining comparison functors.
Inorder iterator on the nodes of a binary tree.
Top-down red-black tree with rank support (select/position).
GenTdRbTreeRk(GenTdRbTreeRk &&other) noexcept
Move constructor.
GenTdRbTreeRk(const Compare &__cmp) noexcept
Constructor with comparator.
Node * select(size_t i) const
Select i-th node in order.
Node * searchAndColorRed(const Key &key, Node *&fp, Node **stack, size_t &stack_len) noexcept
Search for key while coloring nodes red for deletion.
static void findPredAndSwap(Node *p, Node *&fp) noexcept
Find predecessor and swap for deletion.
Node * search(const Key &key) const noexcept
Search for a key.
GenTdRbTreeRk(const GenTdRbTreeRk &)=delete
Node * getRoot() const noexcept
std::pair< long, Node * > position(const Key &key) const noexcept
Find position of a key.
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.
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.
bool less(const Key &a, const Key &b) const noexcept
Returns true if a < b according to comparator.
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
void colorRootAsRed() noexcept
Color root red if safe.
Node * insert(Node *p) noexcept
Insert a node.
Node headNode
Sentinel header node (parent of root)
static Node * rotate_to_right_rk(Node *p, Node *pp) noexcept
Rotate to right with counter update.
GenTdRbTreeRk & operator=(GenTdRbTreeRk &&other) noexcept
Move assignment.
GenTdRbTreeRk() noexcept
Default constructor.
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.
Node * remove(const Key &key) noexcept
Remove node with given key.
Node * search_or_insert(Node *p) noexcept
Search or insert.
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)
Node * searchFlipColorsAndInsert(Node *q) noexcept
Search for insertion point, flipping colors proactively.
static Node * gotoLeftAndColorRed(Node *fp, Node *&ffp) noexcept
Descend to left child for deletion, ensuring red.
Node * head
Pointer to header.
bool equals(const Key &a, const Key &b) const noexcept
Returns true if a == b (neither a < b nor b < a)
void init() noexcept
Initialize header nodes.
GenTdRbTreeRk & operator=(const GenTdRbTreeRk &)=delete
Node * searchFlipColorsAndInsertDup(Node *q) noexcept
Search for insertion point allowing duplicates.
std::pair< long, Node * > find_position(const Key &key) const noexcept
Find position of a key (even if not in tree).
void swap(GenTdRbTreeRk &other) noexcept
Swap with another tree.
bool verify() const noexcept
Verify tree properties.
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
Node * insert_dup(Node *p) noexcept
Insert a node allowing duplicates.
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).
long inorder_position(Node *r, const typename Node::key_type &key, Node *&p, Compare &cmp) noexcept
Compute the inorder position of a key.
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.
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.
and
Check uniqueness with explicit hash + equality functors.
bool is_red_black_bst_rk(Node *node, Compare &cmp)
Verify that tree rooted at node is a valid red-black BST with counters.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
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).