|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Red-black binary search tree implementation (bottom-up). More...
#include <tpl_rb_tree.H>
Classes | |
| struct | Iterator |
| Iterator over tree nodes in sorted order. More... | |
Public Types | |
| using | Node = NodeType< Key > |
| using | key_type = Key |
| using | compare_type = Compare |
Public Member Functions | |
| Compare & | key_comp () noexcept |
| Returns a reference to the comparison criteria. | |
| const Compare & | key_comp () const noexcept |
| Compare & | get_compare () noexcept |
See key_comp(). | |
| const Compare & | get_compare () const noexcept |
| 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. | |
| 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. | |
| void | swap (Gen_Rb_Tree &tree) noexcept(std::is_nothrow_swappable_v< Compare >) |
| Swaps all elements with another tree in constant time. | |
| 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. | |
| 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. | |
| virtual | ~Gen_Rb_Tree ()=default |
| Destructor. | |
| Node * | search (const Key &key) const noexcept(nothrow_compare_v< Compare, Key >) |
| Search for a key in the tree. | |
| Node *& | getRoot () noexcept |
| Get reference to root pointer. | |
| Node * | getRoot () const noexcept |
| Get const root pointer. | |
| bool | is_empty () const noexcept |
| Returns true if the tree is empty. | |
| size_t | size () const noexcept |
| Returns the number of nodes in the tree (O(1)) | |
| void | reset () noexcept |
| Reset tree to empty state (does not free nodes) | |
| Node * | insert (Node *p) noexcept(nothrow_compare_v< Compare, Key >) |
| Insert a node into the tree. | |
| Node * | search_or_insert (Node *p) noexcept(nothrow_compare_v< Compare, Key >) |
| Search for key or insert if not found. | |
| Node * | insert_dup (Node *p) noexcept(nothrow_compare_v< Compare, Key >) |
| Insert a node allowing duplicates. | |
| bool | verify () const |
| Verify that tree satisfies red-black properties and that its keys are in order. | |
| Node * | remove (const Key &key) noexcept(nothrow_compare_v< Compare, Key >) |
| Remove the node containing key. | |
Private Types | |
| using | Path = InlineStack< Node *, Node::MaxHeight+1 > |
Private Member Functions | |
| 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 * | 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. | |
| void | fix_red_condition (Node *p, Path &path) noexcept |
| Fix red-red violation after insertion. | |
| void | find_succ_and_swap (Node *p, Node *&pp, Path &path) noexcept |
| Find successor and swap with node being deleted. | |
| void | fix_black_condition (Node *p, Path &path) noexcept |
| Fix black-height violation after deletion. | |
| void | init () noexcept |
| Initialize tree state. | |
Private Attributes | |
| Node | head_node |
| Sentinel header node. | |
| Node * | head |
| Pointer to sentinel. | |
| Node *& | root |
| Reference to root (right child of head) | |
| Compare | cmp |
| Comparison functor. | |
| size_t | num_nodes |
| Number of nodes in tree. | |
Static Private Attributes | |
| static constexpr signed char | CmpLess = -1 |
| static constexpr signed char | CmpEqual = 0 |
| static constexpr signed char | CmpGreater = 1 |
Red-black binary search tree implementation (bottom-up).
A red-black tree is a self-balancing binary search tree where each node has a color (red or black). The tree maintains balance through four invariants that guarantee the height is at most 2*logâ‚‚(n+1), ensuring O(log n) worst-case time for all operations.
These properties ensure the tree remains approximately balanced, with the longest path at most twice the length of the shortest path.
The usual definition adds that the root is black. This tree does not force it, and neither do the other red-black trees of the library: the root can be red, as the only node of a tree is. A red root has black children, so the bound on the height is the same, and verify() accepts it.
This is the bottom-up implementation that uses a stack to track ancestors during descent and repairs violations by ascending back to the root.
| NodeType | Node template (typically RbNode or RbNodeVtl). |
| Key | The type of keys stored in the tree. |
| Compare | Comparison functor for ordering keys. |
Definition at line 126 of file tpl_rb_tree.H.
| using Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::compare_type = Compare |
Definition at line 131 of file tpl_rb_tree.H.
| using Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::key_type = Key |
Definition at line 130 of file tpl_rb_tree.H.
| using Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::Node = NodeType<Key> |
Definition at line 129 of file tpl_rb_tree.H.
|
private |
Definition at line 147 of file tpl_rb_tree.H.
|
inlinenoexcept |
Builds an empty tree with a comparison criteria built by default.
It is a constructor apart from the one that takes the comparison, and not a default argument of that one: GCC and Clang do not count a default argument in the noexcept of the implicit default constructor of a tree that inherits these, and what the construction of the comparison threw would end the program there.
| what | the default construction of the key of a header node or of the comparison, or the copy of the comparison, throws; nothing when they are noexcept. |
Definition at line 454 of file tpl_rb_tree.H.
|
inlinenoexcept |
Builds an empty tree with the comparison criteria __cmp.
| [in] | __cmp | the comparison criteria, which the tree copies. |
| what | the default construction of the key of a header node or the copy of __cmp throws; nothing when both are noexcept. The copy is from a Compare that is not const, which a comparison may have apart from the one from a const one. |
Definition at line 471 of file tpl_rb_tree.H.
|
inlinenoexcept |
Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.
It builds no key, if it can: the key of the header node is built by default when that cannot throw; else it is that of the header node of tree, moved, when the move of a key cannot throw (see bin_node_detail::header_from()).
| [in,out] | tree | the tree to move from. It keeps its comparison criteria, which are copied: from a const one, or from one that is not const, if they have only that copy. Criteria that cannot be copied are moved, and tree keeps criteria that were moved from. |
| what | the copy of the comparison criteria throws, or their move if they cannot be copied; and what the default construction of the key of the header node throws, with a key whose move may throw too. Then tree keeps its nodes. Nothing when they are noexcept. |
Definition at line 511 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
|
virtualdefault |
Destructor.
|
inlineprivatenoexcept |
Find successor and swap with node being deleted.
Definition at line 285 of file tpl_rb_tree.H.
References COLOR, LLINK, and RLINK.
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::remove().
|
inlineprivatenoexcept |
Fix black-height violation after deletion.
Definition at line 330 of file tpl_rb_tree.H.
References BLACK, COLOR, LLINK, RED, RLINK, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root, Aleph::rotate_to_left(), and Aleph::rotate_to_right().
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::remove().
|
inlineprivatenoexcept |
Fix red-red violation after insertion.
Definition at line 225 of file tpl_rb_tree.H.
References Aleph::and, BLACK, COLOR, LLINK, RED, RLINK, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root, Aleph::rotate_to_left(), and Aleph::rotate_to_right().
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert_dup(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_or_insert().
|
inlinenoexcept |
Definition at line 439 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp.
|
inlinenoexcept |
See key_comp().
Definition at line 438 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp.
|
inlinenoexcept |
Get const root pointer.
Definition at line 576 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
|
inlinenoexcept |
Get reference to root pointer.
Definition at line 573 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
Referenced by main(), and write_rb().
|
inlineprivatenoexcept |
Initialize tree state.
Definition at line 427 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes.
|
inlinenoexcept |
Insert a node into the tree.
| p | Node to insert (must be red with null children). |
| anything | that the comparison throws; the tree changes only after the last comparison, so it is left as it was, without p. |
noexcept only if the comparison cannot throw, so that what it throws reaches the caller. Definition at line 600 of file tpl_rb_tree.H.
References Aleph::and, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpEqual, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpGreater, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpLess, COLOR, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::fix_red_condition(), KEY, LLINK, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes, RED, RLINK, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root, and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_and_stack_rb().
Referenced by RbTreeTest::insert_values(), main(), TEST(), and write_rb().
|
inlinenoexcept |
Insert a node allowing duplicates.
| p | Node to insert. |
| anything | that the comparison throws; the tree changes only after the last comparison, so it is left as it was, without p. |
noexcept only if the comparison cannot throw, so that what it throws reaches the caller. Definition at line 672 of file tpl_rb_tree.H.
References Aleph::and, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpEqual, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpLess, COLOR, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::fix_red_condition(), KEY, LLINK, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes, RED, RLINK, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root, and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_dup_and_stack_rb().
|
inlinenoexcept |
Returns true if the tree is empty.
Definition at line 579 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
Referenced by TEST().
|
inlinenoexcept |
Definition at line 435 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp.
|
inlinenoexcept |
Returns a reference to the comparison criteria.
Definition at line 434 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp.
|
inlinenoexcept |
Takes the nodes and the comparison criteria of tree, which is left empty.
| [in,out] | tree | the tree to move from. |
*this. | what | the move assignment of the comparison criteria throws, which goes first: then the nodes stay where they were. |
Definition at line 531 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes, and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
|
inlinenoexcept |
Remove the node containing key.
| key | Key to remove. |
| anything | that the comparison throws; the tree changes only after the last comparison, so it is left as it was. |
noexcept only if the comparison cannot throw, so that what it throws reaches the caller. Definition at line 710 of file tpl_rb_tree.H.
References BLACK, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpEqual, COLOR, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::find_succ_and_swap(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::fix_black_condition(), LLINK, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes, RLINK, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_and_stack_rb(), and Aleph::InlineStack< T, N >::top().
Referenced by TEST().
|
inlinenoexcept |
Reset tree to empty state (does not free nodes)
Definition at line 585 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes, and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
|
inlinenoexcept |
Search for a key in the tree.
| key | Key to search for. |
| anything | that the comparison throws; the search does not modify the tree, so it is left as it was. |
noexcept only if the comparison cannot throw, so that what it throws reaches the caller. Definition at line 557 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp, KEY, LLINK, RLINK, and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
Referenced by main(), TEST(), and write_rb().
|
inlineprivatenoexcept |
Search for key and stack ancestors (optimistic duplicate detection).
It only reads the tree: a comparison that throws leaves it as it was.
Definition at line 151 of file tpl_rb_tree.H.
References Aleph::and, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpEqual, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpGreater, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpLess, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::head, KEY, LLINK, RLINK, and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::remove(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_or_insert().
|
inlineprivatenoexcept |
Search for insertion point (allows duplicates) and stack ancestors.
It only reads the tree: a comparison that throws leaves it as it was.
Definition at line 197 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpEqual, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpGreater, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpLess, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::head, KEY, LLINK, RLINK, and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert_dup().
|
inlinenoexcept |
Search for key or insert if not found.
| p | Node to search or insert. |
| anything | that the comparison throws; the tree changes only after the last comparison, so it is left as it was, without p. |
noexcept only if the comparison cannot throw, so that what it throws reaches the caller. Definition at line 636 of file tpl_rb_tree.H.
References Aleph::and, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpEqual, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpGreater, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpLess, COLOR, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::fix_red_condition(), KEY, LLINK, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes, RED, RLINK, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root, and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_and_stack_rb().
|
inlinenoexcept |
Returns the number of nodes in the tree (O(1))
Definition at line 582 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes.
Referenced by TEST().
|
inlinenoexcept |
Swaps all elements with another tree in constant time.
| [in] | tree | the red-black tree to swap with |
| what | the swap of the comparison criteria throws, which goes first: then the nodes stay where they were. |
Definition at line 485 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes, and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
|
inline |
Verify that tree satisfies red-black properties and that its keys are in order.
It throws what the comparator throws.
Definition at line 699 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp, is_red_black_bst(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
Referenced by TEST().
|
private |
Comparison functor.
Definition at line 137 of file tpl_rb_tree.H.
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::get_compare(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::get_compare(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::key_comp(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::key_comp(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::operator=(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_and_stack_rb(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_dup_and_stack_rb(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::swap(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::verify().
|
staticconstexprprivate |
Definition at line 140 of file tpl_rb_tree.H.
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::remove(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_and_stack_rb(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_dup_and_stack_rb(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_or_insert().
|
staticconstexprprivate |
Definition at line 141 of file tpl_rb_tree.H.
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_and_stack_rb(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_dup_and_stack_rb(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_or_insert().
|
staticconstexprprivate |
Definition at line 139 of file tpl_rb_tree.H.
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_and_stack_rb(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_dup_and_stack_rb(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_or_insert().
|
private |
Pointer to sentinel.
Definition at line 135 of file tpl_rb_tree.H.
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_and_stack_rb(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_dup_and_stack_rb().
|
private |
Sentinel header node.
Definition at line 134 of file tpl_rb_tree.H.
|
private |
Number of nodes in tree.
Definition at line 138 of file tpl_rb_tree.H.
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::init(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::operator=(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::remove(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::reset(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_or_insert(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::size(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::swap().
|
private |
Reference to root (right child of head)
Definition at line 136 of file tpl_rb_tree.H.
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::Gen_Rb_Tree(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::fix_black_condition(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::fix_red_condition(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::getRoot(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::getRoot(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::is_empty(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::operator=(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::remove(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::reset(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_and_stack_rb(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_dup_and_stack_rb(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_or_insert(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::swap(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::verify().