|
| | SplayTree (void)=default |
| | the empty tree
|
| | SplayTree (const SplayTree &)=delete |
| | no copy
|
| SplayTree & | operator= (const SplayTree &)=delete |
| | no copy
|
| | SplayTree (SplayTree &&other) noexcept |
| | the nodes of other, which is left empty
|
| | ~SplayTree () |
| | frees the nodes
|
| std::optional< V > | lookup (const K &k) |
| | the value of \( k \), or nothing; \( k \), or the last node on its path, goes to the root, in \( O( \log n ) \) amortized time
|
| void | insert (const K &k, const V &v) |
| | associates \( v \) to \( k \), which goes to the root, in \( O( \log n ) \) amortized time
|
| void | remove (const K &k) |
| | removes \( k \), if it is there, in \( O( \log n ) \) amortized time
|
| bool | contains (const K &k) |
| | true if \( k \) is there; \( k \), or the last node on its path, goes to the root, in \( O( \log n ) \) amortized time
|
| std::optional< std::pair< K, V > > | lowerBound (const K &k) |
| | the smallest key not smaller than \( k \) and its value, or nothing (the find( x ) of an SSet of Morin), by lowerBoundNode() in \( O( \log n ) \) amortized time
|
| const K * | findKey (const K &k) |
| | the address of the key \( k \), nil if absent, splayed as by lookup() in \( O( \log n ) \) amortized time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed
|
| const K * | lowerBoundKey (const K &k) |
| | the address of the smallest key not smaller than \( k \), nil if none, by lowerBoundNode() in \( O( \log n ) \) amortized time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed
|
| std::size_t | size (void) const |
| | the number \( n \) of keys
|
| std::optional< K > | root (void) const |
| | the key at the root, if any
|
| 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::SplayTree< K, V >
a DICTIONARY on a splay tree
template<std::totally_ordered K, class V>
associates \( v \) to \( k \), which goes to the root, in \( O( \log n ) \) amortized time
In pseudocode (Sleator and Tarjan, beyond the book), on the splay() of lookup():
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{insert}( k , v ) \cr
2 & \quad T \gets \mathrm{splay}( T , k ) \cr
3 & \quad \textbf{if}\ T \ne \textbf{nil}\ \textbf{and}
\ T.\mathit{key} = k\ \textbf{then}\ T.\mathit{value} \gets v;
\ \textbf{return} \cr
4 & \quad n \gets \text{a new node of } ( k , v ) \cr
5 & \quad \textbf{if}\ k < T.\mathit{key}\ \textbf{then}\ (
n.\mathit{left} , n.\mathit{right} ) \gets ( T.\mathit{left} , T
);\ T.\mathit{left} \gets \textbf{nil} \cr
6 & \quad \textbf{else}\ ( n.\mathit{left} , n.\mathit{right} ) \gets (
T , T.\mathit{right} );\ T.\mathit{right} \gets \textbf{nil} \cr
7 & \quad T \gets n
\end{array}}
\]
- Postcondition
- \( k \) has the value \( v \), at the root
template<std::totally_ordered K, class V>
the value of \( k \), or nothing; \( k \), or the last node on its path, goes to the root, in \( O( \log n ) \) amortized time
In pseudocode (the top-down splay of Sleator and Tarjan, beyond the book), \( L \) and \( R \) the trees of the keys smaller and larger than \( k \) met on the way, \( L \) growing at its largest key and \( R \) at its smallest:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{splay}( t , k ) \cr
2 & \quad L \gets R \gets \textbf{nil} \cr
3 & \quad \textbf{loop} \cr
4 & \qquad \textbf{if}\ k < t.\mathit{key}\ \textbf{then} \cr
5 & \qquad\quad \textbf{if}\ t.\mathit{left} = \textbf{nil}
\ \textbf{then break} \cr
6 & \qquad\quad \textbf{if}\ k < t.\mathit{left}.\mathit{key}
\ \textbf{then}\ t \gets \mathrm{rotateRight}( t )\quad
\triangleright\ \text{zig-zig} \cr
7 & \qquad\quad \textbf{if}\ t.\mathit{left} = \textbf{nil}
\ \textbf{then break} \cr
8 & \qquad\quad \text{hang } t \text{ at the smallest key of } R;\ t
\gets t.\mathit{left}\quad \triangleright\ \text{link right} \cr
9 & \qquad \textbf{else if}\ t.\mathit{key} < k\ \textbf{then}
\ \text{the mirror of the lines 5 to 8, on } L \cr
10 & \qquad \textbf{else break} \cr
11 & \quad \text{hang } t.\mathit{left} \text{ at the largest key of }
L \text{, } t.\mathit{right} \text{ at the smallest of } R \cr
12 & \quad t.\mathit{left} \gets L;\ t.\mathit{right} \gets R;
\ \textbf{return}\ t
\end{array}}
\]
and lookup() finds \( k \) at the root after splay( root , k ), or nowhere.
template<std::totally_ordered K, class V>
removes \( k \), if it is there, in \( O( \log n ) \) amortized time
In pseudocode (Sleator and Tarjan, beyond the book), the largest key of the left part splayed to its root, which then has no right child:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{remove}( k ) \cr
2 & \quad T \gets \mathrm{splay}( T , k ) \cr
3 & \quad \textbf{if}\ T = \textbf{nil}\ \textbf{or}\ T.\mathit{key}
\ne k\ \textbf{then return} \cr
4 & \quad \textbf{if}\ T.\mathit{left} = \textbf{nil}\ \textbf{then}
\ T' \gets T.\mathit{right} \cr
5 & \quad \textbf{else}\ T' \gets \mathrm{splay}( T.\mathit{left} , k
);\ T'.\mathit{right} \gets T.\mathit{right} \cr
6 & \quad \text{free } T;\ T \gets T'
\end{array}}
\]
- Postcondition
- \( k \) is not there