ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::Treap< K, V > Class Template Reference

a DICTIONARY on a treap, with the operations based on join [see Joinable] More...

#include <ADS/tree/Treap.h>

Inheritance diagram for ADS::Treap< K, V >:
ADS::Joinable< Treap< K, V >, TreapNodes< K, V >, K, V >

Classes

struct  Shape
 the shape of the tree, for PathIterator More...

Public types

using Key = K
 the type of the keys
using Value = V
 the type of the values
using value_type = std::pair< K , V >
 the pairs ( key , value ) by value, which own their copies
using key_type = K
 the type of the keys
using mapped_type = V
 the type of the values
using size_type = std::size_t
 the type of the sizes
using difference_type = std::ptrdiff_t
 the type of the distances
using reference = PairRef< const K & , const V & >
 references, by value
using const_reference = reference
 the same
using const_iterator = PathIterator< Treap , Shape >
 the constant iterator by increasing key, along the path from the root
using iterator = const_iterator
 the constant iterator

Public member functions

 Treap (std::uint64_t seed=1)
 the empty treap, its priorities drawn from the seed
std::optional< V > lookup (const K &k) const
 the value of \( k \), or nothing, by the descent of a binary search tree, in \( O( \log n ) \) expected time
void insert (const K &k, const V &v)
 associates \( v \) to \( k \), in \( O( \log n ) \) expected time
void remove (const K &k)
 removes \( k \), if it is there, in \( O( \log n ) \) expected time
bool contains (const K &k) const
 true if \( k \) is there
std::size_t size (void) const
 the number \( n \) of keys
int height (void) const
 the height, -1 if empty
template<class F>
void forEach (F &&f) const
 f( k , v ) on the pairs by increasing keys
std::optional< std::pair< K, V > > lowerBound (const K &k) const
 the smallest key not smaller than \( k \) and its value, or nothing (the find(x) of an SSet of Morin), in \( O( \log n ) \) expected time
const K * findKey (const K &k) const
 the address of the key \( k \), nil if absent, in \( O( \log n ) \) expected time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed
const K * lowerBoundKey (const K &k) const
 the address of the smallest key not smaller than \( k \), nil if none, in \( O( \log n ) \) expected time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed
std::pair< K, V > select (std::size_t i) const
 the pair of rank \( i \), i.e., with \( i \) smaller keys, in \( O( \log n ) \) expected time (exercise 7.10 of Morin)
std::size_t rank (const K &k) const
 the number of keys smaller than \( k \), the sizes of the left subtrees passed on the right along the search of \( k \), in \( O( \log n ) \) expected time
const_iterator begin (void) const
 the iterator on the smallest key
const_iterator end (void) const
 the iterator past the largest key
const_iterator cbegin (void) const
 as begin()
const_iterator cend (void) const
 as end()
Public member functions inherited from ADS::Joinable< Treap< K, V >, TreapNodes< K, V >, K, V >
std::tuple< Treap< K, V >, std::optional< V >, Treap< K, V > > split (const K &k) &&
 the keys before \( k \), the value of \( k \) (if there), the keys after \( k \), by JoinAlgorithms::split(); the tree is consumed, in \( O( \log n ) \) time
Treap< K, V > extractRange (const K &lo, const K &hi)
 the pairs of the keys in \( [ \mathit{lo} , \mathit{hi} ] \), taken out of the tree by JoinAlgorithms::extract(), in \( O( \log n ) \) time
void unionWith (Treap< K, V > &&o, F f=F())
 the union with o, which is consumed, by JoinAlgorithms::unite(); on a key of both, the value f( mine , its ), by default its; \( O( m \log( n / m + 1 ) ) \) work for sizes \( m \le n \)
void intersectWith (Treap< K, V > &&o, F f=F())
 the intersection with o, which is consumed, by JoinAlgorithms::intersect(); the value f( mine , its ), by default mine; the work of unionWith(), plus the freeing of the nodes that leave
void differenceWith (Treap< K, V > &&o)
 the keys not in o, which is consumed, by JoinAlgorithms::subtract(); the work of unionWith(), plus the freeing of the nodes of o
void multiInsert (std::span< const std::pair< K, V > > B, F f=F())
 the pairs of \( B \) inserted, by JoinAlgorithms::multiInsert(); on a key already there, the value f( mine , new ), by default new; \( O( m \log( n / m + 1 ) ) \) work for \( m \) pairs
void multiRemove (std::span< const K > B)
 the keys of \( B \) removed, by JoinAlgorithms::multiRemove(), \( O( m \log( n / m + 1 ) ) \) work
void filter (P p)
 only the pairs with p( k , v ) kept, by JoinAlgorithms::filter(), \( O( n ) \) work
U mapReduce (M map, R reduce, U id) const
 \( \mathrm{reduce}( \ldots \mathrm{reduce}( \mathit{id} , \mathrm{map}( k_1 , v_1 ) ) \ldots , \mathrm{map}( k_n , v_n ) ) \) by an associative reduce of identity id, by JoinAlgorithms::mapReduce(), \( O( n ) \) work

Static public member functions

static Treap fromSorted (const std::vector< std::pair< K, V > > &A, std::uint64_t seed=1)
 the treap of the pairs of \( A \), by increasing keys, in \( O( n ) \) time (exercise 7.8 of Morin): the same tree that the insertions of \( A \) in order into Treap( seed ) build, since the priorities are drawn in the same order, and a heap on them built by the stack of the right spine
Static public member functions inherited from ADS::Joinable< Treap< K, V >, TreapNodes< K, V >, K, V >
static Treap< K, V > join (Treap< K, V > &&l, const K &k, const V &v, Treap< K, V > &&r)
 the tree of the keys of \( l \), the pair \( ( k , v ) \) and the keys of \( r \), by the join of the scheme; \( l \) and \( r \) are consumed, in \( O( | \log n_l - \log n_r | + 1 ) \) time
static Treap< K, V > join2 (Treap< K, V > &&l, Treap< K, V > &&r)
 the tree of the keys of \( l \) and of \( r \), which are consumed, by JoinAlgorithms::join2(), in \( O( \log n ) \) time

Friends

struct TreapNodes< K, V >

Detailed description

template<std::totally_ordered K, class V>
class ADS::Treap< K, V >

a DICTIONARY on a treap, with the operations based on join [see Joinable]

Member typedef documentation

◆ const_iterator

template<std::totally_ordered K, class V>
using ADS::Treap< K, V >::const_iterator = PathIterator< Treap , Shape >

the constant iterator by increasing key, along the path from the root

◆ const_reference

template<std::totally_ordered K, class V>
using ADS::Treap< K, V >::const_reference = reference

the same

◆ difference_type

template<std::totally_ordered K, class V>
using ADS::Treap< K, V >::difference_type = std::ptrdiff_t

the type of the distances

◆ iterator

template<std::totally_ordered K, class V>
using ADS::Treap< K, V >::iterator = const_iterator

the constant iterator

◆ Key

template<std::totally_ordered K, class V>
using ADS::Treap< K, V >::Key = K

the type of the keys

◆ key_type

template<std::totally_ordered K, class V>
using ADS::Treap< K, V >::key_type = K

the type of the keys

◆ mapped_type

template<std::totally_ordered K, class V>
using ADS::Treap< K, V >::mapped_type = V

the type of the values

◆ reference

template<std::totally_ordered K, class V>
using ADS::Treap< K, V >::reference = PairRef< const K & , const V & >

references, by value

◆ size_type

template<std::totally_ordered K, class V>
using ADS::Treap< K, V >::size_type = std::size_t

the type of the sizes

◆ Value

template<std::totally_ordered K, class V>
using ADS::Treap< K, V >::Value = V

the type of the values

◆ value_type

template<std::totally_ordered K, class V>
using ADS::Treap< K, V >::value_type = std::pair< K , V >

the pairs ( key , value ) by value, which own their copies

Constructor and destructor documentation

◆ Treap()

template<std::totally_ordered K, class V>
ADS::Treap< K, V >::Treap ( std::uint64_t seed = 1)
explicit

the empty treap, its priorities drawn from the seed

Member function documentation

◆ begin()

template<std::totally_ordered K, class V>
const_iterator ADS::Treap< K, V >::begin ( void ) const
inlinenodiscard

the iterator on the smallest key

◆ cbegin()

template<std::totally_ordered K, class V>
const_iterator ADS::Treap< K, V >::cbegin ( void ) const
inlinenodiscard

as begin()

◆ cend()

template<std::totally_ordered K, class V>
const_iterator ADS::Treap< K, V >::cend ( void ) const
inlinenodiscard

as end()

◆ contains()

template<std::totally_ordered K, class V>
bool ADS::Treap< K, V >::contains ( const K & k) const
nodiscard

true if \( k \) is there

◆ end()

template<std::totally_ordered K, class V>
const_iterator ADS::Treap< K, V >::end ( void ) const
inlinenodiscard

the iterator past the largest key

◆ findKey()

template<std::totally_ordered K, class V>
const K * ADS::Treap< K, V >::findKey ( const K & k) const
nodiscard

the address of the key \( k \), nil if absent, in \( O( \log n ) \) expected time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed

◆ forEach()

template<std::totally_ordered K, class V>
template<class F>
void ADS::Treap< K, V >::forEach ( F && f) const

f( k , v ) on the pairs by increasing keys

◆ fromSorted()

template<std::totally_ordered K, class V>
Treap ADS::Treap< K, V >::fromSorted ( const std::vector< std::pair< K, V > > & A,
std::uint64_t seed = 1 )
staticnodiscard

the treap of the pairs of \( A \), by increasing keys, in \( O( n ) \) time (exercise 7.8 of Morin): the same tree that the insertions of \( A \) in order into Treap( seed ) build, since the priorities are drawn in the same order, and a heap on them built by the stack of the right spine

In pseudocode (Vuillemin's Cartesian tree, exercise 7.8 of Morin), \( S \) the right spine of the tree of the pairs read so far:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fromSorted}( A ) \cr 2 & \quad \textbf{foreach}\ ( k , v ) \in A\ \textbf{do} \cr 3 & \qquad u \gets \text{a node of } ( k , v ) \text{ and a random} \text{ priority};\ \ell \gets \textbf{nil} \cr 4 & \qquad \textbf{while}\ S \ne \emptyset\ \textbf{and}\ p( \mathrm{top}( S ) ) < p( u )\ \textbf{do}\ \ell \gets \mathrm{pop}( S ) \cr 5 & \qquad u.\mathit{left} \gets \ell;\ \textbf{if}\ S \ne \emptyset\ \textbf{then}\ \mathrm{top}( S ).\mathit{right} \gets u \cr 6 & \qquad \mathrm{push}( S , u ) \cr 7 & \quad \textbf{return}\ \text{the bottom of } S \end{array}} \]

Precondition
the keys of \( A \) strictly increasing

◆ height()

template<std::totally_ordered K, class V>
int ADS::Treap< K, V >::height ( void ) const
nodiscard

the height, -1 if empty

◆ insert()

template<std::totally_ordered K, class V>
void ADS::Treap< K, V >::insert ( const K & k,
const V & v )

associates \( v \) to \( k \), in \( O( \log n ) \) expected time

In pseudocode (Seidel and Aragon, beyond the book), \( p \) the priorities, a new key rotated up while its priority beats its parent's:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( t , k , v ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then}\ t \gets \text{a new node of } ( k , v ) \text{ and a random priority}; \ \textbf{return} \cr 3 & \quad \textbf{if}\ k < t.\mathit{key}\ \textbf{then} \cr 4 & \qquad \mathrm{insert}( t.\mathit{left} , k , v ) \cr 5 & \qquad \textbf{if}\ p( t.\mathit{left} ) > p( t )\ \textbf{then} \ \mathrm{rotateRight}( t ) \cr 6 & \quad \textbf{else if}\ t.\mathit{key} < k\ \textbf{then} \ \text{the mirror of the lines 4 and 5} \cr 7 & \quad \textbf{else}\ t.\mathit{value} \gets v \end{array}} \]

Postcondition
\( k \) has the value \( v \)

◆ lookup()

template<std::totally_ordered K, class V>
std::optional< V > ADS::Treap< K, V >::lookup ( const K & k) const
nodiscard

the value of \( k \), or nothing, by the descent of a binary search tree, in \( O( \log n ) \) expected time

◆ lowerBound()

template<std::totally_ordered K, class V>
std::optional< std::pair< K, V > > ADS::Treap< K, V >::lowerBound ( const K & k) const
nodiscard

the smallest key not smaller than \( k \) and its value, or nothing (the find(x) of an SSet of Morin), in \( O( \log n ) \) expected time

◆ lowerBoundKey()

template<std::totally_ordered K, class V>
const K * ADS::Treap< K, V >::lowerBoundKey ( const K & k) const
nodiscard

the address of the smallest key not smaller than \( k \), nil if none, in \( O( \log n ) \) expected time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed

◆ rank()

template<std::totally_ordered K, class V>
std::size_t ADS::Treap< K, V >::rank ( const K & k) const
nodiscard

the number of keys smaller than \( k \), the sizes of the left subtrees passed on the right along the search of \( k \), in \( O( \log n ) \) expected time

◆ remove()

template<std::totally_ordered K, class V>
void ADS::Treap< K, V >::remove ( const K & k)

removes \( k \), if it is there, in \( O( \log n ) \) expected time

In pseudocode (Seidel and Aragon, beyond the book), the key rotated down, the child of higher priority going up, until it has one child at most:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( t , k ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return} \cr 3 & \quad \textbf{if}\ k < t.\mathit{key}\ \textbf{then} \ \mathrm{remove}( t.\mathit{left} , k ) \cr 4 & \quad \textbf{else if}\ t.\mathit{key} < k\ \textbf{then} \ \mathrm{remove}( t.\mathit{right} , k ) \cr 5 & \quad \textbf{else if}\ t \text{ has one child at most} \ \textbf{then}\ t \gets \text{that child} \cr 6 & \quad \textbf{else if}\ p( t.\mathit{left} ) > p( t.\mathit{right} )\ \textbf{then}\ \mathrm{rotateRight}( t );\ \mathrm{remove}( t.\mathit{right} , k ) \cr 7 & \quad \textbf{else}\ \mathrm{rotateLeft}( t );\ \mathrm{remove}( t.\mathit{left} , k ) \end{array}} \]

Postcondition
\( k \) is not there

◆ select()

template<std::totally_ordered K, class V>
std::pair< K, V > ADS::Treap< K, V >::select ( std::size_t i) const
nodiscard

the pair of rank \( i \), i.e., with \( i \) smaller keys, in \( O( \log n ) \) expected time (exercise 7.10 of Morin)

In pseudocode (exercise 7.10 of Morin), \( s \) the sizes:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{select}( t , i ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad \textbf{if}\ i < s( t.\mathit{left} )\ \textbf{then}\ t \gets t.\mathit{left} \cr 4 & \qquad \textbf{else if}\ i = s( t.\mathit{left} )\ \textbf{then return}\ t \cr 5 & \qquad \textbf{else}\ i \gets i - s( t.\mathit{left} ) - 1;\ t \gets t.\mathit{right} \end{array}} \]

Precondition
\( i < n \)

◆ size()

template<std::totally_ordered K, class V>
std::size_t ADS::Treap< K, V >::size ( void ) const
nodiscard

the number \( n \) of keys

◆ TreapNodes< K, V >

template<std::totally_ordered K, class V>
friend struct TreapNodes< K, V >
friend

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