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

a DICTIONARY on a scapegoat tree of height at most \( \log_b q \), whose nodes keep the size of their subtree if StoredSizes More...

#include <ADS/tree/ScapegoatTree.h>

Classes

struct  Shape
 the shape of the tree, for PathIterator More...

Public types

enum class  Sizes { RECOMPUTE , REUSE }
 how the sizes are found when they are not stored More...
enum class  Scapegoats { HEIGHT , WEIGHT }
 which ancestor is the scapegoat More...
enum class  Rebuilds { ARRAY , LIST }
 how a subtree is rebuilt More...
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< ScapegoatTree , Shape >
 the constant iterator by increasing key, along the path from the root
using iterator = const_iterator
 the constant iterator

Public member functions

 ScapegoatTree (double b=1.5, Sizes sizes=Sizes::REUSE, Rebuilds rebuilds=Rebuilds::ARRAY, Scapegoats scapegoats=Scapegoats::HEIGHT)
 the empty tree of height at most \( \log_b q \)
 ScapegoatTree (const ScapegoatTree &)=delete
 no copy, since the nodes are linked by pointers
ScapegoatTree & operator= (const ScapegoatTree &)=delete
 no copy, since the nodes are linked by pointers
 ScapegoatTree (ScapegoatTree &&other) noexcept
 the nodes of other, which is left empty
 ~ScapegoatTree ()
 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 ) \) amortized time
void remove (const K &k)
 removes \( k \), if it is there, in \( O( \log n ) \) amortized time: the removal of a binary search tree, then the whole tree rebuilt if \( q > 2 n \), and \( q \gets n \)
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 ) \)
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
template<class F>
void forEach (F &&f) const
 f( k , v ) on the pairs by increasing keys
std::size_t rebuilds (void) const
 the number of rebuilds so far
std::size_t rebuiltNodes (void) const
 the nodes of all the rebuilds so far, the work they took
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, bool StoredSizes = false>
class ADS::ScapegoatTree< K, V, StoredSizes >

a DICTIONARY on a scapegoat tree of height at most \( \log_b q \), whose nodes keep the size of their subtree if StoredSizes

Member typedef documentation

◆ const_iterator

template<std::totally_ordered K, class V, bool StoredSizes = false>
using ADS::ScapegoatTree< K, V, StoredSizes >::const_iterator = PathIterator< ScapegoatTree , Shape >

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

◆ const_reference

template<std::totally_ordered K, class V, bool StoredSizes = false>
using ADS::ScapegoatTree< K, V, StoredSizes >::const_reference = reference

the same

◆ difference_type

template<std::totally_ordered K, class V, bool StoredSizes = false>
using ADS::ScapegoatTree< K, V, StoredSizes >::difference_type = std::ptrdiff_t

the type of the distances

◆ iterator

template<std::totally_ordered K, class V, bool StoredSizes = false>
using ADS::ScapegoatTree< K, V, StoredSizes >::iterator = const_iterator

the constant iterator

◆ Key

template<std::totally_ordered K, class V, bool StoredSizes = false>
using ADS::ScapegoatTree< K, V, StoredSizes >::Key = K

the type of the keys

◆ key_type

template<std::totally_ordered K, class V, bool StoredSizes = false>
using ADS::ScapegoatTree< K, V, StoredSizes >::key_type = K

the type of the keys

◆ mapped_type

template<std::totally_ordered K, class V, bool StoredSizes = false>
using ADS::ScapegoatTree< K, V, StoredSizes >::mapped_type = V

the type of the values

◆ reference

template<std::totally_ordered K, class V, bool StoredSizes = false>
using ADS::ScapegoatTree< K, V, StoredSizes >::reference = PairRef< const K & , const V & >

references, by value

◆ size_type

template<std::totally_ordered K, class V, bool StoredSizes = false>
using ADS::ScapegoatTree< K, V, StoredSizes >::size_type = std::size_t

the type of the sizes

◆ Value

template<std::totally_ordered K, class V, bool StoredSizes = false>
using ADS::ScapegoatTree< K, V, StoredSizes >::Value = V

the type of the values

◆ value_type

template<std::totally_ordered K, class V, bool StoredSizes = false>
using ADS::ScapegoatTree< K, V, StoredSizes >::value_type = std::pair< K , V >

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

Member enumeration documentation

◆ Rebuilds

template<std::totally_ordered K, class V, bool StoredSizes = false>
enum class ADS::ScapegoatTree::Rebuilds
strong

how a subtree is rebuilt

Enumerator
ARRAY 

by a buffer of its nodes, as Morin does

LIST 

by a list of its nodes, no buffer (exercise 8.7)

◆ Scapegoats

template<std::totally_ordered K, class V, bool StoredSizes = false>
enum class ADS::ScapegoatTree::Scapegoats
strong

which ancestor is the scapegoat

Enumerator
HEIGHT 

the first too high for its size (Galperin, Rivest)

WEIGHT 

the first with a child too large, as Morin does

◆ Sizes

template<std::totally_ordered K, class V, bool StoredSizes = false>
enum class ADS::ScapegoatTree::Sizes
strong

how the sizes are found when they are not stored

Enumerator
RECOMPUTE 

each one counted, as Morin does

REUSE 

the child on the path known (exercise 8.5)

Constructor and destructor documentation

◆ ScapegoatTree() [1/3]

template<std::totally_ordered K, class V, bool StoredSizes = false>
ADS::ScapegoatTree< K, V, StoredSizes >::ScapegoatTree ( double b = 1.5,
Sizes sizes = Sizes::REUSE,
Rebuilds rebuilds = Rebuilds::ARRAY,
Scapegoats scapegoats = Scapegoats::HEIGHT )
explicit

the empty tree of height at most \( \log_b q \)

Precondition
\( 1 < b < 2 \)

◆ ScapegoatTree() [2/3]

template<std::totally_ordered K, class V, bool StoredSizes = false>
ADS::ScapegoatTree< K, V, StoredSizes >::ScapegoatTree ( const ScapegoatTree< K, V, StoredSizes > & )
delete

no copy, since the nodes are linked by pointers

◆ ScapegoatTree() [3/3]

template<std::totally_ordered K, class V, bool StoredSizes = false>
ADS::ScapegoatTree< K, V, StoredSizes >::ScapegoatTree ( ScapegoatTree< K, V, StoredSizes > && other)
noexcept

the nodes of other, which is left empty

◆ ~ScapegoatTree()

template<std::totally_ordered K, class V, bool StoredSizes = false>
ADS::ScapegoatTree< K, V, StoredSizes >::~ScapegoatTree ( )

frees the nodes

Member function documentation

◆ begin()

template<std::totally_ordered K, class V, bool StoredSizes = false>
const_iterator ADS::ScapegoatTree< K, V, StoredSizes >::begin ( void ) const
inlinenodiscard

the iterator on the smallest key

◆ cbegin()

template<std::totally_ordered K, class V, bool StoredSizes = false>
const_iterator ADS::ScapegoatTree< K, V, StoredSizes >::cbegin ( void ) const
inlinenodiscard

as begin()

◆ cend()

template<std::totally_ordered K, class V, bool StoredSizes = false>
const_iterator ADS::ScapegoatTree< K, V, StoredSizes >::cend ( void ) const
inlinenodiscard

as end()

◆ contains()

template<std::totally_ordered K, class V, bool StoredSizes = false>
bool ADS::ScapegoatTree< K, V, StoredSizes >::contains ( const K & k) const
nodiscard

true if \( k \) is there

◆ end()

template<std::totally_ordered K, class V, bool StoredSizes = false>
const_iterator ADS::ScapegoatTree< K, V, StoredSizes >::end ( void ) const
inlinenodiscard

the iterator past the largest key

◆ findKey()

template<std::totally_ordered K, class V, bool StoredSizes = false>
const K * ADS::ScapegoatTree< K, V, StoredSizes >::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, bool StoredSizes = false>
template<class F>
void ADS::ScapegoatTree< K, V, StoredSizes >::forEach ( F && f) const

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

◆ height()

template<std::totally_ordered K, class V, bool StoredSizes = false>
long ADS::ScapegoatTree< K, V, StoredSizes >::height ( void ) const
nodiscard

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

◆ insert()

template<std::totally_ordered K, class V, bool StoredSizes = false>
void ADS::ScapegoatTree< K, V, StoredSizes >::insert ( const K & k,
const V & v )

associates \( v \) to \( k \), in \( O( \log n ) \) amortized time

In pseudocode (section 8.1 of Morin), \( s \) the sizes and \( P \) the path of the descent:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( k , v ) \cr 2 & \quad u \gets \text{a new leaf of } ( k , v ) \text{ at the depth } d \text{, after } P;\ n \gets n + 1;\ q \gets q + 1 \cr 3 & \quad \textbf{if}\ d > \log_b q\ \textbf{then} \cr 4 & \qquad w \gets u.\mathit{parent} \cr 5 & \qquad \textbf{while}\ b^{d - \mathrm{depth}( w )} \le s( w ) \ \textbf{do}\ u \gets w;\ w \gets w.\mathit{parent} \cr & \qquad\quad \triangleright\ \text{for }\mathtt{WEIGHT}\text{: } \textbf{while}\ b \cdot s( u ) \le s( w ) \cr 6 & \qquad \mathrm{rebuild}( w ) \end{array}} \]

Postcondition
\( k \) has the value \( v \)

◆ lookup()

template<std::totally_ordered K, class V, bool StoredSizes = false>
std::optional< V > ADS::ScapegoatTree< K, V, StoredSizes >::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, bool StoredSizes = false>
std::optional< std::pair< K, V > > ADS::ScapegoatTree< K, V, StoredSizes >::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, bool StoredSizes = false>
const K * ADS::ScapegoatTree< K, V, StoredSizes >::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, bool StoredSizes = false>
ScapegoatTree & ADS::ScapegoatTree< K, V, StoredSizes >::operator= ( const ScapegoatTree< K, V, StoredSizes > & )
delete

no copy, since the nodes are linked by pointers

◆ rebuilds()

template<std::totally_ordered K, class V, bool StoredSizes = false>
std::size_t ADS::ScapegoatTree< K, V, StoredSizes >::rebuilds ( void ) const
inlinenodiscard

the number of rebuilds so far

◆ rebuiltNodes()

template<std::totally_ordered K, class V, bool StoredSizes = false>
std::size_t ADS::ScapegoatTree< K, V, StoredSizes >::rebuiltNodes ( void ) const
inlinenodiscard

the nodes of all the rebuilds so far, the work they took

◆ remove()

template<std::totally_ordered K, class V, bool StoredSizes = false>
void ADS::ScapegoatTree< K, V, StoredSizes >::remove ( const K & k)

removes \( k \), if it is there, in \( O( \log n ) \) amortized time: the removal of a binary search tree, then the whole tree rebuilt if \( q > 2 n \), and \( q \gets n \)

In pseudocode (section 8.1 of Morin):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( k ) \cr 2 & \quad \textbf{if}\ k \text{ is not there}\ \textbf{then return} \cr 3 & \quad \text{the removal of a binary search tree};\ n \gets n - 1 \cr 4 & \quad \textbf{if}\ 2 n < q\ \textbf{then}\ \mathrm{rebuild}( \mathit{root} );\ q \gets n \end{array}} \]

Postcondition
\( k \) is not there

◆ size()

template<std::totally_ordered K, class V, bool StoredSizes = false>
std::size_t ADS::ScapegoatTree< K, V, StoredSizes >::size ( void ) const
inlinenodiscard

the number \( n \) of keys


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