ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::SimpleBinaryTrie< U, V, W > Class Template Reference

a binary trie of the integers of \( w \) bits with no list and no jump pointers (exercise 13.1 of Morin's book) More...

#include <ADS/dictionary/BinaryTrie.h>

Public types

using Key = U
 the type of the keys
using Item = U
 the items of the SSet, the keys
using Value = V
 the type of the values

Public member functions

 SimpleBinaryTrie (void)=default
 the empty trie
 SimpleBinaryTrie (SimpleBinaryTrie &&o) noexcept
 the trie of o, which is left empty
SimpleBinaryTrie & operator= (SimpleBinaryTrie &&o) noexcept
 the trie of o, which is left empty; the nodes of this one are freed
 SimpleBinaryTrie (const SimpleBinaryTrie &)=delete
SimpleBinaryTrie & operator= (const SimpleBinaryTrie &)=delete
 ~SimpleBinaryTrie ()
 frees the nodes
const U * lowerBound (U x) const
 the smallest key \( \ge x \), nil if none (the find( x ) of the SSet of Morin's book), in \( O( w ) \) time
const U * find (U x) const
 the key \( x \), nil if it is not there, in \( O( w ) \) time
std::optional< V > lookup (U x) const
 the value of \( x \), or nothing
bool contains (U x) const
 true if \( x \) is there
bool insert (U x, const V &v=V())
 associates \( v \) to \( x \); true if \( x \) was not there, by adding the missing nodes of its path, \( O( w ) \) time
bool remove (U x)
 removes \( x \); true if it was there, by removing the nodes of its path left with no child, \( O( w ) \) time
std::size_t size (void) const
 the number \( n \) of keys
bool empty (void) const
 true if there are no keys
std::size_t nodes (void) const
 the number of nodes, the root and the leaves included
template<class F>
void forEach (F &&f) const
 f( x , v ) on the pairs by increasing keys

Detailed description

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
requires TrieWidth< U , W >
class ADS::SimpleBinaryTrie< U, V, W >

a binary trie of the integers of \( w \) bits with no list and no jump pointers (exercise 13.1 of Morin's book)

Member typedef documentation

◆ Item

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
using ADS::SimpleBinaryTrie< U, V, W >::Item = U

the items of the SSet, the keys

◆ Key

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
using ADS::SimpleBinaryTrie< U, V, W >::Key = U

the type of the keys

◆ Value

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
using ADS::SimpleBinaryTrie< U, V, W >::Value = V

the type of the values

Constructor and destructor documentation

◆ SimpleBinaryTrie() [1/3]

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
ADS::SimpleBinaryTrie< U, V, W >::SimpleBinaryTrie ( void )
default

the empty trie

◆ SimpleBinaryTrie() [2/3]

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
ADS::SimpleBinaryTrie< U, V, W >::SimpleBinaryTrie ( SimpleBinaryTrie< U, V, W > && o)
noexcept

the trie of o, which is left empty

◆ SimpleBinaryTrie() [3/3]

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
ADS::SimpleBinaryTrie< U, V, W >::SimpleBinaryTrie ( const SimpleBinaryTrie< U, V, W > & )
delete

◆ ~SimpleBinaryTrie()

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
ADS::SimpleBinaryTrie< U, V, W >::~SimpleBinaryTrie ( )

frees the nodes

Member function documentation

◆ contains()

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
bool ADS::SimpleBinaryTrie< U, V, W >::contains ( U x) const
nodiscard

true if \( x \) is there

Precondition
\( x < 2^w \)

◆ empty()

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
bool ADS::SimpleBinaryTrie< U, V, W >::empty ( void ) const
inlinenodiscard

true if there are no keys

◆ find()

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
const U * ADS::SimpleBinaryTrie< U, V, W >::find ( U x) const
nodiscard

the key \( x \), nil if it is not there, in \( O( w ) \) time

Precondition
\( x < 2^w \)

◆ forEach()

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
template<class F>
void ADS::SimpleBinaryTrie< U, V, W >::forEach ( F && f) const

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

◆ insert()

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
bool ADS::SimpleBinaryTrie< U, V, W >::insert ( U x,
const V & v = V() )

associates \( v \) to \( x \); true if \( x \) was not there, by adding the missing nodes of its path, \( O( w ) \) time

Precondition
\( x < 2^w \)
Postcondition
\( x \) is there

◆ lookup()

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
std::optional< V > ADS::SimpleBinaryTrie< U, V, W >::lookup ( U x) const
nodiscard

the value of \( x \), or nothing

Precondition
\( x < 2^w \)

◆ lowerBound()

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
const U * ADS::SimpleBinaryTrie< U, V, W >::lowerBound ( U x) const
nodiscard

the smallest key \( \ge x \), nil if none (the find( x ) of the SSet of Morin's book), in \( O( w ) \) time

In pseudocode (exercise 13.1 of Morin's book), \( a \) the child 1 of the deepest node of the path of \( x \) where the path goes to the child 0 and the child 1 is there, i.e., the root of the larger keys nearest to \( x \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lowerBound}( x ) \cr 2 & \quad u \gets \mathit{root};\ a \gets \textbf{nil} \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ w - 1\ \textbf{do} \cr 4 & \qquad c \gets \text{the bit } i \text{ of } x \cr 5 & \qquad \textbf{if}\ c = 0\ \textbf{and}\ u.\mathit{child}[ 1 ] \ne \textbf{nil}\ \textbf{then}\ a \gets u.\mathit{child}[ 1 ] \cr 6 & \qquad \textbf{if}\ u.\mathit{child}[ c ] = \textbf{nil} \ \textbf{then break} \cr 7 & \qquad u \gets u.\mathit{child}[ c ] \cr 8 & \quad \textbf{if}\ i = w\ \textbf{then return}\ x \cr 9 & \quad \textbf{if}\ a = \textbf{nil}\ \textbf{then return nil} \cr 10 & \quad \textbf{return}\ \text{the leftmost leaf below } a \end{array}} \]

Precondition
\( x < 2^w \)

◆ nodes()

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
std::size_t ADS::SimpleBinaryTrie< U, V, W >::nodes ( void ) const
inlinenodiscard

the number of nodes, the root and the leaves included

◆ operator=() [1/2]

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
SimpleBinaryTrie & ADS::SimpleBinaryTrie< U, V, W >::operator= ( const SimpleBinaryTrie< U, V, W > & )
delete

◆ operator=() [2/2]

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
SimpleBinaryTrie & ADS::SimpleBinaryTrie< U, V, W >::operator= ( SimpleBinaryTrie< U, V, W > && o)
noexcept

the trie of o, which is left empty; the nodes of this one are freed

◆ remove()

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
bool ADS::SimpleBinaryTrie< U, V, W >::remove ( U x)

removes \( x \); true if it was there, by removing the nodes of its path left with no child, \( O( w ) \) time

Precondition
\( x < 2^w \)
Postcondition
\( x \) is not there

◆ size()

template<std::unsigned_integral U, class V = std::monostate, int W = std::numeric_limits< U >::digits>
std::size_t ADS::SimpleBinaryTrie< U, V, W >::size ( void ) const
inlinenodiscard

the number \( n \) of keys


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