a DICTIONARY on a binary search tree whose nodes keep the size, the depth and the height of their subtree
More...
|
| | MeasuredSearchTree (void)=default |
| | the empty tree
|
| | MeasuredSearchTree (const MeasuredSearchTree &)=delete |
| | no copy, since the nodes are linked by pointers
|
| MeasuredSearchTree & | operator= (const MeasuredSearchTree &)=delete |
| | no copy, since the nodes are linked by pointers
|
| | MeasuredSearchTree (MeasuredSearchTree &&other) noexcept |
| | the nodes of other, which is left empty
|
| | ~MeasuredSearchTree () |
| | frees the nodes
|
| std::optional< V > | lookup (const K &k) const |
| | the value of \( k \), or nothing, in \( O( h ) \) time
|
| void | insert (const K &k, const V &v) |
| | associates \( v \) to \( k \), in \( O( h ) \) time
|
| void | remove (const K &k) |
| | removes \( k \), if it is there, in \( O( h ) \) time
|
| bool | contains (const K &k) const |
| | true if \( k \) is there
|
| std::size_t | size (void) const |
| | the number \( n \) of keys
|
| long | height (void) const |
| | the height, \( -1 \) if empty, read at the root in \( O( 1 ) \)
|
| std::optional< Measures > | measures (const K &k) const |
| | the measures of the node of \( k \), or nothing, read in it
|
| 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), in \( O( h ) \) time
|
| const K * | findKey (const K &k) const |
| | the address of the key \( k \), nil if absent, in \( O( h ) \) 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( h ) \) time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed
|
| std::vector< std::pair< K, V > > | getLE (const K &x) const |
| | the pairs whose keys are not larger than \( x \), in increasing order, by lessOrEqual(), in \( O( n' + h ) \) time for \( n' \) pairs (the getLE(x) of exercise 6.14 of Morin)
|
| 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()
|
template<std::totally_ordered K, class V>
class ADS::MeasuredSearchTree< K, V >
a DICTIONARY on a binary search tree whose nodes keep the size, the depth and the height of their subtree
template<std::totally_ordered K, class V>
associates \( v \) to \( k \), in \( O( h ) \) time
In pseudocode (exercise 6.19 of Morin), \( s \), \( d \) and \( h \) the three measures:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{insert}( k , v ) \cr
2 & \quad p \gets \text{the last node of the search of } k \text{;
if it holds } k \text{, its value} \gets v;\ \textbf{return} \cr
3 & \quad u \gets \text{a new leaf of } ( k , v ) \text{ under } p;
\ u.s \gets 1;\ u.h \gets 0;\ u.d \gets p.d + 1 \cr
4 & \quad \textbf{for}\ w \gets p\ \textbf{up to the root do}\ w.s
\gets w.s + 1;\ w.h \gets 1 + \max\{ w.\mathit{left}.h ,
w.\mathit{right}.h \}
\end{array}}
\]
- Postcondition
- \( k \) has the value \( v \)
template<std::totally_ordered K, class V>
removes \( k \), if it is there, in \( O( h ) \) time
In pseudocode (exercise 6.19 of Morin, beyond the book), the pair of the node replaced by that of its successor or predecessor until a leaf is reached:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{remove}( k ) \cr
2 & \quad u \gets \text{the node of } k;\ \textbf{if}\ u =
\textbf{nil}\ \textbf{then return} \cr
3 & \quad \textbf{while}\ u \text{ is not a leaf}\ \textbf{do} \cr
4 & \qquad w \gets \text{the leftmost node below } u.\mathit{right}
\text{, or the rightmost below } u.\mathit{left} \cr
5 & \qquad u.\mathit{pair} \gets w.\mathit{pair};\ u \gets w \cr
6 & \quad p \gets u.\mathit{parent};\ \text{free the leaf } u \cr
7 & \quad \textbf{for}\ w \gets p\ \textbf{up to the root do}\ w.s
\gets w.s - 1;\ w.h \gets 1 + \max\{ w.\mathit{left}.h ,
w.\mathit{right}.h \}
\end{array}}
\]
- Postcondition
- \( k \) is not there