124template <
template <
typename>
class NodeType,
typename Key,
class Compare>
125 requires StrictWeakOrder<Compare, Key>
152 signed char & cmp_result,
Path & path)
153 noexcept(nothrow_compare_v<Compare, Key>)
156 Node *candidate =
nullptr;
157 size_t candidate_pos = 0;
171 candidate_pos = path.size();
176 while (p != Node::NullPtr);
180 if (candidate !=
nullptr and not
static_cast<bool>(
cmp(
KEY(candidate), key))) [[unlikely]]
184 const size_t to_pop =
185 path.size() > candidate_pos ?
186 path.size() - candidate_pos : 0;
198 signed char & cmp_result,
Path & path)
199 noexcept(nothrow_compare_v<Compare, Key>)
207 const Key & pk =
KEY(p);
219 while (p != Node::NullPtr);
231 Node *pp = path.pop();
241 Node *ppp = path.pop();
253 Node *pppp = path.pop();
287 Node *& ref_path_top = path.top();
293 while (
LLINK(succ) != Node::NullPtr)
309 LLINK(p) = Node::NullPtr;
311 if (
RLINK(p) == succ)
338 Node *pp = path.popn(2);
342 assert(
LLINK(path.top()) == pp or
RLINK(path.top()) == pp);
347 Node *& ppp = path.top();
378 Node *ppp = path.top();
394 Node *ppp = path.top();
396 if (
LLINK(sp) == snp)
455 noexcept(
std::is_nothrow_default_constructible_v<
Node>
and
456 std::is_nothrow_default_constructible_v<Compare>
and
457 std::is_nothrow_constructible_v<Compare, Compare &>)
472 noexcept(std::is_nothrow_default_constructible_v<Node>
and
473 std::is_nothrow_constructible_v<Compare, Compare &>)
489 std::swap(
cmp, tree.cmp);
490 std::swap(
root, tree.root);
512 noexcept(bin_node_detail::nothrow_header_from_v<Node>
and
513 bin_node_detail::nothrow_compare_taken_v<Compare>)
516 cmp(bin_node_detail::compare_to_take(tree.
cmp)),
520 tree.root = Node::NullPtr;
532 noexcept(std::is_nothrow_move_assignable_v<Compare>)
537 cmp = std::move(tree.cmp);
540 tree.root = Node::NullPtr;
557 Node *
search(
const Key & key)
const noexcept(nothrow_compare_v<Compare, Key>)
560 while (p != Node::NullPtr)
564 else if (
cmp(
KEY(p), key))
587 root = Node::NullPtr;
602 assert(p !=
nullptr and p != Node::NullPtr);
605 if (
root == Node::NullPtr)
638 assert(p !=
nullptr and p != Node::NullPtr);
641 if (
root == Node::NullPtr)
674 assert(p !=
nullptr and p != Node::NullPtr);
677 if (
root == Node::NullPtr)
710 Node *
remove(
const Key & key)
noexcept(nothrow_compare_v<Compare, Key>)
712 if (
root == Node::NullPtr)
725 if (
LLINK(q) == Node::NullPtr)
734 if (
RLINK(q) == Node::NullPtr)
777template <
typename Key,
class Compare = Aleph::less<Key>>
778 requires StrictWeakOrder<Compare, Key>
792template <
typename Key,
class Compare = Aleph::less<Key>>
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Standard functor implementations and comparison objects.
Inorder iterator on the nodes of a binary tree.
Red-black binary search tree implementation (bottom-up).
const Compare & get_compare() const noexcept
Node * insert_dup(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node allowing duplicates.
void reset() noexcept
Reset tree to empty state (does not free nodes)
void fix_red_condition(Node *p, Path &path) noexcept
Fix red-red violation after insertion.
Node * search_dup_and_stack_rb(const Key &key, signed char &cmp_result, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
Search for insertion point (allows duplicates) and stack ancestors.
Node * search(const Key &key) const noexcept(nothrow_compare_v< Compare, Key >)
Search for a key in the tree.
void fix_black_condition(Node *p, Path &path) noexcept
Fix black-height violation after deletion.
void find_succ_and_swap(Node *p, Node *&pp, Path &path) noexcept
Find successor and swap with node being deleted.
Gen_Rb_Tree(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 * remove(const Key &key) noexcept(nothrow_compare_v< Compare, Key >)
Remove the node containing key.
const Compare & key_comp() const noexcept
size_t num_nodes
Number of nodes in tree.
Gen_Rb_Tree(Gen_Rb_Tree &&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.
Node * search_and_stack_rb(const Key &key, signed char &cmp_result, Path &path) noexcept(nothrow_compare_v< Compare, Key >)
Search for key and stack ancestors (optimistic duplicate detection).
Node *& root
Reference to root (right child of head)
Compare cmp
Comparison functor.
void swap(Gen_Rb_Tree &tree) noexcept(std::is_nothrow_swappable_v< Compare >)
Swaps all elements with another tree in constant time.
Node * getRoot() const noexcept
Get const root pointer.
virtual ~Gen_Rb_Tree()=default
Destructor.
size_t size() const noexcept
Returns the number of nodes in the tree (O(1))
Node * head
Pointer to sentinel.
Node * insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Insert a node into the tree.
Node head_node
Sentinel header node.
Compare & get_compare() noexcept
See key_comp().
bool verify() const
Verify that tree satisfies red-black properties and that its keys are in order.
Node *& getRoot() noexcept
Get reference to root pointer.
Gen_Rb_Tree & operator=(Gen_Rb_Tree &&tree) noexcept(std::is_nothrow_move_assignable_v< Compare >)
Takes the nodes and the comparison criteria of tree, which is left empty.
bool is_empty() const noexcept
Returns true if the tree is empty.
Compare & key_comp() noexcept
Returns a reference to the comparison criteria.
static constexpr signed char CmpEqual
Gen_Rb_Tree() 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.
void init() noexcept
Initialize tree state.
static constexpr signed char CmpLess
static constexpr signed char CmpGreater
Node * search_or_insert(Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Search for key or insert if not found.
Stack of fixed capacity whose storage is inside the object.
T & top() noexcept
Return a modifiable reference to the top; the stack must not be empty.
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
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)
Iterator over tree nodes in sorted order.
Iterator() noexcept=default
Default constructor creates an "end" iterator.
Iterator(const Gen_Rb_Tree &t)
Red-black tree with virtual destructor in nodes.
Red-black tree with nodes without virtual destructor.
Stack implementations backed by dynamic or fixed arrays.
Utility functions for binary tree operations.