50# ifndef TPL_TDRBTREERK_H
51# define TPL_TDRBTREERK_H
94template <
template <
class>
class NodeType,
typename Key,
95 class Compare = std::less<Key>>
112 bool less(
const Key& a,
const Key& b)
const noexcept
118 bool equals(
const Key& a,
const Key& b)
const noexcept
126 assert(p != Node::NullPtr);
148 assert(p != Node::NullPtr);
216 assert(p != Node::NullPtr);
229 assert(q != Node::NullPtr);
263 if (
LLINK(p) == Node::NullPtr)
269 if (
RLINK(p) == Node::NullPtr)
302 assert(q != Node::NullPtr);
332 if (
LLINK(p) == Node::NullPtr)
338 if (
RLINK(p) == Node::NullPtr)
367 if (p == Node::NullPtr)
384 Node* p = stack[--len];
505 assert(p != Node::NullPtr);
514 while (
LLINK(succ) != Node::NullPtr)
527 LLINK(p) = Node::NullPtr;
529 if (
RLINK(p) == succ)
559 assert(p != Node::NullPtr);
581 RLINK(p) = Node::NullPtr;
643 if (
LLINK(p) == Node::NullPtr)
649 if (
RLINK(p) == Node::NullPtr)
661 assert(p != Node::NullPtr);
664 while (
LLINK(p) != Node::NullPtr
or RLINK(p) != Node::NullPtr)
666 if (
RLINK(p) != Node::NullPtr)
668 else if (
LLINK(p) != Node::NullPtr)
683 COUNT(Node::NullPtr) = 0;
710 other.root = Node::NullPtr;
720 other.root = Node::NullPtr;
731 root = Node::NullPtr;
758 assert(p != Node::NullPtr);
762 if (
root == Node::NullPtr)
776 assert(p != Node::NullPtr);
780 if (
root == Node::NullPtr)
794 assert(p != Node::NullPtr);
798 if (
root == Node::NullPtr)
805 if (
found !=
nullptr)
817 while (p != Node::NullPtr)
833 if (
root == Node::NullPtr)
849 if (
root != Node::NullPtr)
878 std::pair<long, Node*>
position(
const Key & key)
const noexcept
880 std::pair<long, Node*>
ret_val;
891 std::pair<long, Node*> r(-2,
nullptr);
916 if (
root == Node::NullPtr)
922 root = Node::NullPtr;
929 root = Node::NullPtr;
936 root = Node::NullPtr;
942 size_t &
count)
noexcept
944 if (p == Node::NullPtr)
981template <
typename Key,
class Compare = std::less<Key>>
993template <
typename Key,
class Compare = std::less<Key>>
Inorder iterator on the nodes of a binary tree.
T & insert(const T &item)
Insert a new item by copy.
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...
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.
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
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.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
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).