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

a B+-tree of order \( m \), with the removal of exercise 14.8 of Morin's book More...

#include <ADS/tree/BPlusTree.h>

Classes

class  const_iterator
 the iterator on the pairs by increasing keys, a leaf and a place in it 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 = typename const_iterator::reference
 references, by value
using const_reference = reference
 the same
using iterator = const_iterator
 the constant iterator

Public member functions

 BPlusTree (int m)
 an empty B+-tree of order \( m > 2 \), the root a leaf
 BPlusTree (const BPlusTree &)=delete
 no copy, since the nodes are linked by pointers
BPlusTree & operator= (const BPlusTree &)=delete
 no copy, since the nodes are linked by pointers
 ~BPlusTree ()
 frees the nodes
std::optional< V > lookup (const K &x) const
 the value of the key \( x \), or nothing
void insert (const K &x, const V &v)
 associates the value \( v \) to the key \( x \)
void remove (const K &x)
 removes the key \( x \), if it is there, in \( O( m \log_m n ) \) time
bool contains (const K &x) const
 true if the key \( x \) is there
const K * lowerBound (const K &x) const
 the smallest key \( \ge x \), nil if none (the find( x ) of exercise 14.8 of Morin's book), in \( O( \log_m n ) \) nodes
const K * findKey (const K &k) const
 the address of the key \( k \), nil if absent, in \( O( \log_m n ) \) nodes: 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_m n ) \) nodes: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the next change
std::vector< std::pair< K, V > > findRange (const K &x, const K &y) const
 the pairs with keys in \( ( x , y ] \), the findRange( x , y ) of exercise 14.8 of Morin's book: range() with the first bound left out
std::size_t size (void) const
 the number \( n \) of keys
int order (void) const
 the order \( m \)
std::vector< std::pair< K, V > > range (const K &A, const K &B) const
 the pairs with keys in \( [ A , B ] \), read along the list of the leaves, \( O( \log n + k ) \) for \( k \) pairs
std::vector< std::vector< std::vector< K > > > levels (void) const
 the keys of the nodes, level by level from the root, each level from left to right: the shape of the tree, as in figure 6.7, by a visit in breadth
void check (void) const
 checks the shape [see shaped()] whatever the level of the contracts; it throws contract_violation if it fails
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::BPlusTree< K, V >

a B+-tree of order \( m \), with the removal of exercise 14.8 of Morin's book

Member typedef documentation

◆ const_reference

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

the same

◆ difference_type

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

the type of the distances

◆ iterator

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

the constant iterator

◆ Key

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

the type of the keys

◆ key_type

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

the type of the keys

◆ mapped_type

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

the type of the values

◆ reference

template<std::totally_ordered K, class V>
using ADS::BPlusTree< K, V >::reference = typename const_iterator::reference

references, by value

◆ size_type

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

the type of the sizes

◆ Value

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

the type of the values

◆ value_type

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

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

Constructor and destructor documentation

◆ BPlusTree() [1/2]

template<std::totally_ordered K, class V>
ADS::BPlusTree< K, V >::BPlusTree ( int m)
explicit

an empty B+-tree of order \( m > 2 \), the root a leaf

Precondition
\( m > 2 \)

◆ BPlusTree() [2/2]

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

no copy, since the nodes are linked by pointers

◆ ~BPlusTree()

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

frees the nodes

Member function documentation

◆ begin()

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

the iterator on the smallest key

◆ cbegin()

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

as begin()

◆ cend()

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

as end()

◆ check()

template<std::totally_ordered K, class V>
void ADS::BPlusTree< K, V >::check ( void ) const

checks the shape [see shaped()] whatever the level of the contracts; it throws contract_violation if it fails

◆ contains()

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

true if the key \( x \) is there

◆ end()

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

the iterator past the largest key

◆ findKey()

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

the address of the key \( k \), nil if absent, in \( O( \log_m n ) \) nodes: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed

◆ findRange()

template<std::totally_ordered K, class V>
std::vector< std::pair< K, V > > ADS::BPlusTree< K, V >::findRange ( const K & x,
const K & y ) const
nodiscard

the pairs with keys in \( ( x , y ] \), the findRange( x , y ) of exercise 14.8 of Morin's book: range() with the first bound left out

◆ insert()

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

associates the value \( v \) to the key \( x \)

In pseudocode (section 6.3), the splits going up from the leaf as far as they are needed:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( x , v ) \cr 2 & \quad f \gets \mathrm{leaf}( x ) \cr 3 & \quad \textbf{if}\ f \text{ holds } x\ \textbf{then} \ \text{replace its value by } v;\ \textbf{return} \cr 4 & \quad \text{insert } ( x , v ) \text{ into } f \text{, keeping the keys sorted} \cr 5 & \quad \textbf{if}\ f \text{ has more than } m \text{ keys} \ \textbf{then}\quad \triangleright\ \text{an overflow: split} \cr 6 & \qquad g \gets \text{a new leaf, chained before } f \text{, that takes the first } \lceil ( m + 1 ) / 2 \rceil \text{ pairs of } f \cr 7 & \qquad \mathrm{up}( f , g , \text{the largest key of } g ) \cr 8 & \textbf{procedure}\ \mathrm{up}( f , g , s )\quad \triangleright\ g \text{, a new sibling to the left of } f \text{, and its separator } s \cr 9 & \quad b \gets \text{the parent of } f \cr 10 & \quad \textbf{if}\ b = \textbf{nil}\ \textbf{then}\ \mathit{root} \gets \text{a new node of children } g , f \text{ and key } s; \ \textbf{return} \cr 11 & \quad \text{in } b \text{, insert } g \text{ just before } f \text{ and } s \text{ just before the key on the right of } g \cr 12 & \quad \textbf{if}\ b \text{ has more than } m \text{ children} \ \textbf{then}\quad \triangleright \ \text{the split goes one level up} \cr 13 & \qquad c \gets \text{a new node that takes the first } \lceil ( m + 1 ) / 2 \rceil \text{ children of } b \text{ and the keys among them} \cr 14 & \qquad \mathrm{up}( b , c , \text{the key of } b \text{ that separated } c \text{ from the rest} ) \end{array}} \]

◆ levels()

template<std::totally_ordered K, class V>
std::vector< std::vector< std::vector< K > > > ADS::BPlusTree< K, V >::levels ( void ) const
nodiscard

the keys of the nodes, level by level from the root, each level from left to right: the shape of the tree, as in figure 6.7, by a visit in breadth

◆ lookup()

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

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

In pseudocode (section 6.3), \( u.k[ 0 .. f - 2 ] \) the keys and \( u.p[ 0 .. f - 1 ] \) the children of an internal node \( u \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lookup}( x ) \cr 2 & \quad u \gets \mathrm{leaf}( x );\ i \gets \text{the least place of } u \text{ with } x \le u.k[ i ] \cr 3 & \quad \textbf{return}\ u.v[ i ] \text{ if there is such a place and } u.k[ i ] = x \text{, else } \textbf{nil} \cr 4 & \textbf{function}\ \mathrm{leaf}( x )\quad \triangleright \ \text{the leaf that holds } x \text{, or would hold it} \cr 5 & \quad u \gets \mathit{root} \cr 6 & \quad \textbf{while}\ u \text{ is an inner node}\ \textbf{do}\ u \gets u.p[ i ] \text{ for the least } i \text{ with } x \le u.k[ i ] \cr 7 & \quad \textbf{return}\ u \end{array}} \]

◆ lowerBound()

template<std::totally_ordered K, class V>
const K * ADS::BPlusTree< K, V >::lowerBound ( const K & x) const
nodiscard

the smallest key \( \ge x \), nil if none (the find( x ) of exercise 14.8 of Morin's book), in \( O( \log_m n ) \) nodes

◆ lowerBoundKey()

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

the address of the smallest key not smaller than \( k \), nil if none, in \( O( \log_m n ) \) nodes: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the next change

◆ operator=()

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

no copy, since the nodes are linked by pointers

◆ order()

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

the order \( m \)

◆ range()

template<std::totally_ordered K, class V>
std::vector< std::pair< K, V > > ADS::BPlusTree< K, V >::range ( const K & A,
const K & B ) const
nodiscard

the pairs with keys in \( [ A , B ] \), read along the list of the leaves, \( O( \log n + k ) \) for \( k \) pairs

In pseudocode (section 6.3), the leaf of \( A \) and then the following ones:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{range}( A , B ) \cr 2 & \quad \mathit{out} \gets \text{an empty sequence} \cr 3 & \quad \textbf{for}\ f \gets \mathrm{leaf}( A ) , f.\mathit{next} , \ldots \text{ while } f \ne \textbf{nil}\ \textbf{do}\quad \triangleright\ \text{along the chain of the leaves} \cr 4 & \qquad \textbf{foreach}\ ( k , v ) \text{ of } f \text{, by increasing } k\ \textbf{do} \cr 5 & \qquad\quad \textbf{if}\ B < k\ \textbf{then return}\ \mathit{out} \cr 6 & \qquad\quad \textbf{if}\ A \le k\ \textbf{then}\ \text{append } ( k , v ) \text{ to } \mathit{out} \cr 7 & \quad \textbf{return}\ \mathit{out} \end{array}} \]

◆ remove()

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

removes the key \( x \), if it is there, in \( O( m \log_m n ) \) time

In pseudocode (exercise 14.8 of Morin's book), \( h \) the least number of keys of a leaf and of children of an internal node, \( \lceil m / 2 \rceil \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( x ) \cr 2 & \quad f \gets \mathrm{leaf}( x );\ \textbf{if}\ x \text{ is not } \text{in } f\ \textbf{then return} \cr 3 & \quad \text{take } x \text{ out of } f \cr 4 & \quad \textbf{while}\ f \ne \mathit{root}\ \textbf{and}\ f \text{ has fewer than } h \text{ pairs or children}\ \textbf{do} \cr 5 & \qquad s \gets \text{the sibling on the left of } f \text{, or on } \text{the right};\ b \gets \text{their parent} \cr 6 & \qquad \textbf{if}\ s \text{ has more than } h\ \textbf{then} \ \text{move its item next to } f \text{ into } f \text{, through } \text{the separator in } b;\ \textbf{return} \cr 7 & \qquad \text{merge } f \text{ and } s \text{ (and the separator, } \text{if internal), and take it out of } b;\ f \gets b \cr 8 & \quad \textbf{if}\ \text{the root is internal with one child} \ \textbf{then}\ \text{the child becomes the root} \end{array}} \]

Postcondition
\( x \) is not there

◆ size()

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

the number \( n \) of keys


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