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

a DICTIONARY on an AVL tree, with the operations based on join [see Joinable] More...

#include <ADS/tree/AVLTree.h>

Inheritance diagram for ADS::AVLTree< K, V >:
ADS::Joinable< AVLTree< K, V >, AVLTreeNodes< K, V >, K, V >

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< AVLTree , Shape >
 the constant iterator by increasing key, along the path from the root
using iterator = const_iterator
 the constant iterator

Public member functions

std::optional< V > lookup (const K &k) const
 the value of \( k \), or nothing, by the descent of a binary search tree, 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::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), in \( O( \log n ) \) time
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
std::size_t size (void) const
 the number \( n \) of keys
bool empty (void) const
 true if there is no key
int height (void) const
 the height of the tree, -1 if it is empty
template<class F>
void forEach (F &&f) const
 f( k , v ) on the pairs by increasing keys
std::pair< std::size_t, std::size_t > rebalancings (void) const
 the rebalancings of insert() and remove() so far, i.e., the nodes found unbalanced on their paths and mended by one or two rotations, and those rotations (exercise 9.12 of Morin)
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()
Public member functions inherited from ADS::Joinable< AVLTree< K, V >, AVLTreeNodes< K, V >, K, V >
std::tuple< AVLTree< K, V >, std::optional< V >, AVLTree< K, V > > split (const K &k) &&
 the keys before \( k \), the value of \( k \) (if there), the keys after \( k \), by JoinAlgorithms::split(); the tree is consumed, in \( O( \log n ) \) time
AVLTree< K, V > extractRange (const K &lo, const K &hi)
 the pairs of the keys in \( [ \mathit{lo} , \mathit{hi} ] \), taken out of the tree by JoinAlgorithms::extract(), in \( O( \log n ) \) time
void unionWith (AVLTree< K, V > &&o, F f=F())
 the union with o, which is consumed, by JoinAlgorithms::unite(); on a key of both, the value f( mine , its ), by default its; \( O( m \log( n / m + 1 ) ) \) work for sizes \( m \le n \)
void intersectWith (AVLTree< K, V > &&o, F f=F())
 the intersection with o, which is consumed, by JoinAlgorithms::intersect(); the value f( mine , its ), by default mine; the work of unionWith(), plus the freeing of the nodes that leave
void differenceWith (AVLTree< K, V > &&o)
 the keys not in o, which is consumed, by JoinAlgorithms::subtract(); the work of unionWith(), plus the freeing of the nodes of o
void multiInsert (std::span< const std::pair< K, V > > B, F f=F())
 the pairs of \( B \) inserted, by JoinAlgorithms::multiInsert(); on a key already there, the value f( mine , new ), by default new; \( O( m \log( n / m + 1 ) ) \) work for \( m \) pairs
void multiRemove (std::span< const K > B)
 the keys of \( B \) removed, by JoinAlgorithms::multiRemove(), \( O( m \log( n / m + 1 ) ) \) work
void filter (P p)
 only the pairs with p( k , v ) kept, by JoinAlgorithms::filter(), \( O( n ) \) work
U mapReduce (M map, R reduce, U id) const
 \( \mathrm{reduce}( \ldots \mathrm{reduce}( \mathit{id} , \mathrm{map}( k_1 , v_1 ) ) \ldots , \mathrm{map}( k_n , v_n ) ) \) by an associative reduce of identity id, by JoinAlgorithms::mapReduce(), \( O( n ) \) work

Friends

struct AVLTreeNodes< K, V >

Additional Inherited Members

Static public member functions inherited from ADS::Joinable< AVLTree< K, V >, AVLTreeNodes< K, V >, K, V >
static AVLTree< K, V > join (AVLTree< K, V > &&l, const K &k, const V &v, AVLTree< K, V > &&r)
 the tree of the keys of \( l \), the pair \( ( k , v ) \) and the keys of \( r \), by the join of the scheme; \( l \) and \( r \) are consumed, in \( O( | \log n_l - \log n_r | + 1 ) \) time
static AVLTree< K, V > join2 (AVLTree< K, V > &&l, AVLTree< K, V > &&r)
 the tree of the keys of \( l \) and of \( r \), which are consumed, by JoinAlgorithms::join2(), in \( O( \log n ) \) time

Detailed description

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

a DICTIONARY on an AVL tree, with the operations based on join [see Joinable]

Member typedef documentation

◆ const_iterator

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

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

◆ const_reference

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

the same

◆ difference_type

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

the type of the distances

◆ iterator

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

the constant iterator

◆ Key

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

the type of the keys

◆ key_type

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

the type of the keys

◆ mapped_type

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

the type of the values

◆ reference

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

references, by value

◆ size_type

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

the type of the sizes

◆ Value

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

the type of the values

◆ value_type

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

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

Member function documentation

◆ begin()

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

the iterator on the smallest key

◆ cbegin()

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

as begin()

◆ cend()

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

as end()

◆ contains()

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

true if \( k \) is there

◆ empty()

template<std::totally_ordered K, class V>
bool ADS::AVLTree< K, V >::empty ( void ) const
nodiscard

true if there is no key

◆ end()

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

the iterator past the largest key

◆ findKey()

template<std::totally_ordered K, class V>
const K * ADS::AVLTree< 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::AVLTree< K, V >::forEach ( F && f) const

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

◆ height()

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

the height of the tree, -1 if it is empty

◆ insert()

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

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

In pseudocode (Knuth, section 6.2.3, beyond the book), recursive, \( h( \textbf{nil} ) = -1 \), and the balance of a node restored by a single or a double rotation on the way back:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( t , k , v ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then}\ t \gets \text{a new node of } ( k , v );\ \textbf{return} \cr 3 & \quad \textbf{if}\ k < t.\mathit{key}\ \textbf{then} \ \mathrm{insert}( t.\mathit{left} , k , v ) \cr 4 & \quad \textbf{else if}\ t.\mathit{key} < k\ \textbf{then} \ \mathrm{insert}( t.\mathit{right} , k , v ) \cr 5 & \quad \textbf{else}\ t.\mathit{value} \gets v \cr 6 & \quad \mathrm{balance}( t ) \cr 7 & \textbf{procedure}\ \mathrm{balance}( t ) \cr 8 & \quad \text{update the height and the size of } t \cr 9 & \quad \textbf{if}\ h( t.\mathit{left} ) - h( t.\mathit{right} ) > 1 \ \textbf{then} \cr 10 & \qquad \textbf{if}\ h( t.\mathit{left}.\mathit{left} ) < h( t.\mathit{left}.\mathit{right} )\ \textbf{then}\ \mathrm{rotateLeft}( t.\mathit{left} ) \cr 11 & \qquad \mathrm{rotateRight}( t ) \cr 12 & \quad \textbf{else if}\ h( t.\mathit{right} ) - h( t.\mathit{left} ) > 1\ \textbf{then}\ \text{the mirror of the } \text{lines 10 and 11} \end{array}} \]

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

◆ lookup()

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

the value of \( k \), or nothing, by the descent of a binary search tree, in \( O( \log n ) \) time

◆ lowerBound()

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

◆ lowerBoundKey()

template<std::totally_ordered K, class V>
const K * ADS::AVLTree< 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

◆ rebalancings()

template<std::totally_ordered K, class V>
std::pair< std::size_t, std::size_t > ADS::AVLTree< K, V >::rebalancings ( void ) const
inlinenodiscard

the rebalancings of insert() and remove() so far, i.e., the nodes found unbalanced on their paths and mended by one or two rotations, and those rotations (exercise 9.12 of Morin)

◆ remove()

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

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

In pseudocode (Knuth, section 6.2.3, beyond the book), the removal of a binary search tree with the balance() of insert() on the way back:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( t , k ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return} \cr 3 & \quad \textbf{if}\ k < t.\mathit{key}\ \textbf{then} \ \mathrm{remove}( t.\mathit{left} , k ) \cr 4 & \quad \textbf{else if}\ t.\mathit{key} < k\ \textbf{then} \ \mathrm{remove}( t.\mathit{right} , k ) \cr 5 & \quad \textbf{else if}\ t \text{ has one child at most} \ \textbf{then}\ t \gets \text{that child};\ \textbf{return} \cr 6 & \quad \textbf{else} \cr 7 & \qquad s \gets \text{the node of the smallest key of } t.\mathit{right} \cr 8 & \qquad ( t.\mathit{key} , t.\mathit{value} ) \gets ( s.\mathit{key} , s.\mathit{value} );\ \mathrm{remove}( t.\mathit{right} , s.\mathit{key} ) \cr 9 & \quad \mathrm{balance}( t ) \end{array}} \]

Postcondition
\( k \) is not there

◆ size()

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

the number \( n \) of keys

◆ AVLTreeNodes< K, V >

template<std::totally_ordered K, class V>
friend struct AVLTreeNodes< K, V >
friend

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