|
| | 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()
|
| 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 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 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
|
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]
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
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 \)
template<std::totally_ordered K, class V>
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