|
Aleph-w 3.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 () |
| void | splay (const Key &key) noexcept |
| search key within tree and splay that node. | |
| GenTdSplayTreeRk (Compare __cmp=Compare()) | |
| Constructor. | |
| void | swap (GenTdSplayTreeRk &tree) |
| virtual | ~GenTdSplayTreeRk () |
| Destructor. | |
| Node * | insert (Node *p) |
| Inserts a node in a top down splay tree. | |
| Node * | insert_dup (Node *p) |
| Node * | search (const Key &key) |
| Searches a key in a top down splay tree. | |
| Node * | search_or_insert (Node *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. |
Definition at line 107 of file tpl_splay_treeRk.H.
| typedef Key GenTdSplayTreeRk< NodeType, Key, Compare >::key_type |
The key type stored in the node.
Definition at line 207 of file tpl_splay_treeRk.H.
| typedef NodeType<Key> GenTdSplayTreeRk< NodeType, Key, Compare >::Node |
Definition at line 110 of file tpl_splay_treeRk.H.
|
inline |
Constructor.
Definition at line 226 of file tpl_splay_treeRk.H.
|
inlinevirtual |
Destructor.
Definition at line 239 of file tpl_splay_treeRk.H.
|
inlineprivate |
Definition at line 244 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::cmp, Aleph::COUNT(), KEY, Aleph::LLINK(), Aleph::maps(), Aleph::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. |
Definition at line 511 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, GenTdSplayTreeRk< NodeType, Key, Compare >::CmpLess, Aleph::COUNT(), Aleph::LLINK(), Aleph::maps(), GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
inline |
Definition at line 213 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 442 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::root.
|
inline |
Inserts a node in a top down splay tree.
| p | a pointer to the node to be inserted |
Definition at line 274 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::__insert(), GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, Aleph::COUNT(), KEY, Aleph::LLINK(), Aleph::maps(), Aleph::RLINK(), GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
inline |
Definition at line 293 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::__insert(), KEY, Aleph::LLINK(), Aleph::maps(), Aleph::RLINK(), GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
inline |
Returns true if the tree is empty.
Definition at line 456 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::root.
|
inline |
Returns a reference to the comparison functor.
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 210 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::cmp.
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::get_compare().
|
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. |
Definition at line 472 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, Aleph::COUNT(), Aleph::LLINK(), Aleph::maps(), GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
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 |
Definition at line 405 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, Aleph::COUNT(), Aleph::LLINK(), Aleph::maps(), Aleph::HTList::reset(), Aleph::RLINK(), GenTdSplayTreeRk< NodeType, Key, Compare >::root, GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl(), and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_max().
|
inline |
Searches a key in a top down splay tree.
| key | key to search |
Definition at line 314 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, Aleph::maps(), GenTdSplayTreeRk< NodeType, Key, Compare >::root, and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
inline |
Definition at line 324 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::__insert(), GenTdSplayTreeRk< NodeType, Key, Compare >::CmpEqual, KEY, Aleph::maps(), 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 537 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::root, and Aleph::select().
|
inline |
Returns the number of nodes stored in the tree.
Definition at line 450 of file tpl_splay_treeRk.H.
References Aleph::COUNT(), and GenTdSplayTreeRk< NodeType, Key, Compare >::root.
|
inlinenoexcept |
search key within tree and splay that node.
Definition at line 220 of file tpl_splay_treeRk.H.
References Aleph::maps(), and GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
inlineprivatenoexcept |
Definition at line 119 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, Aleph::LLINK(), Aleph::maps(), Aleph::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 348 of file tpl_splay_treeRk.H.
References Aleph::COUNT(), l, Aleph::LLINK(), Aleph::maps(), Aleph::RLINK(), Aleph::rotate_to_left_xt(), sentinelCtor, and y.
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::remove().
|
inline |
Definition at line 232 of file tpl_splay_treeRk.H.
References GenTdSplayTreeRk< NodeType, Key, Compare >::cmp, and GenTdSplayTreeRk< NodeType, Key, Compare >::root.
|
inline |
Definition at line 447 of file tpl_splay_treeRk.H.
References Aleph::check_bst(), Aleph::check_rank_tree(), GenTdSplayTreeRk< NodeType, Key, Compare >::cmp, Aleph::maps(), and GenTdSplayTreeRk< NodeType, Key, Compare >::root.
|
private |
Definition at line 114 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 116 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 117 of file tpl_splay_treeRk.H.
Referenced by GenTdSplayTreeRk< NodeType, Key, Compare >::splay_impl().
|
staticconstexprprivate |
Definition at line 115 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 113 of file tpl_splay_treeRk.H.
Referenced by 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().