|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Treap - A randomized binary search tree using heap-ordered priorities. More...
#include <tpl_treap.H>
Classes | |
| struct | Iterator |
| Iterator on nodes of the tree. More... | |
Public Types | |
| using | Node = NodeType< Key > |
Public Member Functions | |
| void | set_seed (const unsigned long seed) noexcept |
| Set the random number generator seed. | |
| void | swap (Gen_Treap &tree) noexcept |
Swap in constant time all the nodes of this with tree | |
| Compare & | key_comp () noexcept |
| return the comparison criteria | |
| Compare & | get_compare () noexcept |
| Gen_Treap (const unsigned long seed, const Compare &__cmp=Compare()) | |
Initialize a treap with random seed and comparison criteria __cmp | |
| Gen_Treap (const Compare &cmp=Compare()) | |
Initialize a treap with random seed from system time and comparison criteria cmp | |
| ~Gen_Treap () | |
| gsl_rng * | gsl_rng_object () noexcept |
| Get a pointer to gsl random number generator. | |
| Node *& | getRoot () noexcept |
| Return the tree's root. | |
| Node * | search (const Key &key) const noexcept |
| Search a key in a treap. | |
| Node * | insert (Node *p) noexcept |
| Insert a node in a treap. | |
| Node * | search_or_insert (Node *p) noexcept |
| Search or insert a key. | |
| Node * | insert_dup (Node *p) noexcept |
| Insert a node in the tree. | |
| bool | verify () const noexcept |
Return true if this is a consistent treap. | |
| Node * | remove (const Key &key) noexcept |
| Remove a key from the tree. | |
| void | join (Gen_Treap &t, Gen_Treap &dup) noexcept |
Join this with t filtering the duplicated keys. | |
| void | join_dup (Gen_Treap &t) noexcept |
Join this with t independently of the presence of duplicated keys. | |
| void | join_exclusive (Gen_Treap &t) noexcept |
Join exclusive of this with t | |
| bool | split_key (const Key &key, Gen_Treap &t1, Gen_Treap &t2) |
| Split the tree according to a key. | |
| void | split_key_dup (const Key &key, Gen_Treap &t1, Gen_Treap &t2) |
| Split the tree according to a key that could be in the tree. | |
Private Member Functions | |
| void | init (const unsigned int seed) |
| Node * | insert (Node *root, Node *p) noexcept |
| Node * | search_or_insert (Node *&root, Node *p) noexcept |
| Node * | insert_dup (Node *root, Node *p) noexcept |
| Node * | remove (Node *&root, const Key &key) noexcept |
| void | join_dup (Node *&t1, Node *t2) noexcept |
| void | join (Node *&t1, Node *t2, Node *&dup) noexcept |
Static Private Member Functions | |
| static Node * | join_exclusive (Node *t1, Node *t2) noexcept |
Private Attributes | |
| Node | head |
| The type of node. | |
| Node * | head_ptr |
| Node *& | tree_root |
| gsl_rng * | r |
| Compare | cmp |
Treap - A randomized binary search tree using heap-ordered priorities.
A Treap (tree + heap) is a randomized binary search tree that combines BST properties on keys with heap properties on randomly assigned priorities. Each node has a key (BST-ordered) and a random priority (heap-ordered).
The dual ordering (BST for keys, heap for priorities) creates a unique tree structure for any set of keys with given priorities, ensuring good balance with high probability.
| NodeType | Node template (TreapNode or TreapNodeVtl). |
| Key | The type of keys stored in the tree. |
| Compare | Comparison functor for ordering keys. |
Definition at line 147 of file tpl_treap.H.
| using Aleph::Gen_Treap< NodeType, Key, Compare >::Node = NodeType<Key> |
Definition at line 150 of file tpl_treap.H.
|
inline |
Initialize a treap with random seed and comparison criteria __cmp
Definition at line 189 of file tpl_treap.H.
References Aleph::init.
|
inline |
Initialize a treap with random seed from system time and comparison criteria cmp
Definition at line 197 of file tpl_treap.H.
|
inline |
Definition at line 202 of file tpl_treap.H.
References Aleph::maps(), and Aleph::Gen_Treap< NodeType, Key, Compare >::r.
|
inlinenoexcept |
Definition at line 185 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::key_comp().
|
inlinenoexcept |
Return the tree's root.
Definition at line 212 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
Referenced by TreapTest::tree_empty(), TreapTest::tree_size(), and write_treap().
|
inlinenoexcept |
Get a pointer to gsl random number generator.
Definition at line 209 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::r.
Definition at line 159 of file tpl_treap.H.
References ah_bad_alloc_if, Aleph::Gen_Treap< NodeType, Key, Compare >::head_ptr, Aleph::maps(), Aleph::Min_Priority, Aleph::PRIO(), and Aleph::Gen_Treap< NodeType, Key, Compare >::r.
|
inlinenoexcept |
Insert a node in a treap.
insert(p) inserta el nodo p en el treap this.
| [in] | p | pointer to the node to be inserted |
p->get_key() is not in the tree, then p is inserted and this node pointer is returned. Otherwise, it is returned nullptr. Definition at line 318 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::insert(), Aleph::maps(), Aleph::PRIO(), Aleph::Gen_Treap< NodeType, Key, Compare >::r, and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
|
inlineprivatenoexcept |
Definition at line 227 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::cmp, Aleph::Gen_Treap< NodeType, Key, Compare >::insert(), KEY, Aleph::LLINK(), Aleph::maps(), Aleph::PRIO(), Aleph::RLINK(), root(), Aleph::rotate_to_left(), and Aleph::rotate_to_right().
Referenced by Aleph::Gen_Treap< NodeType, Key, Compare >::insert(), Aleph::Gen_Treap< NodeType, Key, Compare >::insert(), TreapTest::insert_values(), Aleph::Gen_Treap< NodeType, Key, Compare >::join(), and write_treap().
|
inlinenoexcept |
Insert a node in the tree.
This method does not fail. It always inserts.
| [in] | p | pointer to the node to insert |
p pointer Definition at line 360 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::insert_dup(), Aleph::maps(), Aleph::PRIO(), Aleph::Gen_Treap< NodeType, Key, Compare >::r, and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
|
inlineprivatenoexcept |
Definition at line 290 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::cmp, Aleph::Gen_Treap< NodeType, Key, Compare >::insert_dup(), KEY, Aleph::LLINK(), Aleph::maps(), Aleph::PRIO(), Aleph::RLINK(), root(), Aleph::rotate_to_left(), and Aleph::rotate_to_right().
Referenced by Aleph::Gen_Treap< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Treap< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Treap< NodeType, Key, Compare >::join(), and Aleph::Gen_Treap< NodeType, Key, Compare >::join_dup().
|
inlinenoexcept |
Join this with t filtering the duplicated keys.
join(t, dup) produces a treap result of join of this and t. The resulting tree is stored in this.
Nodes containing duplicated keys are inserted in the randomized tree dup. The nodes could belong to any of two trees
| [in] | t | ramdomized tree to join with this |
| [out] | dup | ramdomized tree where nodes containing duplicated keys are inserted |
Definition at line 505 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::join(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
|
inlineprivatenoexcept |
Definition at line 472 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::insert(), Aleph::Gen_Treap< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Treap< NodeType, Key, Compare >::join(), KEY, l, Aleph::LLINK(), Aleph::maps(), Aleph::Gen_Treap< NodeType, Key, Compare >::r, Aleph::Gen_Treap< NodeType, Key, Compare >::remove(), Aleph::HTList::reset(), and Aleph::RLINK().
Referenced by Aleph::Gen_Treap< NodeType, Key, Compare >::join(), and Aleph::Gen_Treap< NodeType, Key, Compare >::join().
|
inlinenoexcept |
Join this with t independently of the presence of duplicated keys.
join(t) produces a treap result of join of this and t. The resulting tree is stored in this.
| [in] | t | ramdomized tree to join with this keys are inserted |
Definition at line 519 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::join_dup(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
|
inlineprivatenoexcept |
Definition at line 460 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Treap< NodeType, Key, Compare >::join_dup(), l, Aleph::LLINK(), Aleph::maps(), Aleph::Gen_Treap< NodeType, Key, Compare >::r, Aleph::HTList::reset(), and Aleph::RLINK().
Referenced by Aleph::Gen_Treap< NodeType, Key, Compare >::join_dup(), and Aleph::Gen_Treap< NodeType, Key, Compare >::join_dup().
|
inlinenoexcept |
Join exclusive of this with t
Exclusive means that all the keys of this are lesser than all the keys of t. This knowlege allows a more effcient way for joining that when the keys ranks are overlapped. However, use very carefully because the algorithm does not perform any check and the result would be incorrect.
| [in] | t | ramdomized tree to exclusively join with this keys are inserted |
Definition at line 536 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::join_exclusive(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
|
inlinestaticprivatenoexcept |
Definition at line 425 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::join_exclusive(), Aleph::LLINK(), Aleph::maps(), Aleph::PRIO(), and Aleph::RLINK().
Referenced by Aleph::Gen_Treap< NodeType, Key, Compare >::join_exclusive(), Aleph::Gen_Treap< NodeType, Key, Compare >::join_exclusive(), and Aleph::Gen_Treap< NodeType, Key, Compare >::remove().
|
inlinenoexcept |
return 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 182 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::cmp.
Referenced by Aleph::Gen_Treap< NodeType, Key, Compare >::get_compare().
|
inlinenoexcept |
Remove a key from the tree.
| [in] | key | to remove |
key was found in the tree, nullptr otherwise Definition at line 379 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::cmp, Aleph::Gen_Treap< NodeType, Key, Compare >::head_ptr, KEY, Aleph::LLINK(), Aleph::maps(), Aleph::PRIO(), Aleph::HTList::reset(), Aleph::RLINK(), Aleph::rotate_to_left(), Aleph::rotate_to_right(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
Referenced by Aleph::Gen_Treap< NodeType, Key, Compare >::join(), and Aleph::Gen_Treap< NodeType, Key, Compare >::remove().
|
inlineprivatenoexcept |
Definition at line 442 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::cmp, Aleph::Gen_Treap< NodeType, Key, Compare >::join_exclusive(), KEY, Aleph::LLINK(), Aleph::maps(), Aleph::Gen_Treap< NodeType, Key, Compare >::remove(), Aleph::HTList::reset(), Aleph::RLINK(), and root().
|
inlinenoexcept |
Search a key in a treap.
| [in] | key | to be searched |
key if this is found; otherwise, nullptr is returned Definition at line 220 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::cmp, Aleph::maps(), Aleph::searchInBinTree(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
Referenced by write_treap().
|
inlineprivatenoexcept |
Definition at line 261 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::cmp, KEY, Aleph::LLINK(), Aleph::maps(), Aleph::PRIO(), Aleph::RLINK(), root(), Aleph::rotate_to_left(), Aleph::rotate_to_right(), and Aleph::Gen_Treap< NodeType, Key, Compare >::search_or_insert().
Referenced by Aleph::Gen_Treap< NodeType, Key, Compare >::search_or_insert(), and Aleph::Gen_Treap< NodeType, Key, Compare >::search_or_insert().
|
inlinenoexcept |
Search or insert a key.
search_or_insert(p) searches in the tree the key KEY(p). If this key is found, then a pointer to the node containing it is returned. Otherwise, p is inserted.
| [in] | p | node containing a key to be searched and eventually inserted |
p is found, then a pointer to the containing key in the tree is returned. Otherwise, p is inserted and returned Definition at line 344 of file tpl_treap.H.
References Aleph::maps(), Aleph::PRIO(), Aleph::Gen_Treap< NodeType, Key, Compare >::r, Aleph::Gen_Treap< NodeType, Key, Compare >::search_or_insert(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
|
inlinenoexcept |
Set the random number generator seed.
Definition at line 171 of file tpl_treap.H.
References Aleph::maps(), and Aleph::Gen_Treap< NodeType, Key, Compare >::r.
Referenced by TreapTest::SetUp().
|
inline |
Split the tree according to a key.
| [in] | key | for splitting |
| [out] | t1 | tree with keys lesser than key |
| [out] | t2 | tree with keys greater than key |
true if tree was split; that is if key is not in the tree. Otherwise, if key is in the tree, false is returned Definition at line 550 of file tpl_treap.H.
References Aleph::maps(), Aleph::split_key_rec(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
|
inline |
Split the tree according to a key that could be in the tree.
split_dup(key, t1, t2) splits the tree according to key in two trees t1 which contains the key lesser than key and t2 which contains the keys greater or equal than key.
| [in] | key | for splitting |
| [out] | t1 | resulting tree with the keys lesser than key |
| [out] | t2 | resulting tree with the keys greater or equal than key |
Definition at line 565 of file tpl_treap.H.
References Aleph::maps(), Aleph::split_key_dup_rec(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
|
inlinenoexcept |
Swap in constant time all the nodes of this with tree
Definition at line 174 of file tpl_treap.H.
References Aleph::Gen_Treap< NodeType, Key, Compare >::cmp, Aleph::Gen_Treap< NodeType, Key, Compare >::r, and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
|
inlinenoexcept |
Return true if this is a consistent treap.
Definition at line 371 of file tpl_treap.H.
References Aleph::is_treap(), and Aleph::Gen_Treap< NodeType, Key, Compare >::tree_root.
|
private |
Definition at line 157 of file tpl_treap.H.
Referenced by Aleph::Gen_Treap< NodeType, Key, Compare >::insert(), Aleph::Gen_Treap< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Treap< NodeType, Key, Compare >::key_comp(), Aleph::Gen_Treap< NodeType, Key, Compare >::remove(), Aleph::Gen_Treap< NodeType, Key, Compare >::remove(), Aleph::Gen_Treap< NodeType, Key, Compare >::search(), Aleph::Gen_Treap< NodeType, Key, Compare >::search_or_insert(), and Aleph::Gen_Treap< NodeType, Key, Compare >::swap().
|
private |
The type of node.
Definition at line 153 of file tpl_treap.H.
|
private |
Definition at line 154 of file tpl_treap.H.
Referenced by Aleph::Gen_Treap< NodeType, Key, Compare >::init(), and Aleph::Gen_Treap< NodeType, Key, Compare >::remove().
|
private |
Definition at line 156 of file tpl_treap.H.
Referenced by Aleph::Gen_Treap< NodeType, Key, Compare >::~Gen_Treap(), Aleph::Gen_Treap< NodeType, Key, Compare >::gsl_rng_object(), Aleph::Gen_Treap< NodeType, Key, Compare >::init(), Aleph::Gen_Treap< NodeType, Key, Compare >::insert(), Aleph::Gen_Treap< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Treap< NodeType, Key, Compare >::join(), Aleph::Gen_Treap< NodeType, Key, Compare >::join_dup(), Aleph::Gen_Treap< NodeType, Key, Compare >::search_or_insert(), Aleph::Gen_Treap< NodeType, Key, Compare >::set_seed(), and Aleph::Gen_Treap< NodeType, Key, Compare >::swap().
|
private |
Definition at line 155 of file tpl_treap.H.
Referenced by Aleph::Gen_Treap< NodeType, Key, Compare >::getRoot(), Aleph::Gen_Treap< NodeType, Key, Compare >::insert(), Aleph::Gen_Treap< NodeType, Key, Compare >::insert_dup(), Aleph::Gen_Treap< NodeType, Key, Compare >::join(), Aleph::Gen_Treap< NodeType, Key, Compare >::join_dup(), Aleph::Gen_Treap< NodeType, Key, Compare >::join_exclusive(), Aleph::Gen_Treap< NodeType, Key, Compare >::remove(), Aleph::Gen_Treap< NodeType, Key, Compare >::search(), Aleph::Gen_Treap< NodeType, Key, Compare >::search_or_insert(), Aleph::Gen_Treap< NodeType, Key, Compare >::split_key(), Aleph::Gen_Treap< NodeType, Key, Compare >::split_key_dup(), Aleph::Gen_Treap< NodeType, Key, Compare >::swap(), and Aleph::Gen_Treap< NodeType, Key, Compare >::verify().