a B+-tree of order \( m \), with the removal of exercise 14.8 of Morin's book
More...
|
| | 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()
|
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
template<std::totally_ordered K, class 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}}
\]
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}}
\]
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}}
\]
template<std::totally_ordered K, class V>
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