|
ADS
Algorithms and Data Structures
|
a DICTIONARY on a randomized binary search tree of Martínez and Roura More...
#include <ADS/tree/RandomizedSearchTree.h>
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< RandomizedSearchTree , Shape > |
| the constant iterator by increasing key, along the path from the root | |
| using | iterator = const_iterator |
| the constant iterator | |
Public member functions | |
| RandomizedSearchTree (std::uint64_t seed=1) | |
| the empty tree, its random choices drawn from the seed | |
| std::optional< V > | lookup (const K &k) const |
| the value of \( k \), or nothing, 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, in \( O( n ) \) | |
| 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) | |
| 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 \), by the sizes, as Treap::select() | |
| std::size_t | rank (const K &k) const |
| the number of keys smaller than \( k \), by the sizes | |
| template<class F> | |
| void | forEach (F &&f) const |
| f( k , v ) on the pairs by increasing keys | |
| 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() | |
a DICTIONARY on a randomized binary search tree of Martínez and Roura
| using ADS::RandomizedSearchTree< K, V >::const_iterator = PathIterator< RandomizedSearchTree , Shape > |
the constant iterator by increasing key, along the path from the root
| using ADS::RandomizedSearchTree< K, V >::const_reference = reference |
the same
| using ADS::RandomizedSearchTree< K, V >::difference_type = std::ptrdiff_t |
the type of the distances
| using ADS::RandomizedSearchTree< K, V >::iterator = const_iterator |
the constant iterator
| using ADS::RandomizedSearchTree< K, V >::Key = K |
the type of the keys
| using ADS::RandomizedSearchTree< K, V >::key_type = K |
the type of the keys
| using ADS::RandomizedSearchTree< K, V >::mapped_type = V |
the type of the values
| using ADS::RandomizedSearchTree< K, V >::reference = PairRef< const K & , const V & > |
references, by value
| using ADS::RandomizedSearchTree< K, V >::size_type = std::size_t |
the type of the sizes
| using ADS::RandomizedSearchTree< K, V >::Value = V |
the type of the values
| using ADS::RandomizedSearchTree< K, V >::value_type = std::pair< K , V > |
the pairs ( key , value ) by value, which own their copies
|
inlineexplicit |
the empty tree, its random choices drawn from the seed
|
inlinenodiscard |
the iterator on the smallest key
|
inlinenodiscard |
as begin()
|
inlinenodiscard |
as end()
|
nodiscard |
true if \( k \) is there
|
inlinenodiscard |
the iterator past the largest key
|
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
| void ADS::RandomizedSearchTree< K, V >::forEach | ( | F && | f | ) | const |
f( k , v ) on the pairs by increasing keys
|
nodiscard |
the height, \( -1 \) if empty, in \( O( n ) \)
| void ADS::RandomizedSearchTree< K, V >::insert | ( | const K & | k, |
| const V & | v ) |
associates \( v \) to \( k \), in \( O( \log n ) \) expected time
In pseudocode (Martínez and Roura, beyond the book), \( s \) the sizes, recomputed from the children at each node that a split returns, after a search that found \( k \) absent:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( t , k , v ) \cr 2 & \quad \textbf{if}\ \mathrm{below}( s( t ) + 1 ) = 0\ \textbf{then} \cr 3 & \qquad ( a , b ) \gets \mathrm{split}( t , k );\ \textbf{return} \ \text{a node of } ( k , v ) \text{ over } a \text{ and } b \cr 4 & \quad \textbf{if}\ k < t.\mathit{key}\ \textbf{then} \ t.\mathit{left} \gets \mathrm{insert}( t.\mathit{left} , k , v ) \cr 5 & \quad \textbf{else}\ t.\mathit{right} \gets \mathrm{insert}( t.\mathit{right} , k , v ) \cr 6 & \quad s( t ) \gets s( t ) + 1;\ \textbf{return}\ t \cr 7 & \textbf{function}\ \mathrm{split}( t , k ) \cr 8 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return} \ ( \textbf{nil} , \textbf{nil} ) \cr 9 & \quad \textbf{if}\ k < t.\mathit{key}\ \textbf{then}\ ( a , b ) \gets \mathrm{split}( t.\mathit{left} , k );\ t.\mathit{left} \gets b;\ \textbf{return}\ ( a , t ) \cr 10 & \quad ( a , b ) \gets \mathrm{split}( t.\mathit{right} , k ); \ t.\mathit{right} \gets a;\ \textbf{return}\ ( t , b ) \end{array}} \]
|
nodiscard |
the value of \( k \), or nothing, in \( O( \log n ) \) expected time
|
nodiscard |
the smallest key not smaller than \( k \) and its value, or nothing (the find(x) of an SSet of Morin)
|
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
|
nodiscard |
the number of keys smaller than \( k \), by the sizes
| void ADS::RandomizedSearchTree< K, V >::remove | ( | const K & | k | ) |
removes \( k \), if it is there, in \( O( \log n ) \) expected time
In pseudocode (Martínez and Roura, beyond the book), the node of \( k \) replaced by the join of its subtrees, and the size of each node that a join returns recomputed from its children:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{join}( a , b ) \cr 2 & \quad \textbf{if}\ a = \textbf{nil}\ \textbf{or}\ b = \textbf{nil} \ \textbf{then return}\ \text{the other} \cr 3 & \quad \textbf{if}\ \mathrm{below}( s( a ) + s( b ) ) < s( a ) \ \textbf{then}\ a.\mathit{right} \gets \mathrm{join}( a.\mathit{right} , b );\ \textbf{return}\ a \cr 4 & \quad b.\mathit{left} \gets \mathrm{join}( a , b.\mathit{left} ); \ \textbf{return}\ b \end{array}} \]
|
nodiscard |
the pair of rank \( i \), by the sizes, as Treap::select()
|
inlinenodiscard |
the number \( n \) of keys