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

a DICTIONARY on a binary search tree whose nodes keep the size, the depth and the height of their subtree More...

#include <ADS/tree/MeasuredSearchTree.h>

Classes

struct  Measures
 the measures of a node More...
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< MeasuredSearchTree , Shape >
 the constant iterator by increasing key, along the path from the root
using iterator = const_iterator
 the constant iterator

Public member functions

 MeasuredSearchTree (void)=default
 the empty tree
 MeasuredSearchTree (const MeasuredSearchTree &)=delete
 no copy, since the nodes are linked by pointers
MeasuredSearchTree & operator= (const MeasuredSearchTree &)=delete
 no copy, since the nodes are linked by pointers
 MeasuredSearchTree (MeasuredSearchTree &&other) noexcept
 the nodes of other, which is left empty
 ~MeasuredSearchTree ()
 frees the nodes
std::optional< V > lookup (const K &k) const
 the value of \( k \), or nothing, in \( O( h ) \) time
void insert (const K &k, const V &v)
 associates \( v \) to \( k \), in \( O( h ) \) time
void remove (const K &k)
 removes \( k \), if it is there, in \( O( h ) \) 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, read at the root in \( O( 1 ) \)
std::optional< Measures > measures (const K &k) const
 the measures of the node of \( k \), or nothing, read in it
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), in \( O( h ) \) time
const K * findKey (const K &k) const
 the address of the key \( k \), nil if absent, in \( O( h ) \) 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( h ) \) time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed
std::vector< std::pair< K, V > > getLE (const K &x) const
 the pairs whose keys are not larger than \( x \), in increasing order, by lessOrEqual(), in \( O( n' + h ) \) time for \( n' \) pairs (the getLE(x) of exercise 6.14 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::MeasuredSearchTree< K, V >

a DICTIONARY on a binary search tree whose nodes keep the size, the depth and the height of their subtree

Member typedef documentation

◆ const_iterator

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

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

◆ const_reference

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

the same

◆ difference_type

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

the type of the distances

◆ iterator

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

the constant iterator

◆ Key

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

the type of the keys

◆ key_type

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

the type of the keys

◆ mapped_type

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

the type of the values

◆ reference

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

references, by value

◆ size_type

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

the type of the sizes

◆ Value

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

the type of the values

◆ value_type

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

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

Constructor and destructor documentation

◆ MeasuredSearchTree() [1/3]

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

the empty tree

◆ MeasuredSearchTree() [2/3]

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

no copy, since the nodes are linked by pointers

◆ MeasuredSearchTree() [3/3]

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

the nodes of other, which is left empty

◆ ~MeasuredSearchTree()

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

frees the nodes

Member function documentation

◆ begin()

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

the iterator on the smallest key

◆ cbegin()

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

as begin()

◆ cend()

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

as end()

◆ contains()

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

true if \( k \) is there

◆ end()

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

the iterator past the largest key

◆ findKey()

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

the address of the key \( k \), nil if absent, in \( O( h ) \) 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::MeasuredSearchTree< K, V >::forEach ( F && f) const

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

◆ getLE()

template<std::totally_ordered K, class V>
std::vector< std::pair< K, V > > ADS::MeasuredSearchTree< K, V >::getLE ( const K & x) const
nodiscard

the pairs whose keys are not larger than \( x \), in increasing order, by lessOrEqual(), in \( O( n' + h ) \) time for \( n' \) pairs (the getLE(x) of exercise 6.14 of Morin)

◆ height()

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

the height, \( -1 \) if empty, read at the root in \( O( 1 ) \)

◆ insert()

template<std::totally_ordered K, class V>
void ADS::MeasuredSearchTree< K, V >::insert ( const K & k,
const V & v )

associates \( v \) to \( k \), in \( O( h ) \) time

In pseudocode (exercise 6.19 of Morin), \( s \), \( d \) and \( h \) the three measures:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( k , v ) \cr 2 & \quad p \gets \text{the last node of the search of } k \text{; if it holds } k \text{, its value} \gets v;\ \textbf{return} \cr 3 & \quad u \gets \text{a new leaf of } ( k , v ) \text{ under } p; \ u.s \gets 1;\ u.h \gets 0;\ u.d \gets p.d + 1 \cr 4 & \quad \textbf{for}\ w \gets p\ \textbf{up to the root do}\ w.s \gets w.s + 1;\ w.h \gets 1 + \max\{ w.\mathit{left}.h , w.\mathit{right}.h \} \end{array}} \]

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

◆ lookup()

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

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

◆ lowerBound()

template<std::totally_ordered K, class V>
std::optional< std::pair< K, V > > ADS::MeasuredSearchTree< 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), in \( O( h ) \) time

◆ lowerBoundKey()

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

the address of the smallest key not smaller than \( k \), nil if none, in \( O( h ) \) time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed

◆ measures()

template<std::totally_ordered K, class V>
std::optional< Measures > ADS::MeasuredSearchTree< K, V >::measures ( const K & k) const
nodiscard

the measures of the node of \( k \), or nothing, read in it

◆ operator=()

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

no copy, since the nodes are linked by pointers

◆ remove()

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

removes \( k \), if it is there, in \( O( h ) \) time

In pseudocode (exercise 6.19 of Morin, beyond the book), the pair of the node replaced by that of its successor or predecessor until a leaf is reached:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( k ) \cr 2 & \quad u \gets \text{the node of } k;\ \textbf{if}\ u = \textbf{nil}\ \textbf{then return} \cr 3 & \quad \textbf{while}\ u \text{ is not a leaf}\ \textbf{do} \cr 4 & \qquad w \gets \text{the leftmost node below } u.\mathit{right} \text{, or the rightmost below } u.\mathit{left} \cr 5 & \qquad u.\mathit{pair} \gets w.\mathit{pair};\ u \gets w \cr 6 & \quad p \gets u.\mathit{parent};\ \text{free the leaf } u \cr 7 & \quad \textbf{for}\ w \gets p\ \textbf{up to the root do}\ w.s \gets w.s - 1;\ w.h \gets 1 + \max\{ w.\mathit{left}.h , w.\mathit{right}.h \} \end{array}} \]

Postcondition
\( k \) is not there

◆ size()

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

the number \( n \) of keys


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