|
Aleph-w 3.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 |
| const Compare & | get_compare () const noexcept |
| Gen_Rb_Tree (Compare __cmp=Compare()) noexcept | |
| Default constructor with optional comparator. | |
| void | swap (Gen_Rb_Tree &tree) noexcept |
| Swaps all elements with another tree in constant time. | |
| Gen_Rb_Tree (Gen_Rb_Tree &&tree) noexcept | |
| Move constructor. | |
| Gen_Rb_Tree & | operator= (Gen_Rb_Tree &&tree) noexcept |
| Move assignment operator. | |
| virtual | ~Gen_Rb_Tree ()=default |
| Destructor. | |
| Node * | search (const Key &key) const noexcept |
| 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 |
| Insert a node into the tree. | |
| Node * | search_or_insert (Node *p) noexcept |
| Search for key or insert if not found. | |
| Node * | insert_dup (Node *p) noexcept |
| Insert a node allowing duplicates. | |
| bool | verify () const noexcept |
| Verify that tree satisfies red-black properties. | |
| Node * | remove (const Key &key) noexcept |
| Remove the node containing key. | |
Private Member Functions | |
| Node * | search_and_stack_rb (const Key &key, signed char &cmp_result) noexcept |
| Search for key and stack ancestors (optimistic duplicate detection) | |
| Node * | search_dup_and_stack_rb (const Key &key, signed char &cmp_result) noexcept |
| Search for insertion point (allows duplicates) and stack ancestors. | |
| void | fix_red_condition (Node *p) noexcept |
| Fix red-red violation after insertion. | |
| void | find_succ_and_swap (Node *p, Node *&pp) noexcept |
| Find successor and swap with node being deleted. | |
| void | fix_black_condition (Node *p) 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) | |
| FixedStack< Node * > | rb_stack |
| Stack for ancestor path. | |
| 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 five 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.
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 118 of file tpl_rb_tree.H.
| using Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::compare_type = Compare |
Definition at line 123 of file tpl_rb_tree.H.
| using Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::key_type = Key |
Definition at line 122 of file tpl_rb_tree.H.
| using Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::Node = NodeType<Key> |
Definition at line 121 of file tpl_rb_tree.H.
|
inlinenoexcept |
Default constructor with optional comparator.
Definition at line 423 of file tpl_rb_tree.H.
|
inlinenoexcept |
Move constructor.
Definition at line 441 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 266 of file tpl_rb_tree.H.
References COLOR, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::push(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::rb_stack, Aleph::RLINK(), and Aleph::FixedStack< T >::top().
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::remove().
|
inlineprivatenoexcept |
Fix black-height violation after deletion.
Definition at line 311 of file tpl_rb_tree.H.
References BLACK, COLOR, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::pop(), Aleph::FixedStack< T >::popn(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::rb_stack, RED, Aleph::RLINK(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root, Aleph::rotate_to_left(), Aleph::rotate_to_right(), and Aleph::FixedStack< T >::top().
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::remove().
|
inlineprivatenoexcept |
Fix red-red violation after insertion.
Definition at line 204 of file tpl_rb_tree.H.
References BLACK, COLOR, Aleph::FixedStack< T >::empty(), Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::pop(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::rb_stack, RED, Aleph::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 420 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp.
|
inlinenoexcept |
Definition at line 419 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp.
|
inlinenoexcept |
Get const root pointer.
Definition at line 491 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
|
inlinenoexcept |
Get reference to root pointer.
Definition at line 488 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
Referenced by write_rb().
|
inlineprivatenoexcept |
Initialize tree state.
Definition at line 408 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). |
Definition at line 511 of file tpl_rb_tree.H.
References 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::FixedStack< T >::empty(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::fix_red_condition(), KEY, Aleph::LLINK(), Aleph::maps(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::rb_stack, RED, Aleph::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(), and write_rb().
|
inlinenoexcept |
Insert a node allowing duplicates.
| p | Node to insert. |
Definition at line 579 of file tpl_rb_tree.H.
References 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, Aleph::LLINK(), Aleph::maps(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes, RED, Aleph::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 494 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
|
inlinenoexcept |
Definition at line 416 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp.
|
inlinenoexcept |
Returns a reference to the comparison criteria.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 415 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp.
|
inlinenoexcept |
Move assignment operator.
Definition at line 452 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. |
Definition at line 611 of file tpl_rb_tree.H.
References BLACK, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::CmpEqual, COLOR, Aleph::FixedStack< T >::empty(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::find_succ_and_swap(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::fix_black_condition(), Aleph::LLINK(), Aleph::maps(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::rb_stack, Aleph::RLINK(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::search_and_stack_rb(), and Aleph::FixedStack< T >::top().
|
inlinenoexcept |
Reset tree to empty state (does not free nodes)
Definition at line 500 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. |
Definition at line 472 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::cmp, KEY, Aleph::LLINK(), Aleph::RLINK(), and Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root.
Referenced by write_rb().
|
inlineprivatenoexcept |
Search for key and stack ancestors (optimistic duplicate detection)
Definition at line 137 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, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::popn(), Aleph::FixedStack< T >::push(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::rb_stack, Aleph::RLINK(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root, Aleph::FixedStack< T >::size(), and Aleph::FixedStack< T >::top().
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.
Definition at line 177 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, Aleph::LLINK(), Aleph::maps(), Aleph::FixedStack< T >::push(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::rb_stack, Aleph::RLINK(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::root, and Aleph::FixedStack< T >::top().
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. |
Definition at line 545 of file tpl_rb_tree.H.
References 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::FixedStack< T >::empty(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::fix_red_condition(), KEY, Aleph::LLINK(), Aleph::maps(), Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes, Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::rb_stack, RED, Aleph::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 497 of file tpl_rb_tree.H.
References Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::num_nodes.
|
inlinenoexcept |
Swaps all elements with another tree in constant time.
| [in] | tree | the red-black tree to swap with |
Definition at line 433 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 |
Verify that tree satisfies red-black properties.
Definition at line 604 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.
|
private |
Comparison functor.
Definition at line 130 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 133 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 134 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 132 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 127 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 126 of file tpl_rb_tree.H.
|
private |
Number of nodes in tree.
Definition at line 131 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 |
Stack for ancestor path.
Definition at line 129 of file tpl_rb_tree.H.
Referenced by Aleph::Gen_Rb_Tree< NodeType, Key, Compare >::find_succ_and_swap(), 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 >::insert(), 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().
|
private |
Reference to root (right child of head)
Definition at line 128 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().