|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Top-down splay tree with rank support. More...
#include <tpl_splay_treeRk.H>
Classes | |
| class | Iterator |
| Inorder iterator over the extended splay tree. More... | |
Public Types | |
| typedef NodeType< Key > | Node |
| typedef Key | key_type |
| The key type stored in the node. | |
Public Member Functions | |
| Compare & | key_comp () |
| Returns a reference to the comparison functor. | |
| Compare & | get_compare () |
See key_comp(). | |
| void | splay (const Key &key) noexcept |
Search key within the tree and splay that node; if key is not found, the last node of the search is splayed. | |
| GenTdSplayTreeRk () noexcept(std::is_nothrow_default_constructible_v< Compare >) | |
| Builds an empty tree with a comparison criteria built by default. | |
| GenTdSplayTreeRk (Compare __cmp) noexcept(std::is_nothrow_constructible_v< Compare, Compare & >) | |
Builds an empty tree with the comparison criteria __cmp. | |
| GenTdSplayTreeRk (const GenTdSplayTreeRk &)=delete | |
| Not copyable: the tree does not own its nodes, and a copy would hold the same ones. | |
| GenTdSplayTreeRk & | operator= (const GenTdSplayTreeRk &)=delete |
| Not copyable, for the same reason as the copy constructor. | |
| GenTdSplayTreeRk (GenTdSplayTreeRk &&tree) noexcept(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. | |
| void | swap (GenTdSplayTreeRk &tree) noexcept(std::is_nothrow_swappable_v< Compare >) |
| Exchange in O(1) the nodes and the comparison criteria of this tree and another. | |
| GenTdSplayTreeRk & | operator= (GenTdSplayTreeRk &&tree) noexcept(noexcept(swap(tree))) |
| Exchanges this tree with another, which is left with the nodes and the comparison criteria that this one had. | |
| virtual | ~GenTdSplayTreeRk () |
| Destructor. | |
| Node * | insert (Node *p) |
| Inserts a node in a top down splay tree. | |
| Node * | insert_dup (Node *p) |
| Inserts a node in a top down splay tree, even if its key is already in the tree. | |
| Node * | search (const Key &key) |
| Searches a key in a top down splay tree. | |
| Node * | search_or_insert (Node *p) |
Searches the key of p and, if it is not in the tree, inserts p. | |
| Node * | remove (const Key &key) |
| Remove a key from a top-down splay tree. | |
| Node *& | getRoot () |
| Get the top-down splay tree's root. | |
| bool | verify () const |
| size_t | size () const |
| Returns the number of nodes stored in the tree. | |
| bool | is_empty () const |
| Returns true if the tree is empty. | |
| std::pair< long, Node * > | position (const Key &key) |
| Returns the inorder (sorted) position of key. | |
| std::pair< long, Node * > | find_position (const Key &key) |
| Returns the inorder (sorted) position of key. | |
| Node * | select (const size_t &i) |
| Returns the node whose inorder position in the extended tree is i. | |
Private Member Functions | |
| signed char | splay_impl (const Key &key) noexcept |
| Node * | __insert (Node *p) |
| Node * | splay_max (Node *r) noexcept |
| Splay the maximum element to the root. | |
Private Attributes | |
| Node * | root |
| Compare | cmp |
Static Private Attributes | |
| static constexpr signed char | CmpLess = -1 |
| static constexpr signed char | CmpEqual = 0 |
| static constexpr signed char | CmpGreater = 1 |
Top-down splay tree with rank support.
A splay tree is a self-adjusting binary search tree that moves recently accessed elements closer to the root. This provides amortized O(log n) time complexity for all operations, with frequently accessed elements having faster access times.
This implementation extends the basic splay tree with COUNT fields in each node, enabling:
The splay operation brings the accessed node to the root using a sequence of zig, zig-zig, and zig-zag rotations. This top-down variant performs splaying during the descent, avoiding a second pass.
| NodeType | Template for node type (typically BinNodeXt). |
| Key | The type of keys stored in the tree. |
| Compare | Comparison functor for ordering keys. |
noexcept, and if it throws the program ends (std::terminate()). A splay relinks the tree as it compares, so a comparison that threw in the middle would leave nodes hanging from a local header, out of the tree. Key throw: that local header is a whole node, whose key each splay builds by default inside noexcept. In a DynMapTree, whose keys are pairs, that is the default construction of its Data.Definition at line 120 of file tpl_splay_treeRk.H.
| typedef Key GenTdSplayTreeRk< NodeType, Key, Compare >::key_type |
The key type stored in the node.
Definition at line 220 of file tpl_splay_treeRk.H.
| typedef NodeType<Key> GenTdSplayTreeRk< NodeType, Key, Compare >::Node |
Definition at line 123 of file tpl_splay_treeRk.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 comparison throws, and nothing else: this tree has no header node, so that it builds no key. |
Definition at line 258 of file tpl_splay_treeRk.H.
|
inlinenoexcept |
Builds an empty tree with the comparison criteria __cmp.
| [in] | __cmp | The comparison criteria, which is copied. |
| What | the copy of __cmp throws, and nothing else: a copy from a Compare that is not const, which a comparison may have apart from the one from a const one. |
Definition at line 271 of file tpl_splay_treeRk.H.
|
delete |
Not copyable: the tree does not own its nodes, and a copy would hold the same ones.
The copy that the compiler wrote left two trees with the same root, each of which splayed the nodes of the other, and std::move() of a tree copied it too. Move the tree, which leaves the other one empty, or copy its nodes (copyRec()).
|
inlinenoexcept |
Takes the nodes and the comparison criteria of tree, which is left empty and can take nodes again.
This tree has no header node: it builds no key.
| [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; then tree keeps its nodes. Nothing when it is noexcept. |
Definition at line 305 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::root.
|
inlinevirtual |
Destructor.
Definition at line 347 of file tpl_splay_treeRk.H.
|
inlineprivate |
Definition at line 352 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::cmp, Aleph::COUNT(), KEY, LLINK, RLINK, and GenTdSplayTreeRk< NodeType, Key, Compare >::root.
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::insert(), GenTdSplayTreeRk< NodeType, Key, Compare >::insert_dup(), and GenTdSplayTreeRk< NodeType, Key, Compare >::search_or_insert().
|
inline |
Returns the inorder (sorted) position of key.
find_position(key) searches in the extended splay tree for key (which takes \(O(\lg n)\) time) and returns the inorder position of the node that contains the key together with the node. If the key is not in the tree, find_position(key) returns the position where it would be if key belonged to the tree.
The return value is a pair<long, Node*>. The first component is the inorder position and the second is the node.
If key is not found in the tree, three cases can be distinguished:
first is -1 and second is the node containing the minimum key.first is COUNT(root) (number of nodes in the tree) and second is the node containing the maximum key.first is the position that key would have in the tree and second (node p) is a key immediately adjacent to key. Note that second can hold a smaller or greater key, but it is guaranteed to be immediately adjacent to key.| [in] | key | key to search and whose inorder position is requested. |
std::terminate()), since the splay is noexcept: it relinks the tree as it compares. Definition at line 651 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, GenTdSplayTreeRk< NodeType, Key, Compare >::CmpLess, Aleph::COUNT(), LLINK, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
inline |
See key_comp().
Definition at line 226 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::key_comp().
|
inline |
Get the top-down splay tree's root.
Definition at line 576 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::root.
Referenced by main().
|
inline |
Inserts a node in a top down splay tree.
| p | a pointer to the node to be inserted |
std::terminate()), since the splay is noexcept: it relinks the tree as it compares. Definition at line 385 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::__insert(), Aleph::and, GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, Aleph::COUNT(), KEY, LLINK, RLINK, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
Referenced by main().
|
inline |
Inserts a node in a top down splay tree, even if its key is already in the tree.
| p | a pointer to the node to be inserted |
p std::terminate()), since the splay is noexcept: it relinks the tree as it compares. Definition at line 413 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::__insert(), Aleph::and, KEY, LLINK, RLINK, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
inline |
Returns true if the tree is empty.
Definition at line 590 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::root.
|
inline |
Returns a reference to the comparison functor.
Definition at line 223 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::cmp.
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::get_compare().
|
delete |
Not copyable, for the same reason as the copy constructor.
|
inlinenoexcept |
Exchanges this tree with another, which is left with the nodes and the comparison criteria that this one had.
The tree does not own its nodes: those of this tree are not freed, and go to tree, as in swap(). The assignment that the compiler wrote copied the root, and left the two trees with the same nodes.
| [in,out] | tree | The tree to move from. |
*this. | What | swapping the comparison criteria throws; then nothing is exchanged. |
Definition at line 340 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::swap().
|
inline |
Returns the inorder (sorted) position of key.
position(key) searches in the extended splay tree for key (which takes \(O(\lg n)\) time) and returns the inorder position of the node that contains the key.
| [in] | key | key to search and whose inorder position is requested. |
std::terminate()), since the splay is noexcept: it relinks the tree as it compares. Definition at line 609 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, Aleph::COUNT(), LLINK, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
Referenced by main().
|
inline |
Remove a key from a top-down splay tree.
Searches a key in a top-down splay tree and remove the containing the key if this is found.
| key | to search |
std::terminate()), since the splay is noexcept: it relinks the tree as it compares. Definition at line 539 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, Aleph::COUNT(), LLINK, RLINK, GenTdSplayTreeRk< NodeType, Key, Compare >::root, GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl(), and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_max().
Referenced by main().
|
inline |
Searches a key in a top down splay tree.
| key | key to search |
std::terminate()), since the splay is noexcept: it relinks the tree as it compares. Definition at line 436 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
Referenced by main().
|
inline |
Searches the key of p and, if it is not in the tree, inserts p.
| p | a pointer to the node to search or insert |
p, or p if it was inserted std::terminate()), since the splay is noexcept: it relinks the tree as it compares. Definition at line 456 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::__insert(), GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, KEY, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
inline |
Returns the node whose inorder position in the extended tree is i.
select(i) returns the node in the extended splay tree whose inorder position is i.
| [in] | i | inorder position to select. |
| out_of_range | if i is greater than or equal to the number of nodes in the tree. |
Definition at line 677 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::root, and Aleph::select().
Referenced by main().
|
inline |
Returns the number of nodes stored in the tree.
Definition at line 584 of file tpl_splay_treeRk.H.
References Aleph::COUNT(), and GenTdSplayTreeRk< NodeType, Key, Compare >::root.
|
inlinenoexcept |
Search key within the tree and splay that node; if key is not found, the last node of the search is splayed.
| [in] | key | the key to search |
std::terminate()). noexcept because the splay relinks the tree as it compares: a comparison that threw in the middle would leave nodes out of the tree. Definition at line 240 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
inlineprivatenoexcept |
Definition at line 132 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::cmp, GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, GenTdSplayTreeRk< NodeType, Key, Compare >::CmpGreater, GenTdSplayTreeRk< NodeType, Key, Compare >::CmpLess, Aleph::COUNT(), KEY, l, LLINK, r, RLINK, GenTdSplayTreeRk< NodeType, Key, Compare >::root, Aleph::rotate_to_left_xt(), Aleph::rotate_to_right_xt(), sentinelCtor, and y.
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::find_position(), GenTdSplayTreeRk< NodeType, Key, Compare >::insert(), GenTdSplayTreeRk< NodeType, Key, Compare >::insert_dup(), GenTdSplayTreeRk< NodeType, Key, Compare >::position(), GenTdSplayTreeRk< NodeType, Key, Compare >::remove(), GenTdSplayTreeRk< NodeType, Key, Compare >::search(), GenTdSplayTreeRk< NodeType, Key, Compare >::search_or_insert(), and GenTdSplayTreeRk< NodeType, Key, Compare >::splay().
|
inlineprivatenoexcept |
Splay the maximum element to the root.
Uses the same zig-zig and zig rotations as standard splay to maintain the amortized O(log n) guarantee. Unlike splay(key), this function always brings the actual maximum to the root, which is needed for correct behavior when there are duplicate keys.
| r | root of the subtree |
Definition at line 480 of file tpl_splay_treeRk.H.
References Aleph::COUNT(), l, LLINK, r, RLINK, Aleph::rotate_to_left_xt(), sentinelCtor, and y.
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::remove().
|
inlinenoexcept |
Exchange in O(1) the nodes and the comparison criteria of this tree and another.
| [in,out] | tree | the tree to exchange with. |
| what | swapping the comparison criteria throws. They go first, so that, if that swap throws, nothing else is exchanged. |
Definition at line 319 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::cmp, GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::swap().
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::operator=(), and GenTdSplayTreeRk< NodeType, Key, Compare >::swap().
|
inline |
Definition at line 581 of file tpl_splay_treeRk.H.
References Aleph::and, Aleph::check_bst(), Aleph::check_rank_tree(), GenTdSplayTreeRk< NodeType, Key, Compare >::cmp, and GenTdSplayTreeRk< NodeType, Key, Compare >::root.
Referenced by main().
|
private |
Definition at line 127 of file tpl_splay_treeRk.H.
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::__insert(), GenTdSplayTreeRk< NodeType, Key, Compare >::key_comp(), GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl(), GenTdSplayTreeRk< NodeType, Key, Compare >::swap(), and GenTdSplayTreeRk< NodeType, Key, Compare >::verify().
|
staticconstexprprivate |
Definition at line 129 of file tpl_splay_treeRk.H.
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::find_position(), GenTdSplayTreeRk< NodeType, Key, Compare >::insert(), GenTdSplayTreeRk< NodeType, Key, Compare >::position(), GenTdSplayTreeRk< NodeType, Key, Compare >::remove(), GenTdSplayTreeRk< NodeType, Key, Compare >::search(), GenTdSplayTreeRk< NodeType, Key, Compare >::search_or_insert(), and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
staticconstexprprivate |
Definition at line 130 of file tpl_splay_treeRk.H.
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
staticconstexprprivate |
Definition at line 128 of file tpl_splay_treeRk.H.
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::find_position(), and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
private |
Definition at line 126 of file tpl_splay_treeRk.H.
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::GenTdSplayTreeRk(), GenTdSplayTreeRk< NodeType, Key, Compare >::Iterator::Iterator(), GenTdSplayTreeRk< NodeType, Key, Compare >::__insert(), GenTdSplayTreeRk< NodeType, Key, Compare >::find_position(), GenTdSplayTreeRk< NodeType, Key, Compare >::getRoot(), GenTdSplayTreeRk< NodeType, Key, Compare >::Iterator::inorder_successor(), GenTdSplayTreeRk< NodeType, Key, Compare >::insert(), GenTdSplayTreeRk< NodeType, Key, Compare >::insert_dup(), GenTdSplayTreeRk< NodeType, Key, Compare >::is_empty(), GenTdSplayTreeRk< NodeType, Key, Compare >::Iterator::next(), GenTdSplayTreeRk< NodeType, Key, Compare >::Iterator::next_ne(), GenTdSplayTreeRk< NodeType, Key, Compare >::position(), GenTdSplayTreeRk< NodeType, Key, Compare >::remove(), GenTdSplayTreeRk< NodeType, Key, Compare >::Iterator::reset_first(), GenTdSplayTreeRk< NodeType, Key, Compare >::search(), GenTdSplayTreeRk< NodeType, Key, Compare >::search_or_insert(), GenTdSplayTreeRk< NodeType, Key, Compare >::select(), GenTdSplayTreeRk< NodeType, Key, Compare >::size(), GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl(), GenTdSplayTreeRk< NodeType, Key, Compare >::swap(), and GenTdSplayTreeRk< NodeType, Key, Compare >::verify().