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

a binary search tree, balanced as a Red-Black tree if RB, and then with the operations based on join [see Joinable] More...

#include <ADS/tree/SearchTree.h>

Inheritance diagram for ADS::SearchTree< K, V, RB >:

Classes

 the links of a node; the sentinel leaf is a Link and nothing more More...
struct  Node
 a node of the tree, with its key and its value More...
class  const_iterator
 the constant iterator on the pairs, by increasing key 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 iterator = const_iterator
 the constant iterator

Public member functions

 SearchTree (void)
 an empty tree
 SearchTree (const SearchTree &o)
 a copy, node by node with the same shape, in \( O( n ) \) time
 SearchTree (SearchTree &&o) noexcept
 the nodes of o, which is left empty, in \( O( 1 ) \) time
SearchTree & operator= (SearchTree o) noexcept
 the nodes of o, by copy and swap
 ~SearchTree ()
 frees the nodes
void swap (SearchTree &o) noexcept
 exchanges the nodes with o
std::optional< V > lookup (const K &k) const
 the value of the key \( k \), or nothing
void insert (const K &k, const V &v)
 associates the value \( v \) to the key \( k \), in \( O( h ) \) time
void remove (const K &k)
 removes the key \( k \), if present, in \( O( h ) \) time
bool contains (const K &k) const
 true if the key \( k \) is present
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), by lowerBoundNode() in \( O( h ) \) time
const K * findKey (const K &k) const
 the address of the key \( k \) in the tree, nil if absent, in \( O( h ) \) time: the find( x ) of a USet of Morin, which makes DictionarySet a USet [see SetInterfaces.h]; 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, by lowerBoundNode() in \( O( h ) \) time: the find( x ) of an SSet of Morin, which makes DictionarySet an SSet; 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
void clear (void)
 removes all the nodes
Link * root (void) const
 the root, from which the nodes can be walked as in the book, along the fields parent, left and right of a Link, until the leaf, isNil(); they must not be changed
bool isNil (const Link *u) const
 true if \( u \) is the sentinel leaf (or the parent of the root)
Link * lookupNode (const K &x) const
 the node of the key \( x \), nil if absent, in \( O( h ) \) time
Link * lowerBoundNode (const K &x) const
 the node of the smallest key not smaller than \( x \), nil if none, in \( O( h ) \) time (the successor search of the find( x ) of an SSet of Morin)
Link * min (void) const
 the node of the smallest key, nil if the tree is empty, by the left path from the root
Link * max (void) const
 the node of the largest key, nil if the tree is empty, by the right path from the root
Link * successorNode (Link *t) const
 the node of the next key, nil after the largest one and from nil, in \( O( h ) \) time
template<class F>
void forEach (F &&f) const
 the foreach of the book: f( k , v ) on the pairs in increasing order of the keys, \( O( n ) \) time, by the visit in symmetric order
Link * predecessorNode (Link *t) const
 the node of the previous key, nil before the first one and from nil, successorNode() with left and right swapped
const_iterator begin (void) const
 the iterator on the smallest key
const_iterator end (void) const
 the iterator past the largest key, the sentinel
const_iterator cbegin (void) const
 as begin()
const_iterator cend (void) const
 as end()

Static public member functions

static const K & key (const Link *u)
 the key of the node \( u \)
static const V & value (const Link *u)
 the value of the node \( u \)

Friends

struct SearchTreeNodes< K, V, RB >

Detailed description

template<std::totally_ordered K, class V, bool RB>
class ADS::SearchTree< K, V, RB >

a binary search tree, balanced as a Red-Black tree if RB, and then with the operations based on join [see Joinable]

Member typedef documentation

◆ const_reference

template<std::totally_ordered K, class V, bool RB>
using ADS::SearchTree< K, V, RB >::const_reference = reference

the same

◆ difference_type

template<std::totally_ordered K, class V, bool RB>
using ADS::SearchTree< K, V, RB >::difference_type = std::ptrdiff_t

the type of the distances

◆ iterator

template<std::totally_ordered K, class V, bool RB>
using ADS::SearchTree< K, V, RB >::iterator = const_iterator

the constant iterator

◆ Key

template<std::totally_ordered K, class V, bool RB>
using ADS::SearchTree< K, V, RB >::Key = K

the type of the keys

◆ key_type

template<std::totally_ordered K, class V, bool RB>
using ADS::SearchTree< K, V, RB >::key_type = K

the type of the keys

◆ mapped_type

template<std::totally_ordered K, class V, bool RB>
using ADS::SearchTree< K, V, RB >::mapped_type = V

the type of the values

◆ reference

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

references, by value

◆ size_type

template<std::totally_ordered K, class V, bool RB>
using ADS::SearchTree< K, V, RB >::size_type = std::size_t

the type of the sizes

◆ Value

template<std::totally_ordered K, class V, bool RB>
using ADS::SearchTree< K, V, RB >::Value = V

the type of the values

◆ value_type

template<std::totally_ordered K, class V, bool RB>
using ADS::SearchTree< K, V, RB >::value_type = std::pair< K , V >

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

Constructor and destructor documentation

◆ SearchTree() [1/3]

template<std::totally_ordered K, class V, bool RB>
ADS::SearchTree< K, V, RB >::SearchTree ( void )

an empty tree

◆ SearchTree() [2/3]

template<std::totally_ordered K, class V, bool RB>
ADS::SearchTree< K, V, RB >::SearchTree ( const SearchTree< K, V, RB > & o)

a copy, node by node with the same shape, in \( O( n ) \) time

◆ SearchTree() [3/3]

template<std::totally_ordered K, class V, bool RB>
ADS::SearchTree< K, V, RB >::SearchTree ( SearchTree< K, V, RB > && o)
noexcept

the nodes of o, which is left empty, in \( O( 1 ) \) time

◆ ~SearchTree()

template<std::totally_ordered K, class V, bool RB>
ADS::SearchTree< K, V, RB >::~SearchTree ( )

frees the nodes

Member function documentation

◆ begin()

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

the iterator on the smallest key

◆ cbegin()

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

as begin()

◆ cend()

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

as end()

◆ clear()

template<std::totally_ordered K, class V, bool RB>
void ADS::SearchTree< K, V, RB >::clear ( void )

removes all the nodes

◆ contains()

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

true if the key \( k \) is present

◆ empty()

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

true if there is no key

◆ end()

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

the iterator past the largest key, the sentinel

◆ findKey()

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

the address of the key \( k \) in the tree, nil if absent, in \( O( h ) \) time: the find( x ) of a USet of Morin, which makes DictionarySet a USet [see SetInterfaces.h]; valid until the key is removed

◆ forEach()

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

the foreach of the book: f( k , v ) on the pairs in increasing order of the keys, \( O( n ) \) time, by the visit in symmetric order

◆ insert()

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

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

In pseudocode (sections 6.1 and 6.2), the insertion of the binary search tree, balanceInsert() for a Red-Black tree, and the rotation of figure 6.4 (rotateRight() its mirror):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insertNode}( T , x , v ) \cr 2 & \quad p \gets \textbf{nil};\ u \gets T\quad \triangleright\ p \text{ is the parent of } u \cr 3 & \quad \textbf{while}\ u \ne \textbf{nil}\ \textbf{and} \ u.\mathit{key} \ne x\ \textbf{do}\ ( p , u ) \gets ( u , \text{the child of } u \text{ on the side of } x ) \cr 4 & \quad \textbf{if}\ u \ne \textbf{nil}\ \textbf{then} \ u.\mathit{value} \gets v;\ \textbf{return}\ T\quad \triangleright \ x \text{ was there} \cr 5 & \quad n \gets \text{a new node of } ( x , v ) \text{, hung under } p \text{ on the side of } x \cr 6 & \quad \text{add } 1 \text{ to the size of } p \text{ and of each node above it} \cr 7 & \quad \textbf{if}\ \mathit{RB}\ \textbf{then} \ \mathrm{balanceInsert}( n );\ \textbf{return} \ \text{the root above } n \cr 8 & \quad \textbf{return}\ n \text{ if } p = \textbf{nil} \text{, else } T \cr 9 & \textbf{procedure}\ \mathrm{balanceInsert}( t )\quad \triangleright \ \text{only } t \text{ and its parent may be both red} \cr 10 & \quad t.\mathit{color} \gets \text{red} \cr 11 & \quad \textbf{while}\ t \text{ is not the root and its parent } p \text{ is red}\ \textbf{do}\quad \triangleright \ \text{none if (2), } p \text{ black} \cr 12 & \qquad n \gets p.\mathit{parent};\ z \gets \text{the child of } n \text{ other than } p\quad \triangleright\ n \text{ is black} \cr 13 & \qquad \textbf{if}\ z \text{ is red}\ \textbf{then} \ p.\mathit{color} \gets z.\mathit{color} \gets \text{black}; \ n.\mathit{color} \gets \text{red};\ t \gets n\quad \triangleright \ \text{(3)} \cr 14 & \qquad \textbf{else} \cr 15 & \qquad\quad \textbf{if}\ t \text{ and } p \text{ hang on opposite sides}\ \textbf{then}\quad \triangleright \ \text{(4a), (4b)} \cr 16 & \qquad\qquad \text{rotate at } p \text{, so that } t \text{ takes its place};\ ( t , p ) \gets ( p , t ) \cr 17 & \qquad\quad \text{rotate at } n \text{, so that } p \text{ takes its place};\ p.\mathit{color} \gets \text{black}; \ n.\mathit{color} \gets \text{red}\quad \triangleright \ \text{(5a), (5b)} \cr 18 & \quad \text{color the root black}\quad \triangleright \ \text{(1) if } t \text{ is the root} \cr 19 & \textbf{function}\ \mathrm{rotateLeft}( x )\quad \triangleright \ \text{the right child } y \text{ of } x \text{ goes up} \cr 20 & \quad y \gets x.\mathit{right};\ x.\mathit{right} \gets y.\mathit{left};\ y.\mathit{left} \gets x \cr 21 & \quad y \text{ replaces } x \text{ as the child of the old parent of } x; \ \text{fix the parents} \cr 22 & \quad \text{recompute the size of } x \text{, then that of } y; \ \textbf{return}\ y \end{array}} \]

◆ isNil()

template<std::totally_ordered K, class V, bool RB>
bool ADS::SearchTree< K, V, RB >::isNil ( const Link * u) const
nodiscard

true if \( u \) is the sentinel leaf (or the parent of the root)

◆ key()

template<std::totally_ordered K, class V, bool RB>
const K & ADS::SearchTree< K, V, RB >::key ( const Link * u)
staticnodiscard

the key of the node \( u \)

Precondition
\( u \) is a node, not the sentinel leaf, which has no key (only nil is checked, as a static function cannot know the sentinel)

◆ lookup()

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

the value of the key \( k \), or nothing

◆ lookupNode()

template<std::totally_ordered K, class V, bool RB>
Link * ADS::SearchTree< K, V, RB >::lookupNode ( const K & x) const
nodiscard

the node of the key \( x \), nil if absent, in \( O( h ) \) time

In pseudocode (section 6.1):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lookupNode}( T , x ) \cr 2 & \quad \textbf{while}\ T \ne \textbf{nil}\ \textbf{and} \ T.\mathit{key} \ne x\ \textbf{do}\ T \gets \text{the child of } T \text{ on the side of } x \cr 3 & \quad \textbf{return}\ T\quad \triangleright\ \textbf{nil} \text{ if } x \text{ is not in the tree} \end{array}} \]

◆ lowerBound()

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

◆ lowerBoundKey()

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

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

◆ lowerBoundNode()

template<std::totally_ordered K, class V, bool RB>
Link * ADS::SearchTree< K, V, RB >::lowerBoundNode ( const K & x) const
nodiscard

the node of the smallest key not smaller than \( x \), nil if none, in \( O( h ) \) time (the successor search of the find( x ) of an SSet of Morin)

In pseudocode (section 6.2.1 of Morin, beyond the book), the last node at which the path turns left being the best so far, and the search stopping at a key equal to \( x \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lowerBoundNode}( T , x ) \cr 2 & \quad b \gets \textbf{nil} \cr 3 & \quad \textbf{while}\ T \ne \textbf{nil}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ T.\mathit{key} < x\ \textbf{then}\ T \gets T.\mathit{right} \cr 5 & \qquad \textbf{else if}\ x < T.\mathit{key}\ \textbf{then}\ b \gets T;\ T \gets T.\mathit{left} \cr 6 & \qquad \textbf{else return}\ T \cr 7 & \quad \textbf{return}\ b \end{array}} \]

◆ max()

template<std::totally_ordered K, class V, bool RB>
Link * ADS::SearchTree< K, V, RB >::max ( void ) const
nodiscard

the node of the largest key, nil if the tree is empty, by the right path from the root

◆ min()

template<std::totally_ordered K, class V, bool RB>
Link * ADS::SearchTree< K, V, RB >::min ( void ) const
nodiscard

the node of the smallest key, nil if the tree is empty, by the left path from the root

◆ operator=()

template<std::totally_ordered K, class V, bool RB>
SearchTree & ADS::SearchTree< K, V, RB >::operator= ( SearchTree< K, V, RB > o)
noexcept

the nodes of o, by copy and swap

◆ predecessorNode()

template<std::totally_ordered K, class V, bool RB>
Link * ADS::SearchTree< K, V, RB >::predecessorNode ( Link * t) const
nodiscard

the node of the previous key, nil before the first one and from nil, successorNode() with left and right swapped

Precondition
\( t \) is not a null pointer

◆ remove()

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

removes the key \( k \), if present, in \( O( h ) \) time

In pseudocode (sections 6.1 and 6.2), the removal of the binary search tree and balanceDelete() for a Red-Black tree, its cases (1) to (4) written once for the two sides of \( p \), \( \mathit{near} \) the side of \( t \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{removeNode}( T , x ) \cr 2 & \quad u \gets \mathrm{lookupNode}( T , x );\ \textbf{if}\ u = \textbf{nil}\ \textbf{then return}\ T \cr 3 & \quad \textbf{if}\ u \text{ has two children}\ \textbf{then}\quad \triangleright\ \text{the node to cut has one child at most} \cr 4 & \qquad s \gets \mathrm{min}( u.\mathit{right} ); \ \text{copy the key and the value of } s \text{ into } u;\ u \gets s \cr 5 & \quad t \gets \text{the child of } u \text{ if it has one, else } \textbf{nil} \cr 6 & \quad \text{subtract } 1 \text{ from the size of each node above } u;\ \text{put } t \text{ in the place of } u \cr 7 & \quad \textbf{if}\ \mathit{RB}\ \textbf{and}\ u \text{ is black} \ \textbf{then}\ \mathrm{balanceDelete}( t , u.\mathit{parent} ) \cr 8 & \quad \text{free } u;\ \textbf{return}\ \text{the root} \cr 9 & \textbf{procedure}\ \mathrm{balanceDelete}( t , p )\quad \triangleright\ \text{a black node is missing below } p \text{ on the side of } t \cr 10 & \quad \textbf{while}\ p \ne \textbf{nil}\ \textbf{and}\ t \text{ is black}\ \textbf{do} \cr 11 & \qquad \mathit{near} \gets \text{the side of } p \text{ that holds } t;\ \mathit{far} \gets \text{the other side} \cr 12 & \qquad f \gets p.\mathit{far};\ a \gets f.\mathit{near};\ b \gets f.\mathit{far}\quad \triangleright \ \text{the sibling and the nephews} \cr 13 & \qquad \textbf{if}\ f \text{ is red}\ \textbf{then} \ p.\mathit{color} \gets \text{red};\ f.\mathit{color} \gets \text{black};\ \text{rotate } p \text{ down to } \mathit{near}\quad \triangleright\ \text{(1)} \cr 14 & \qquad \textbf{else if}\ a \text{ and } b \text{ are black} \ \textbf{then}\ f.\mathit{color} \gets \text{red};\ ( t , p ) \gets ( p , p.\mathit{parent} )\quad \triangleright\ \text{(2)} \cr 15 & \qquad \textbf{else if}\ b \text{ is black}\ \textbf{then} \ a.\mathit{color} \gets \text{black};\ f.\mathit{color} \gets \text{red};\ \text{rotate } f \text{ down to } \mathit{far}\quad \triangleright\ \text{(3)} \cr 16 & \qquad \textbf{else}\quad \triangleright\ \text{(4)} \cr 17 & \qquad\quad f.\mathit{color} \gets p.\mathit{color}; \ p.\mathit{color} \gets b.\mathit{color} \gets \text{black}; \ \text{rotate } p \text{ down to } \mathit{near} \cr 18 & \qquad\quad ( t , p ) \gets ( \text{the root} , \textbf{nil} )\quad \triangleright\ \text{the missing black is restored} \cr 19 & \quad \textbf{if}\ t \ne \textbf{nil}\ \textbf{then} \ t.\mathit{color} \gets \text{black} \end{array}} \]

◆ root()

template<std::totally_ordered K, class V, bool RB>
Link * ADS::SearchTree< K, V, RB >::root ( void ) const
nodiscard

the root, from which the nodes can be walked as in the book, along the fields parent, left and right of a Link, until the leaf, isNil(); they must not be changed

◆ size()

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

the number \( n \) of keys

◆ successorNode()

template<std::totally_ordered K, class V, bool RB>
Link * ADS::SearchTree< K, V, RB >::successorNode ( Link * t) const
nodiscard

the node of the next key, nil after the largest one and from nil, in \( O( h ) \) time

In pseudocode (section 6.1), by the links to the parents:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{successorNode}( t ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return} \ \textbf{nil} \cr 3 & \quad \textbf{if}\ t.\mathit{right} \ne \textbf{nil} \ \textbf{then return}\ \mathrm{min}( t.\mathit{right} ) \cr 4 & \quad \textbf{while}\ t \text{ is the right child of its parent} \ \textbf{do}\ t \gets t.\mathit{parent} \cr 5 & \quad \textbf{return}\ t.\mathit{parent}\quad \triangleright \ \text{the first ancestor with } t \text{ on its left, or } \textbf{nil} \end{array}} \]

Precondition
\( t \) is not a null pointer

◆ swap()

template<std::totally_ordered K, class V, bool RB>
void ADS::SearchTree< K, V, RB >::swap ( SearchTree< K, V, RB > & o)
noexcept

exchanges the nodes with o

◆ value()

template<std::totally_ordered K, class V, bool RB>
const V & ADS::SearchTree< K, V, RB >::value ( const Link * u)
staticnodiscard

the value of the node \( u \)

Precondition
\( u \) is a node, not the sentinel leaf

◆ SearchTreeNodes< K, V, RB >

template<std::totally_ordered K, class V, bool RB>
friend struct SearchTreeNodes< K, V, RB >
friend

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