ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::LeftLeaningRedBlackTree< K, V > Class Template Reference

a DICTIONARY on the left-leaning red-black tree of Morin, a simulated 2-4 tree More...

#include <ADS/tree/LeftLeaningRedBlackTree.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< LeftLeaningRedBlackTree , Shape >
 the constant iterator by increasing key, along the path from the root
using iterator = const_iterator
 the constant iterator

Public member functions

 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()

Detailed description

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

Member typedef documentation

◆ const_iterator

template<std::totally_ordered K, class V>
using ADS::LeftLeaningRedBlackTree< K, V >::const_iterator = PathIterator< LeftLeaningRedBlackTree , Shape >

the constant iterator by increasing key, along the path from the root

◆ const_reference

template<std::totally_ordered K, class V>
using ADS::LeftLeaningRedBlackTree< K, V >::const_reference = reference

the same

◆ difference_type

template<std::totally_ordered K, class V>
using ADS::LeftLeaningRedBlackTree< K, V >::difference_type = std::ptrdiff_t

the type of the distances

◆ iterator

template<std::totally_ordered K, class V>
using ADS::LeftLeaningRedBlackTree< K, V >::iterator = const_iterator

the constant iterator

◆ Key

template<std::totally_ordered K, class V>
using ADS::LeftLeaningRedBlackTree< K, V >::Key = K

the type of the keys

◆ key_type

template<std::totally_ordered K, class V>
using ADS::LeftLeaningRedBlackTree< K, V >::key_type = K

the type of the keys

◆ mapped_type

template<std::totally_ordered K, class V>
using ADS::LeftLeaningRedBlackTree< K, V >::mapped_type = V

the type of the values

◆ reference

template<std::totally_ordered K, class V>
using ADS::LeftLeaningRedBlackTree< K, V >::reference = PairRef< const K & , const V & >

references, by value

◆ size_type

template<std::totally_ordered K, class V>
using ADS::LeftLeaningRedBlackTree< K, V >::size_type = std::size_t

the type of the sizes

◆ Value

template<std::totally_ordered K, class V>
using ADS::LeftLeaningRedBlackTree< K, V >::Value = V

the type of the values

◆ value_type

template<std::totally_ordered K, class V>
using ADS::LeftLeaningRedBlackTree< K, V >::value_type = std::pair< K , V >

the pairs ( key , value ) by value, which own their copies

Constructor and destructor documentation

◆ LeftLeaningRedBlackTree() [1/3]

template<std::totally_ordered K, class V>
ADS::LeftLeaningRedBlackTree< K, V >::LeftLeaningRedBlackTree ( void )
default

the empty tree

◆ LeftLeaningRedBlackTree() [2/3]

template<std::totally_ordered K, class V>
ADS::LeftLeaningRedBlackTree< K, V >::LeftLeaningRedBlackTree ( const LeftLeaningRedBlackTree< K, V > & )
delete

no copy, since the nodes are linked by pointers

◆ LeftLeaningRedBlackTree() [3/3]

template<std::totally_ordered K, class V>
ADS::LeftLeaningRedBlackTree< K, V >::LeftLeaningRedBlackTree ( LeftLeaningRedBlackTree< K, V > && other)
inlinenoexcept

the nodes of other, which is left empty

◆ ~LeftLeaningRedBlackTree()

template<std::totally_ordered K, class V>
ADS::LeftLeaningRedBlackTree< K, V >::~LeftLeaningRedBlackTree ( )

frees the nodes

Member function documentation

◆ absorb()

template<std::totally_ordered K, class V>
void ADS::LeftLeaningRedBlackTree< K, V >::absorb ( LeftLeaningRedBlackTree< K, V > & t)

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

◆ begin()

template<std::totally_ordered K, class V>
const_iterator ADS::LeftLeaningRedBlackTree< K, V >::begin ( void ) const
inlinenodiscard

the iterator on the smallest key

◆ blackHeight()

template<std::totally_ordered K, class V>
long ADS::LeftLeaningRedBlackTree< K, V >::blackHeight ( void ) const
nodiscard

the black height, the black nodes on a path from the root to nil

◆ cbegin()

template<std::totally_ordered K, class V>
const_iterator ADS::LeftLeaningRedBlackTree< K, V >::cbegin ( void ) const
inlinenodiscard

as begin()

◆ cend()

template<std::totally_ordered K, class V>
const_iterator ADS::LeftLeaningRedBlackTree< K, V >::cend ( void ) const
inlinenodiscard

as end()

◆ contains()

template<std::totally_ordered K, class V>
bool ADS::LeftLeaningRedBlackTree< K, V >::contains ( const K & k) const
nodiscard

true if \( k \) is there

◆ end()

template<std::totally_ordered K, class V>
const_iterator ADS::LeftLeaningRedBlackTree< K, V >::end ( void ) const
inlinenodiscard

the iterator past the largest key

◆ findKey()

template<std::totally_ordered K, class V>
const K * ADS::LeftLeaningRedBlackTree< K, V >::findKey ( const K & k) const
nodiscard

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

◆ forEach()

template<std::totally_ordered K, class V>
template<class F>
void ADS::LeftLeaningRedBlackTree< K, V >::forEach ( F && f) const

f( k , v ) on the pairs by increasing keys

◆ height()

template<std::totally_ordered K, class V>
long ADS::LeftLeaningRedBlackTree< K, V >::height ( void ) const
nodiscard

the height, \( -1 \) if empty, in \( O( n ) \)

◆ insert()

template<std::totally_ordered K, class V>
void ADS::LeftLeaningRedBlackTree< K, V >::insert ( const K & k,
const V & 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 \)

◆ lookup()

template<std::totally_ordered K, class V>
std::optional< V > ADS::LeftLeaningRedBlackTree< K, V >::lookup ( const K & k) const
nodiscard

the value of \( k \), or nothing, in \( O( \log n ) \) time

◆ lowerBound()

template<std::totally_ordered K, class V>
std::optional< std::pair< K, V > > ADS::LeftLeaningRedBlackTree< K, V >::lowerBound ( const K & k) const
nodiscard

the smallest key not smaller than \( k \) and its value, or nothing (the find(x) of an SSet of Morin)

◆ lowerBoundKey()

template<std::totally_ordered K, class V>
const K * ADS::LeftLeaningRedBlackTree< K, V >::lowerBoundKey ( const K & k) const
nodiscard

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

◆ operator=()

template<std::totally_ordered K, class V>
LeftLeaningRedBlackTree & ADS::LeftLeaningRedBlackTree< K, V >::operator= ( const LeftLeaningRedBlackTree< K, V > & )
delete

no copy, since the nodes are linked by pointers

◆ remove()

template<std::totally_ordered K, class V>
void ADS::LeftLeaningRedBlackTree< K, V >::remove ( const K & k)

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

◆ size()

template<std::totally_ordered K, class V>
std::size_t ADS::LeftLeaningRedBlackTree< K, V >::size ( void ) const
inlinenodiscard

the number \( n \) of keys

◆ twoFourDegrees()

template<std::totally_ordered K, class V>
std::array< std::size_t, 3 > ADS::LeftLeaningRedBlackTree< K, V >::twoFourDegrees ( void ) const
nodiscard

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 ) \)


The documentation for this class was generated from the following file: