Aleph-w  1.9
General library for algorithms and data structures
Aleph::Treap_Rk_Vtl< Key, Compare > Struct Template Reference

#include <tpl_treapRk.H>

+ Inheritance diagram for Aleph::Treap_Rk_Vtl< Key, Compare >:
+ Collaboration diagram for Aleph::Treap_Rk_Vtl< Key, Compare >:

Public Types

using Base = Gen_Treap_Rk< Treap_Rk_NodeVtl, Key, Compare >
 
using Node = Treap_Rk_NodeVtl< Key >
 

Public Member Functions

void set_seed (unsigned long seed) noexcept
 Set the random number generator seed.
 
Compare & key_comp () noexcept
 return the comparison criteria
 
Compare & get_compare () noexcept
 
void swap (Gen_Treap_Rk &tree) noexcept
 Swap in constant time all the nodes of this with tree
 
Node *& getRoot () noexcept
 Return the tree's root.
 
Node * getRoot () const noexcept
 
Node * search (const Key &key) const noexcept
 
Node * insert (Node *p) noexcept
 
Node * search_or_insert (Node *p) noexcept
 
Node * insert_dup (Node *p) noexcept
 
bool verify () const
 Return true if the treap is consistent.
 
void join_exclusive (Gen_Treap_Rk &t) noexcept
 
Node * remove (const Key &key) noexcept
 
Node * remove (const size_t beg, const size_t end)
 
Node * remove_pos (const size_t pos)
 
Node * select (const size_t i) const
 
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
 
std::pair< int, Node *> find_position (const Key &key) const noexcept
 
bool split_key (const Key &key, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) noexcept
 
void split_key_dup (const Key &key, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2) noexcept
 
void split_pos (size_t pos, Gen_Treap_Rk &t1, Gen_Treap_Rk &t2)
 
void join_dup (Gen_Treap_Rk &t) noexcept
 
void join (Gen_Treap_Rk &t, Gen_Treap_Rk &dup) noexcept
 

Detailed Description

template<typename Key, class Compare = Aleph::less<Key>>
struct Aleph::Treap_Rk_Vtl< Key, Compare >

Extended treap (a special type of ramdomized 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 perfomance 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 adjustements are done in a botton-top way (by contrast with the randomized which is top-bottom), the treap takes less time.

Although tgus 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 constrast with the radomized which only requieres one interger (the counter).

For splitting and join of independent and large data sets the ramdomized 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 radomized trees. In addition,

The class internally uses the gsl random number generator of GSL - GNU Scientific Library. By default, the Mersene twister is used and the seed is taken from system time.

See also
Treap Treap_Vtl Treap_Rk Treap_Rk_Vtl Rand_Tree

Member Function Documentation

◆ find_position()

std::pair<int, Node*> Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::find_position ( const Key &  key) const
inlinenoexceptinherited

Find the inorder position of a key in the tree.

find_position(key) determines the inorder position that has or had key in the tree. Themethod return a tuple with a position and a node pointer.

If key is found then its inorder position is returned along with a pointer to the node containing it.

Otherwise, the the tuple returns the position that would have key if this was in the tree and the parent node that the key would had. At this regard, there are three cases:

  1. if key is lesser than the minimum key of tree, then first is -1 and the node is one with the smallest key.
  2. If key is greater than the maximum key in the tree, then first is the number of keys and the node is one with the maximum key in the tree.
  3. For any other case, first is the inorder position that would have key if this was in the tree and second is the node whose key is inmediataly greater than key.
Parameters
[in]keyto be searched
Returns
a pair with the inorder position and and related node

◆ get_compare()

Compare& Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::get_compare ( )
inlinenoexceptinherited

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ insert()

Node* Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::insert ( Node *  p)
inlinenoexceptinherited

Insert a node in a treap.

insert(p) inserta el nodo p en el treap this.

Parameters
[in]ppointer to the node to be inserted
Returns
if p->get_key() is not in the tree, then p is inserted and this node pointer is returned. Otherwise, it is returned nullptr.

◆ insert_dup()

Node* Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::insert_dup ( Node *  p)
inlinenoexceptinherited

Insert a node in the tree.

This method does not fail. It always inserts.

Parameters
[in]ppointer to the node to insert
Returns
the p pointer

◆ join()

void Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::join ( Gen_Treap_Rk< Treap_Rk_NodeVtl, Key, Compare > &  t,
Gen_Treap_Rk< Treap_Rk_NodeVtl, Key, Compare > &  dup 
)
inlinenoexceptinherited

Join this with t filtering the duplicated keys

join(t, dup) produces a random tree 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

Parameters
[in]tramdomized tree to join with this
[out]dupramdomized tree where nodes containing duplicated keys are inserted

◆ join_dup()

void Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::join_dup ( Gen_Treap_Rk< Treap_Rk_NodeVtl, Key, Compare > &  t)
inlinenoexceptinherited

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.

Parameters
[in]tramdomized tree to join with this keys are inserted

◆ join_exclusive()

void Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::join_exclusive ( Gen_Treap_Rk< Treap_Rk_NodeVtl, Key, Compare > &  t)
inlinenoexceptinherited

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.

Parameters
[in]tramdomized tree to exclusively join with this keys are inserted

◆ position()

std::pair<int, Node*> Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::position ( const Key &  key) const
inlinenoexceptinherited

Compute the inorder position of a key

Parameters
[in]rroot of tree
[in]keyto be searched
Returns
inorder position of key if this is in the tree or -1 if key is not found

◆ remove() [1/2]

Node* Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::remove ( const Key &  key)
inlinenoexceptinherited

Remove a key from the tree

Parameters
[in]keyto remove
Returns
a valid pointer to the removed node if key was found in the tree, nullptr otherwise

◆ remove() [2/2]

Node* Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::remove ( const size_t  beg,
const size_t  end 
)
inlineinherited

Remove from the treap all the keys between inorder position beg and end.

Parameters
[in]begbeggining inorder position
[in]endending inorder position
Returns
a pointer to a tree root containing all the removed nodes
Exceptions
range_errorif the range is invalid

◆ remove_pos()

Node* Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::remove_pos ( const size_t  pos)
inlineinherited

Remove the node at the inorder position pos

Parameters
[in]posinorder position of node to be removed
Returns
a vaid pointer to the removed node
Exceptions
out_of_rangeif pos is greater or equal than the number of nodes of tree

◆ search()

Node* Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::search ( const Key &  key) const
inlinenoexceptinherited

Search a key in a treap

Parameters
[in]keyto be searched
Returns
a pointer to the node containing key if this is found; otherwise, nullptr is returned

◆ search_or_insert()

Node* Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::search_or_insert ( Node *  p)
inlinenoexceptinherited

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.

Parameters
[in]pnode containing a key to be searched and eventually inserted
Returns
if the key contained in p is found, then a pointer to the containing key in the tree is returned. Otherwise, p is inserted and returned

◆ select()

Node* Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::select ( const size_t  i) const
inlineinherited

Return the i-th node in order sense

Parameters
[in]iinorder position of node to be selected
Returns
a pointer to the i-th node inorder sense
Exceptions
out_of_rangeif pos is greater or equal than the number of nodes of tree

◆ split_key()

bool Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::split_key ( const Key &  key,
Gen_Treap_Rk< Treap_Rk_NodeVtl, Key, Compare > &  t1,
Gen_Treap_Rk< Treap_Rk_NodeVtl, Key, Compare > &  t2 
)
inlinenoexceptinherited

Split the tree according to a key

Parameters
[in]keyfor splitting
[out]t1tree with keys lesser than key
[out]t2tree with keys greater than key
Returns
true if tree was split; that is if key is not in the tree. Otherwise, if key is in the tree, false is returned

◆ split_key_dup()

void Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::split_key_dup ( const Key &  key,
Gen_Treap_Rk< Treap_Rk_NodeVtl, Key, Compare > &  t1,
Gen_Treap_Rk< Treap_Rk_NodeVtl, Key, Compare > &  t2 
)
inlinenoexceptinherited

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.

Parameters
[in]keyfor splitting
[out]t1resulting tree with the keys lesser than key
[out]t2resulting tree with the keys greater or equal than key

◆ split_pos()

void Aleph::Gen_Treap_Rk< Treap_Rk_NodeVtl , Key, Compare >::split_pos ( size_t  pos,
Gen_Treap_Rk< Treap_Rk_NodeVtl, Key, Compare > &  t1,
Gen_Treap_Rk< Treap_Rk_NodeVtl, Key, Compare > &  t2 
)
inlineinherited

Split the tree at the inorder position pos

Parameters
[in]posinorder position where to split
[out]t1tree where the rank of keys $[0, pos)$ will be put
[out]t2tree where the rank of keys $[pos, n]$ will be put

The documentation for this struct was generated from the following file:

Leandro Rabindranath León