52# ifndef TPL_TDRBTREE_H
53# define TPL_TDRBTREE_H
95template <
template <
class>
class NodeType,
typename Key,
115 bool less(
const Key& a,
const Key& b)
const noexcept
121 bool equals(
const Key& a,
const Key& b)
const noexcept
182 assert(p != Node::NullPtr);
193 assert(q != Node::NullPtr);
224 if (
LLINK(p) == Node::NullPtr)
230 if (
RLINK(p) == Node::NullPtr)
259 assert(q != Node::NullPtr);
287 if (
LLINK(p) == Node::NullPtr)
293 if (
RLINK(p) == Node::NullPtr)
320 assert(fp != Node::NullPtr);
382 assert(fp != Node::NullPtr);
443 assert(p != Node::NullPtr);
445 assert(fp != Node::NullPtr);
452 while (
LLINK(succ) != Node::NullPtr)
467 LLINK(p) = Node::NullPtr;
470 if (
RLINK(p) == succ)
498 assert(p != Node::NullPtr);
500 assert(fp != Node::NullPtr);
522 RLINK(p) = Node::NullPtr;
579 if (
LLINK(p) == Node::NullPtr)
585 if (
RLINK(p) == Node::NullPtr)
595 assert(p != Node::NullPtr);
596 assert(fp != Node::NullPtr);
599 while (
LLINK(p) != Node::NullPtr
or RLINK(p) != Node::NullPtr)
601 if (
RLINK(p) != Node::NullPtr)
603 else if (
LLINK(p) != Node::NullPtr)
608 LLINK(fp) = Node::NullPtr;
610 RLINK(fp) = Node::NullPtr;
646 other.root = Node::NullPtr;
658 other.root = Node::NullPtr;
673 root = Node::NullPtr;
704 assert(p != Node::NullPtr);
708 if (
root == Node::NullPtr)
725 assert(p != Node::NullPtr);
729 if (
root == Node::NullPtr)
746 assert(p != Node::NullPtr);
750 if (
root == Node::NullPtr)
759 if (found !=
nullptr)
774 while (p != Node::NullPtr)
790 if (
root == Node::NullPtr)
815 if (p == Node::NullPtr)
835 if (p == Node::NullPtr)
852 if (p == Node::NullPtr)
866 if (
root == Node::NullPtr)
892template <
typename Key,
class Compare = Aleph::less<Key>>
906template <
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 binary search tree implementation.
void reset() noexcept
Reset tree to empty state (does not free nodes)
virtual ~GenTdRbTree()=default
void init() noexcept
Initialize header nodes.
Compare & get_compare() noexcept
Get reference to comparator.
GenTdRbTree(GenTdRbTree &&other) noexcept
Move constructor.
bool is_empty() const noexcept
Check if tree is empty.
Node * insert_dup(Node *p) noexcept
Insert a node, allowing duplicates.
const Compare & get_compare() const noexcept
Node * fHead
Pointer to header's parent.
size_t size() const noexcept
Get number of nodes in tree (O(1))
Node *& root
Reference to root (right child of head)
Node * searchAndColorRed(const Key &key, Node *&fp) noexcept
Search for key while coloring nodes red for deletion.
static void flipColors(Node *p) noexcept
Flip colors: black parent with two red children becomes red with two black children.
Node * searchFlipColorsAndInsert(Node *q) noexcept
Search for insertion point, flipping colors proactively to maintain balance.
static Node * gotoLeftAndColorRed(Node *fp, Node *&ffp) noexcept
Descend to left child, ensuring current node is red for deletion.
Node headNode
Sentinel header node (parent of root)
void colorRootAsRed() noexcept
Color root red if safe (both children black)
void restoreRedCondition(Node *p, Node *&fp, Node *ffp, Node *fffp) noexcept
Restore red-black property after color flip creates two consecutive reds.
size_t num_nodes
Number of nodes in tree.
GenTdRbTree & operator=(GenTdRbTree &&other) noexcept
Move assignment operator.
GenTdRbTree & operator=(const GenTdRbTree &)=delete
Deleted copy assignment.
Node * remove(const Key &key) noexcept
Remove and return node with given key.
Node * searchFlipColorsAndInsertDup(Node *q) noexcept
Search for insertion point allowing duplicates.
Node * search_or_insert(Node *p) noexcept
Search for key or insert if not found.
static void findSuccAndSwap(Node *p, Node *&fp) noexcept
Find successor and swap with node p for deletion.
Node * search(const Key &key) const noexcept
Search for a key in the tree.
static Node * gotoRightAndColorRed(Node *fp, Node *&ffp) noexcept
Descend to right child, ensuring current node is red for deletion.
Node headParent
Sentinel grandparent node.
GenTdRbTree() noexcept
Default constructor.
bool verifyRedBlack() const noexcept
Verify that tree satisfies all red-black properties.
Node * head
Pointer to header.
static void removeAndRendLeafRed(Node *p, Node *fp) noexcept
Remove node p and ensure it becomes a red leaf.
static bool verify(Node *p) noexcept
Recursively verify red-black properties.
static bool testBlackHeight(Node *p, int &max, int bh=0) noexcept
Test black height property recursively.
Node *& getRoot() noexcept
Get reference to root pointer.
static Node * getSibling(Node *p, Node *fp) noexcept
Get sibling of node p whose parent is fp.
GenTdRbTree(const Compare &__cmp) noexcept
Constructor with comparator.
bool less(const Key &a, const Key &b) const noexcept
Returns true if a < b according to comparator.
Node * getRoot() const noexcept
Get const root pointer
GenTdRbTree(const GenTdRbTree &)=delete
Deleted copy constructor (use explicit copy if needed)
Compare cmp
Comparison functor.
bool equals(const Key &a, const Key &b) const noexcept
Returns true if a == b (neither a < b nor b < a)
void swap(GenTdRbTree &other) noexcept
Swap contents with another tree.
Node * insert(Node *p) noexcept
Insert a node into the tree.
static void findPredAndSwap(Node *p, Node *&fp) noexcept
Find predecessor and swap with node p for deletion.
static bool testNode(Node *p) noexcept
Test red-black properties for a single node.
bool verify() const noexcept
Alias for verify.
Top-down red-black tree with virtual destructor.
Top-down red-black tree without virtual destructor.
Strict weak ordering constraint for BST comparators.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Node * rotate_to_left(Node *p) noexcept
Rotate to the left the tree with root p
Node * rotate_to_right(Node *p) noexcept
Rotate to the right the tree with root p
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
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.
Red-Black tree node definition and validation utilities.
#define BLACK
Black color constant.
#define RED
Red color constant (newly inserted nodes are red)
Iterator over tree nodes in sorted order.
Iterator(const GenTdRbTree &t)
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.
Basic binary tree node definitions.