52# ifndef TPL_TDRBTREE_H
53# define TPL_TDRBTREE_H
94template <
template <
class>
class NodeType,
typename Key,
95 class Compare = std::less<Key>>
113 bool less(
const Key& a,
const Key& b)
const noexcept
119 bool equals(
const Key& a,
const Key& b)
const noexcept
180 assert(p != Node::NullPtr);
191 assert(q != Node::NullPtr);
222 if (
LLINK(p) == Node::NullPtr)
228 if (
RLINK(p) == Node::NullPtr)
257 assert(q != Node::NullPtr);
285 if (
LLINK(p) == Node::NullPtr)
291 if (
RLINK(p) == Node::NullPtr)
441 assert(p != Node::NullPtr);
450 while (
LLINK(succ) != Node::NullPtr)
465 LLINK(p) = Node::NullPtr;
468 if (
RLINK(p) == succ)
496 assert(p != Node::NullPtr);
520 RLINK(p) = Node::NullPtr;
577 if (
LLINK(p) == Node::NullPtr)
583 if (
RLINK(p) == Node::NullPtr)
593 assert(p != Node::NullPtr);
597 while (
LLINK(p) != Node::NullPtr
or RLINK(p) != Node::NullPtr)
599 if (
RLINK(p) != Node::NullPtr)
601 else if (
LLINK(p) != Node::NullPtr)
644 other.root = Node::NullPtr;
656 other.root = Node::NullPtr;
671 root = Node::NullPtr;
702 assert(p != Node::NullPtr);
706 if (
root == Node::NullPtr)
723 assert(p != Node::NullPtr);
727 if (
root == Node::NullPtr)
744 assert(p != Node::NullPtr);
748 if (
root == Node::NullPtr)
757 if (
found !=
nullptr)
772 while (p != Node::NullPtr)
788 if (
root == Node::NullPtr)
813 if (p == Node::NullPtr)
833 if (p == Node::NullPtr)
850 if (p == Node::NullPtr)
864 if (
root == Node::NullPtr)
890template <
typename Key,
class Compare = std::less<Key>>
903template <
typename Key,
class Compare = std::less<Key>>
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.
__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
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
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.
DynList< T > maps(const C &c, Op op)
Classic map operation.
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.