|
| 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 ) \) time
|
| void | insert (const K &k, const V &v) |
| | associates \( v \) to \( k \), in \( O( \log n ) \) time
|
| void | remove (const K &k) |
| | removes \( k \), if it is there, in \( O( \log n ) \) time
|
| bool | contains (const K &k) const |
| | true if \( k \) is there
|
| 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 ) \) time
|
| const K * | findKey (const K &k) const |
| | the address of the key \( k \), nil if absent, in \( O( \log n ) \) 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 ) \) 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
|
| bool | empty (void) const |
| | true if there is no key
|
| int | height (void) const |
| | the height of the tree, -1 if it is empty
|
| template<class F> |
| void | forEach (F &&f) const |
| | f( k , v ) on the pairs by increasing keys
|
| std::pair< std::size_t, std::size_t > | rebalancings (void) const |
| | the rebalancings of insert() and remove() so far, i.e., the nodes found unbalanced on their paths and mended by one or two rotations, and those rotations (exercise 9.12 of Morin)
|
| 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< AVLTree< K, V >, std::optional< V >, AVLTree< 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
|
| AVLTree< 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 (AVLTree< 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 (AVLTree< 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 (AVLTree< 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
|
template<std::totally_ordered K, class V>
class ADS::AVLTree< K, V >
a DICTIONARY on an AVL tree, with the operations based on join [see Joinable]
template<std::totally_ordered K, class V>
| void ADS::AVLTree< K, V >::insert |
( |
const K & | k, |
|
|
const V & | v ) |
associates \( v \) to \( k \), in \( O( \log n ) \) time
In pseudocode (Knuth, section 6.2.3, beyond the book), recursive, \( h( \textbf{nil} ) = -1 \), and the balance of a node restored by a single or a double rotation on the way back:
\[\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 );\ \textbf{return} \cr
3 & \quad \textbf{if}\ k < t.\mathit{key}\ \textbf{then}
\ \mathrm{insert}( t.\mathit{left} , k , v ) \cr
4 & \quad \textbf{else if}\ t.\mathit{key} < k\ \textbf{then}
\ \mathrm{insert}( t.\mathit{right} , k , v ) \cr
5 & \quad \textbf{else}\ t.\mathit{value} \gets v \cr
6 & \quad \mathrm{balance}( t ) \cr
7 & \textbf{procedure}\ \mathrm{balance}( t ) \cr
8 & \quad \text{update the height and the size of } t \cr
9 & \quad \textbf{if}\ h( t.\mathit{left} ) - h( t.\mathit{right} ) > 1
\ \textbf{then} \cr
10 & \qquad \textbf{if}\ h( t.\mathit{left}.\mathit{left} ) < h(
t.\mathit{left}.\mathit{right} )\ \textbf{then}\ \mathrm{rotateLeft}(
t.\mathit{left} ) \cr
11 & \qquad \mathrm{rotateRight}( t ) \cr
12 & \quad \textbf{else if}\ h( t.\mathit{right} ) - h(
t.\mathit{left} ) > 1\ \textbf{then}\ \text{the mirror of the }
\text{lines 10 and 11}
\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 ) \) time
In pseudocode (Knuth, section 6.2.3, beyond the book), the removal of a binary search tree with the balance() of insert() on the way back:
\[\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};\ \textbf{return} \cr
6 & \quad \textbf{else} \cr
7 & \qquad s \gets \text{the node of the smallest key of }
t.\mathit{right} \cr
8 & \qquad ( t.\mathit{key} , t.\mathit{value} ) \gets (
s.\mathit{key} , s.\mathit{value} );\ \mathrm{remove}(
t.\mathit{right} , s.\mathit{key} ) \cr
9 & \quad \mathrm{balance}( t )
\end{array}}
\]
- Postcondition
- \( k \) is not there