a DICTIONARY on the left-leaning red-black tree of Morin, a simulated 2-4 tree
More...
|
| | LeftLeaningRedBlackTree (void)=default |
| | the empty tree
|
| | LeftLeaningRedBlackTree (const LeftLeaningRedBlackTree &)=delete |
| | no copy, since the nodes are linked by pointers
|
| LeftLeaningRedBlackTree & | operator= (const LeftLeaningRedBlackTree &)=delete |
| | no copy, since the nodes are linked by pointers
|
| | LeftLeaningRedBlackTree (LeftLeaningRedBlackTree &&other) noexcept |
| | the nodes of other, which is left empty
|
| | ~LeftLeaningRedBlackTree () |
| | frees the nodes
|
| std::optional< V > | lookup (const K &k) const |
| | the value of \( k \), or nothing, 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::size_t | size (void) const |
| | the number \( n \) of keys
|
| long | height (void) const |
| | the height, \( -1 \) if empty, in \( O( n ) \)
|
| long | blackHeight (void) const |
| | the black height, the black nodes on a path from the root to nil
|
| std::array< std::size_t, 3 > | twoFourDegrees (void) const |
| | the numbers of the nodes of degree 2, 3 and 4 of the 2-4 tree that the tree simulates (exercises 9.1 and 9.8 of Morin), 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 ) \) 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
|
| void | absorb (LeftLeaningRedBlackTree &t) |
| | the pairs of \( t \) appended and \( t \) left empty, in \( O( h + h_t ) \) time (exercises 9.10 and 9.11 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::LeftLeaningRedBlackTree< K, V >
a DICTIONARY on the left-leaning red-black tree of Morin, a simulated 2-4 tree
template<std::totally_ordered K, class V>
the pairs of \( t \) appended and \( t \) left empty, in \( O( h + h_t ) \) time (exercises 9.10 and 9.11 of Morin)
In pseudocode (exercises 9.10 and 9.11 of Morin, beyond the book), \( b \) the black heights, for \( b( T ) \ge b( t ) \) (the other case along the left spine of \( t \)):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{absorb}( t ) \cr
2 & \quad m \gets \text{the smallest node of } t \text{, removed from }
t \cr
3 & \quad c \gets \text{the first black node of black height } b( t )
\text{ on the right spine of } T \cr
4 & \quad m \text{ red};\ m.\mathit{left} \gets c;\ m.\mathit{right}
\gets t.\mathit{root};\ m \text{ takes the place of } c \cr
5 & \quad \mathrm{addFixup}( m )
\end{array}}
\]
- Precondition
- the keys of \( t \) larger than those of this tree
template<std::totally_ordered K, class V>
associates \( v \) to \( k \), in \( O( \log n ) \) time
In pseudocode (section 9.2.3 of Morin), a red leaf \( u \) added and the properties restored going up:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{addFixup}( u ) \cr
2 & \quad \textbf{while}\ u \text{ is red}\ \textbf{do} \cr
3 & \qquad \textbf{if}\ u \text{ is the root}\ \textbf{then}\ u
\text{ black};\ \textbf{return} \cr
4 & \qquad w \gets u.\mathit{parent} \cr
5 & \qquad \textbf{if}\ w.\mathit{left} \text{ is black}\ \textbf{then}
\ \mathrm{flipLeft}( w );\ u \gets w;\ w \gets u.\mathit{parent} \cr
6 & \qquad \textbf{if}\ w \text{ is black}\ \textbf{then return} \cr
7 & \qquad g \gets w.\mathit{parent} \cr
8 & \qquad \textbf{if}\ g.\mathit{right} \text{ is black}\ \textbf{then}
\ \mathrm{flipRight}( g );\ \textbf{return} \cr
9 & \qquad \mathrm{pushBlack}( g );\ u \gets g
\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 (section 9.2.4 of Morin), \( u \) the child of the spliced node with the color of the two summed (2 is double black), \( v \) its sibling and \( w \) its parent:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{removeFixup}( u ) \cr
2 & \quad \textbf{while}\ u.\mathit{color} > 1\ \textbf{do} \cr
3 & \qquad \textbf{if}\ u \text{ is the root}\ \textbf{then}\ u
\text{ black} \cr
4 & \qquad \textbf{else if}\ v \text{ is red}\ \textbf{then}
\ \mathrm{flipRight}( w ) \quad \triangleright\ \text{case 1, }
\text{then case 3} \cr
5 & \qquad \textbf{else if}\ u = w.\mathit{left}\ \textbf{then}\ u
\gets \mathrm{case2}( u ) \cr
6 & \qquad \textbf{else}\ u \gets \mathrm{case3}( u ) \cr
7 & \quad \textbf{if}\ u \text{ is not the root and } w.\mathit{right}
\text{ is red and } w.\mathit{left} \text{ black}\ \textbf{then}
\ \mathrm{flipLeft}( w ) \cr
8 & \textbf{function}\ \mathrm{case2}( u ) \cr
9 & \quad \mathrm{pullBlack}( w );\ \mathrm{flipLeft}( w );\ q \gets
w.\mathit{right} \cr
10 & \quad \textbf{if}\ q \text{ is black}\ \textbf{then return}\ v \cr
11 & \quad \mathrm{rotateLeft}( w );\ \mathrm{flipRight}( v );
\ \mathrm{pushBlack}( q ) \cr
12 & \quad \textbf{if}\ v.\mathit{right} \text{ is red}\ \textbf{then}
\ \mathrm{flipLeft}( v ) \cr
13 & \quad \textbf{return}\ q \cr
14 & \textbf{function}\ \mathrm{case3}( u ) \cr
15 & \quad \mathrm{pullBlack}( w );\ \mathrm{flipRight}( w );\ q \gets
w.\mathit{left} \cr
16 & \quad \textbf{if}\ q \text{ is red}\ \textbf{then}
\ \mathrm{rotateRight}( w );\ \mathrm{flipLeft}( v );
\ \mathrm{pushBlack}( q );\ \textbf{return}\ q \cr
17 & \quad \textbf{if}\ v.\mathit{left} \text{ is red}\ \textbf{then}
\ \mathrm{pushBlack}( v );\ \textbf{return}\ v \cr
18 & \quad \mathrm{flipLeft}( v );\ \textbf{return}\ w
\end{array}}
\]
- Postcondition
- \( k \) is not there