45# ifndef TPL_HTDRBTREE_H
46# define TPL_HTDRBTREE_H
83 template <
class Key,
class Compare = Aleph::less<Key>>
84 requires StrictWeakOrder<Compare, Key>
156 assert(
LLINK(fffp) == ffp ||
RLINK(fffp) == ffp);
218 noexcept(nothrow_compare_v<Compare, Key>)
225 const Key & qk =
KEY(q);
235 const Key & pk =
KEY(p);
237 if (are_equals<Key, Compare>(qk, pk,
cmp)) [[unlikely]]
271 const Key & pk =
KEY(p);
289 noexcept(nothrow_compare_v<Compare, Key>)
296 const Key & qk =
KEY(q);
306 const Key & pk =
KEY(p);
338 const Key & pk =
KEY(p);
369 noexcept(nothrow_compare_v<Compare, Key>)
380 const Key & pk =
KEY(p);
382 if (are_equals<Key, Compare>(key, pk,
cmp))
438 Node *& refToSearchPath = path.top();
440 assert(refToSearchPath == p);
461 refToSearchPath = succ;
475 if (
RLINK(p) == succ)
518 assert(!path.is_empty());
522 Node *& ffp = path.top();
564 assert(!path.is_empty());
566 Node *ffp = path.top();
599 assert(!path.is_empty());
601 Node *ffp = path.top();
607 if (
LLINK(sp) == snp)
664 assert(path.top() == q);
668 Node *fq = path.top(1);
791 [[nodiscard]]
constexpr const Compare &
get_compare() const noexcept {
return cmp; }
807 noexcept(
std::is_nothrow_default_constructible_v<
Node>
and
808 std::is_nothrow_default_constructible_v<Compare>
and
809 std::is_nothrow_constructible_v<Compare, Compare &>)
824 noexcept(std::is_nothrow_default_constructible_v<Node>
and
825 std::is_nothrow_constructible_v<Compare, Compare &>)
851 std::swap(
cmp, tree.cmp);
852 std::swap(
root, tree.root);
874 noexcept(bin_node_detail::nothrow_header_from_v<Node>
and
875 bin_node_detail::nothrow_compare_taken_v<Compare>)
884 cmp(bin_node_detail::compare_to_take(tree.
cmp))
982 const Key & pk =
KEY(p);
984 if (found !=
nullptr) [[unlikely]]
1025 Node *
search(
const Key & key)
const noexcept(nothrow_compare_v<Compare, Key>)
1040 Node *
remove(
const Key & key)
noexcept(nothrow_compare_v<Compare, Key>)
1049 if (no_equals<Key, Compare>(
KEY(p), key,
cmp))
1111 template <
class Key,
class Compare = Aleph::less<Key>>
1112 requires StrictWeakOrder<Compare, Key>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Inorder iterator on the nodes of a binary tree.
Hybrid red-black tree with virtual node destructor.
Hybrid top-down/bottom-up red-black tree.
HtdRbTree(const HtdRbTree &)=delete
Copy constructor deleted.
Compare cmp
Comparison functor.
static void colorParentAndSibling(Node *fp, Node *sp) noexcept
Recolor parent and sibling to fix violation.
Node * fHead
Pointer to head's parent.
Node * getRoot() const noexcept
Get root pointer (const)
Node * searchAndBuildPath(const Key &key, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
Search for key while building path stack.
static Node * getSibling(Node *p, Node *fp) noexcept
Returns sibling of p given its parent fp.
static void flipColors(Node *p) noexcept
Flip colors of a black node with two red children.
void balanceDownAndColor(Node *p, Node *&fp, Node *&sp, Path &path) noexcept
Balance down a violating black node.
void doubleRotateNephewAndColor(Node *fp, Node *sp, Node *snp, Path &path) noexcept
Double rotation and recolor to fix violation.
HtdRbTree(HtdRbTree &&tree) 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 tree, which is left empty and can take nodes again.
unsigned char Color
Color type for red-black nodes.
Node *& root
Reference to root (right child of head)
void removeAndFixBlackCondition(Node *q, Path &path) noexcept
Remove node and fix any black-height violations.
void restoreRedCondition(Node *p, Node *&fp, Node *ffp, Node *fffp)
Restore red-black condition after insertion.
Key key_type
The key type stored in nodes.
void rotateNephewAndColor(Node *fp, Node *sp, Node *np, Path &path) noexcept
Rotate nephew up and recolor to fix violation.
Compare & key_comp() noexcept
Returns a reference to the comparison functor.
Node * searchFlipColorsAndInsert(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Search for insertion point with proactive color flipping.
size_t node_count
Number of nodes in tree.
static void colorSiblingAsRed(Node *sp) noexcept
Color sibling red to propagate violation upward.
Node *& getRoot() noexcept
Get reference to root pointer.
Node * head
Pointer to head node.
void reset() noexcept
Reset tree (does not free nodes)
HtdRbTree & operator=(const HtdRbTree &)=delete
Copy assignment deleted.
HtdRbTree & operator=(HtdRbTree &&tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps the nodes and the comparison criteria with tree.
Compare & get_compare() noexcept
Alias for key_comp()
Node headParent
Auxiliary node, grandparent of root.
Node headNode
Auxiliary node, parent of root.
void findSuccAndSwap(Node *p, Node *&fp, Path &path) noexcept
Find successor and swap with node to delete.
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert allowing duplicate keys.
HtdRbTree(Compare __cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with the comparison criteria __cmp.
Node * searchFlipColorsAndInsertDup(Node *q) noexcept(nothrow_compare_v< Compare, Key >)
Like searchFlipColorsAndInsert but allows duplicate keys; as it, a comparison that throws leaves a va...
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search for a key.
RbNode< Key > Node
The node type used by this tree.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node into the tree.
constexpr const Compare & get_compare() const noexcept
bool verifyRedBlack() const noexcept
Verify the red-black properties of every node.
void swap(HtdRbTree &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps in constant time the nodes and the comparison criteria of this and tree.
constexpr size_t size() const noexcept
Get number of nodes in tree.
bool verify() const
Verify tree is a valid red-black BST.
Node * ffHead
Pointer to head's grandparent.
Node headGrandParent
Auxiliary node, great-grandparent of root.
HtdRbTree() noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_default_constructible_v< Compare > and std::is_nothrow_constructible_v< Compare, Compare & >)
Builds an empty tree with a comparison criteria built by default.
constexpr bool is_empty() const noexcept
Check if tree is empty.
virtual ~HtdRbTree()=default
Virtual destructor.
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search for key or insert if not found.
Node * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove node with given key.
const Compare & key_comp() const noexcept
Stack of fixed capacity whose storage is inside the object.
Declare RbNode type with 128-byte pool allocation.
static RbNode *const NullPtr
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
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
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept(nothrow_compare_v< Compare, typename Node::key_type >)
Search a key in a binary search tree using optimistic search.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Red-Black tree node definition and validation utilities.
bool is_red_black_bst(Node *node, Compare &cmp)
Verify that tree is both a valid RB tree and valid BST.
#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(HtdRbTree &t) noexcept
Iterator(const HtdRbTree &t) noexcept
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.
Basic binary tree node definitions.