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

a DICTIONARY on a splay tree More...

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

Public member functions

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

Detailed description

template<std::totally_ordered K, class V>
class ADS::SplayTree< K, V >

a DICTIONARY on a splay tree

Member typedef documentation

◆ const_iterator

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

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

◆ const_reference

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

the same

◆ difference_type

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

the type of the distances

◆ iterator

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

the constant iterator

◆ Key

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

the type of the keys

◆ key_type

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

the type of the keys

◆ mapped_type

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

the type of the values

◆ reference

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

references, by value

◆ size_type

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

the type of the sizes

◆ Value

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

the type of the values

◆ value_type

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

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

Constructor and destructor documentation

◆ SplayTree() [1/3]

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

the empty tree

◆ SplayTree() [2/3]

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

no copy

◆ SplayTree() [3/3]

template<std::totally_ordered K, class V>
ADS::SplayTree< K, V >::SplayTree ( SplayTree< K, V > && other)
noexcept

the nodes of other, which is left empty

◆ ~SplayTree()

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

frees the nodes

Member function documentation

◆ begin()

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

the iterator on the smallest key

◆ cbegin()

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

as begin()

◆ cend()

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

as end()

◆ contains()

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

true if \( k \) is there; \( k \), or the last node on its path, goes to the root, in \( O( \log n ) \) amortized time

◆ end()

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

the iterator past the largest key

◆ findKey()

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

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

◆ forEach()

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

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

◆ insert()

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

◆ lookup()

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

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.

◆ lowerBound()

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

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

◆ lowerBoundKey()

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

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

◆ operator=()

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

no copy

◆ remove()

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

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

◆ root()

template<std::totally_ordered K, class V>
std::optional< K > ADS::SplayTree< K, V >::root ( void ) const
nodiscard

the key at the root, if any

◆ size()

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

the number \( n \) of keys


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