|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Extended treap (a special type of randomized binary search tree) which manages selection and splitting for inorder position. More...
#include <tpl_treapRk.H>
Public Types | |
| using | Base = Gen_Treap_Rk< Treap_Rk_NodeVtl, Key, Compare > |
Public Types inherited from Aleph::Gen_Treap_Rk< NodeType, Key, Compare > | |
| using | Node = NodeType< Key > |
Additional Inherited Members | |
Public Member Functions inherited from Aleph::Gen_Treap_Rk< NodeType, Key, Compare > | |
| void | set_seed (const unsigned long seed) |
Seeds the Mersenne Twister of the treap, as gsl_rng_set() did: the priorities are from then on those of GSL for the seed. | |
| Random_Source & | random_source () noexcept |
| The source of random numbers, which gives the priorities. | |
| const Random_Source & | random_source () const noexcept |
| The source of random numbers, to look at it: its engine, or whether it is equal to another one. | |
| Gsl_Compatible_Rng & | rng () |
| The Mersenne Twister of the treap, which a treap that was given no seed makes here, and draws from it from then on. | |
| Compare & | key_comp () noexcept |
| return the comparison criteria | |
| Compare & | get_compare () noexcept |
See key_comp(). | |
| Gen_Treap_Rk (const unsigned long seed, Compare __cmp=Compare()) | |
Builds an empty treap whose priorities are those of GSL for seed: it draws them from a Mersenne Twister, in dynamic memory. | |
| Gen_Treap_Rk () 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 treap with a comparison criteria built by default, which draws its priorities from a SplitMix64 with a seed of its own. | |
| Gen_Treap_Rk (Compare __cmp) noexcept(std::is_nothrow_default_constructible_v< Node > and std::is_nothrow_constructible_v< Compare, Compare & >) | |
Builds an empty treap with the comparison criteria __cmp, which draws its priorities from a SplitMix64 with a seed of its own. | |
| Gen_Treap_Rk (Gen_Treap_Rk &&tree) noexcept(bin_node_detail::nothrow_header_from_v< Node > and bin_node_detail::nothrow_compare_taken_v< Compare >) | |
Takes the nodes, the comparison criteria and the generator of tree, which keeps no nodes, and draws from then on from a SplitMix64 with a seed of its own: it can take nodes again. | |
| void | swap (Gen_Treap_Rk &tree) noexcept(std::is_nothrow_swappable_v< Compare >) |
Swaps in constant time the nodes and the comparison criteria of this and tree. | |
| Node *& | getRoot () noexcept |
| Return the tree's root. | |
| Node * | getRoot () const noexcept |
| Node * | search (const Key &key) const noexcept(nothrow_compare_v< Compare, Key >) |
| Search a key in a treap. | |
| Node * | insert (Node *p) noexcept(nothrow_compare_v< Compare, Key >) |
| Insert a node in a treap. | |
| Node * | insert_dup (Node *p) noexcept(nothrow_compare_v< Compare, Key >) |
| Insert a node in the tree. | |
| Node * | search_or_insert (Node *p) noexcept(nothrow_compare_v< Compare, Key >) |
| Search or insert a key. | |
| bool | verify () const |
Return true if the treap is consistent. | |
| Node * | remove (const Key &key) noexcept(nothrow_compare_v< Compare, Key >) |
| Remove a key from the tree. | |
| Node * | remove (const size_t beg, const size_t end) |
Remove from the treap all the keys between inorder position beg and end. | |
| Node * | remove_pos (const size_t pos) |
| Remove the node at the inorder position pos. | |
| Node * | select (const size_t i) const |
| Return the i-th node in order sense. | |
| size_t | size () const noexcept |
| Return the number of nodes contained in the tree. | |
| bool | is_empty () const noexcept |
Return true if tree is empty. | |
| std::pair< int, Node * > | position (const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >) |
| Compute the inorder position of a key. | |
| std::pair< int, Node * > | find_position (const Key &key) const noexcept(nothrow_compare_copy_v< Compare, Key >) |
| Find the inorder position of a key in the tree. | |
| bool | split_key (const Key &key, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) noexcept(nothrow_compare_v< Compare, Key >) |
| Split the tree according to a key. | |
| void | split_key_dup (const Key &key, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) noexcept(nothrow_compare_v< Compare, Key >) |
| Split the tree according to a key that could be in the tree. | |
| void | split_pos (size_t pos, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) |
| Split the tree at the inorder position pos. | |
| void | join (Gen_Treap_Rk &t, Gen_Treap_Rk &dup) noexcept |
Join this with t filtering the duplicated keys. | |
| void | join_dup (Gen_Treap_Rk &t) noexcept |
Join this with t independently of the presence of duplicated keys. | |
| void | join_exclusive (Gen_Treap_Rk &t) noexcept |
Join exclusive of this with t | |
Extended treap (a special type of randomized binary search tree) which manages selection and splitting for inorder position.
The treap is a binary search tree whose very high performance is achieved by randomization. The basic idea is to store a priority value in each node which is randomly chosen. By the side of keys, the tree a binary search, but by the side of priorities, the tree is a heap. It is shown that this class of tree has an expected performance of \(O(\lg n)\) for the majority of its operations. In addition, this extended tree uses and second integer for storing subtrees sizes.
The treap is faster than the randomized tree by a constant time (both approaches are logarithmic). Since the priority is chosen just one time and the adjustments are done in a botton-top way (by contrast with the randomized which is top-bottom), the treap takes less time.
Although this approach trends to be faster than the randomized trees, takes in account that this treap is more space consuming because each node requires 2 additional integers to the data (priority and counter) in contrast with the randomized which only requires one integer (the counter).
For splitting and join of independent and large data sets the randomized option trends to be faster. The split is equivalent, but the join is definitively faster. The join of two trees of n and m keys respectively takes \(O(n \lg m)\) with treaps, while it takes \(O(\max(\lg n, \lg m))\) with randomized trees. In addition,
The priorities come from a Random_Source: by default, from a SplitMix64 inside the treap, with a seed of its own; with a seed, from a Gsl_Compatible_Rng, the Mersenne twister with the numbers of the one of GSL - GNU Scientific Library, which it used before.
Definition at line 1408 of file tpl_treapRk.H.
| using Aleph::Treap_Rk_Vtl< Key, Compare >::Base = Gen_Treap_Rk<Treap_Rk_NodeVtl, Key, Compare> |
Definition at line 1410 of file tpl_treapRk.H.