52# ifndef TPL_TDRBTREE_H
53# define TPL_TDRBTREE_H
95template <
template <
class>
class NodeType,
typename Key,
97 requires StrictWeakOrder<Compare, Key>
116 bool less(
const Key& a,
const Key& b)
const
117 noexcept(nothrow_compare_v<Compare, Key>)
124 bool equals(
const Key& a,
const Key& b)
const
125 noexcept(nothrow_compare_v<Compare, Key>)
127 return not
static_cast<bool>(
cmp(a, b))
and not
static_cast<bool>(
cmp(b, a));
152 assert(
LLINK(fffp) == ffp or
RLINK(fffp) == ffp);
186 assert(p != Node::NullPtr);
199 noexcept(nothrow_compare_v<Compare, Key>)
201 assert(q != Node::NullPtr);
202 assert(
root != Node::NullPtr);
204 assert(
LLINK(q) == Node::NullPtr
and RLINK(q) == Node::NullPtr);
209 Node *fffp = Node::NullPtr;
224 assert(fffp != Node::NullPtr);
232 if (
LLINK(p) == Node::NullPtr)
238 if (
RLINK(p) == Node::NullPtr)
268 noexcept(nothrow_compare_v<Compare, Key>)
270 assert(q != Node::NullPtr);
271 assert(
root != Node::NullPtr);
273 assert(
LLINK(q) == Node::NullPtr
and RLINK(q) == Node::NullPtr);
278 Node *fffp = Node::NullPtr;
290 assert(fffp != Node::NullPtr);
298 if (
LLINK(p) == Node::NullPtr)
304 if (
RLINK(p) == Node::NullPtr)
331 assert(fp != Node::NullPtr);
332 assert(ffp != Node::NullPtr);
334 assert(
LLINK(fp) != Node::NullPtr);
393 assert(fp != Node::NullPtr);
394 assert(ffp != Node::NullPtr);
396 assert(
RLINK(fp) != Node::NullPtr);
454 assert(p != Node::NullPtr);
455 assert(
RLINK(p) != Node::NullPtr);
456 assert(fp != Node::NullPtr);
463 while (
LLINK(succ) != Node::NullPtr)
478 LLINK(p) = Node::NullPtr;
481 if (
RLINK(p) == succ)
509 assert(p != Node::NullPtr);
510 assert(
LLINK(p) != Node::NullPtr);
511 assert(fp != Node::NullPtr);
533 RLINK(p) = Node::NullPtr;
575 noexcept(nothrow_compare_v<Compare, Key>)
593 if (
LLINK(p) == Node::NullPtr)
599 if (
RLINK(p) == Node::NullPtr)
609 assert(p != Node::NullPtr);
610 assert(fp != Node::NullPtr);
613 while (
LLINK(p) != Node::NullPtr or
RLINK(p) != Node::NullPtr)
615 if (
RLINK(p) != Node::NullPtr)
617 else if (
LLINK(p) != Node::NullPtr)
622 LLINK(fp) = Node::NullPtr;
624 RLINK(fp) = Node::NullPtr;
644 noexcept(
std::is_nothrow_default_constructible_v<
Node>
and
645 std::is_nothrow_default_constructible_v<Compare>)
658 noexcept(std::is_nothrow_default_constructible_v<Node>
and
659 std::is_nothrow_copy_constructible_v<Compare>)
683 noexcept(bin_node_detail::nothrow_header_from_v<Node>
and
684 bin_node_detail::nothrow_compare_taken_v<Compare>)
694 other.root = Node::NullPtr;
706 noexcept(std::is_nothrow_move_assignable_v<Compare>)
711 cmp = std::move(other.cmp);
714 other.root = Node::NullPtr;
729 root = Node::NullPtr;
744 std::swap(
cmp, other.cmp);
745 std::swap(
root, other.root);
774 assert(p != Node::NullPtr);
776 assert(
LLINK(p) == Node::NullPtr
and RLINK(p) == Node::NullPtr);
778 if (
root == Node::NullPtr)
800 assert(p != Node::NullPtr);
802 assert(
LLINK(p) == Node::NullPtr
and RLINK(p) == Node::NullPtr);
804 if (
root == Node::NullPtr)
826 assert(p != Node::NullPtr);
828 assert(
LLINK(p) == Node::NullPtr
and RLINK(p) == Node::NullPtr);
830 if (
root == Node::NullPtr)
839 if (found !=
nullptr)
855 Node*
search(
const Key& key)
const noexcept(nothrow_compare_v<Compare, Key>)
858 while (p != Node::NullPtr)
878 Node*
remove(
const Key& key)
noexcept(nothrow_compare_v<Compare, Key>)
880 if (
root == Node::NullPtr)
943template <
typename Key,
class Compare = Aleph::less<Key>>
944 requires StrictWeakOrder<Compare, Key>
957template <
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.
Top-down red-black binary search tree implementation.
Node * searchFlipColorsAndInsert(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Search for insertion point, flipping colors proactively to maintain balance.
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.
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.
bool is_empty() const noexcept
Check if tree is empty.
Node * searchFlipColorsAndInsertDup(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Search for insertion point 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)
static void flipColors(Node *p) noexcept
Flip colors: black parent with two red children becomes red with two black children.
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.
GenTdRbTree(GenTdRbTree &&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.
static Node * gotoLeftAndColorRed(Node *fp, Node *&ffp) noexcept
Descend to left child, ensuring current node is red for deletion.
GenTdRbTree(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.
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=(const GenTdRbTree &)=delete
Deleted copy assignment.
GenTdRbTree & operator=(GenTdRbTree &&other) noexcept(std::is_nothrow_move_assignable_v< Compare >)
Takes the nodes and the comparison criteria of other, which is left empty.
static void findSuccAndSwap(Node *p, Node *&fp) noexcept
Find successor and swap with node p for deletion.
bool verify() const
Verify the red-black properties and the order of the keys.
static Node * gotoRightAndColorRed(Node *fp, Node *&ffp) noexcept
Descend to right child, ensuring current node is red for deletion.
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node, allowing duplicates.
Node headParent
Sentinel grandparent node.
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.
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.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search for a key in the tree.
Node * getRoot() const noexcept
Get const root pointer
Node * searchAndColorRed(const Key &key, Node *&fp) noexcept(nothrow_compare_v< Compare, Key >)
Search for key while coloring nodes red for deletion.
GenTdRbTree(const GenTdRbTree &)=delete
Deleted copy constructor (use explicit copy if needed)
Compare cmp
Comparison functor.
static void findPredAndSwap(Node *p, Node *&fp) noexcept
Find predecessor and swap with node p for deletion.
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove and return node with given key.
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search for key or insert if not found.
void swap(GenTdRbTree &other) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and other.
GenTdRbTree() 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.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node into the tree.
Top-down red-black tree with virtual destructor.
Top-down red-black tree without virtual destructor.
Strict weak ordering constraint for BST comparators.
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
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Red-Black tree node definition and validation utilities.
#define BLACK
Black color constant.
#define RED
Red color constant (newly inserted nodes are red)
bool is_red_black_tree(Node *node)
Test RB properties for entire subtree.
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.