ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BPlusTree.h File Reference

The B+-tree of order \( m \) of the reality check of chapter 6, the search tree of the file systems and of the databases, whose nodes are pages of the secondary memory. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <iterator>
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include "ADS/tree/impl/BPlusTree.tpp"

Go to the source code of this file.

Classes

class  ADS::BPlusTree< K, V >
 a B+-tree of order \( m \), with the removal of exercise 14.8 of Morin's book More...
class  ADS::BPlusTree< K, V >::const_iterator
 the iterator on the pairs by increasing keys, a leaf and a place in it More...

Namespaces

namespace  ADS

Detailed description

The B+-tree of order \( m \) of the reality check of chapter 6, the search tree of the file systems and of the databases, whose nodes are pages of the secondary memory.

An internal node has \( f \) children, \( \lceil m / 2 \rceil \leq f \leq m \) (the root at least 2), and \( f - 1 \) keys \( k[ 0 ] , \ldots , k[ f - 2 ] \): the child \( p[ i ] \) holds the keys in \( ( k[ i - 1 ] , k[ i ] ] \), \( p[ 0 ] \) those up to \( k[ 0 ] \) and \( p[ f - 1 ] \) those larger than \( k[ f - 2 ] \). The values are only in the leaves, which hold up to \( m \) keys each, are all on the same level and are linked in a list in the order of the keys, so that the \( k \) keys in an interval are read in \( O( \log n + k ) \) time [see range()]. The height is \( O( \log_{\lceil m / 2 \rceil} n ) \): with pages of 4 KB and \( m = 256 \) a few levels index billions of keys.

The search goes down to the first child whose key is not smaller than the one looked for. The insertion goes into the leaf of the key; a leaf with \( m \) keys already splits its \( m + 1 \) keys between a new leaf on its left, with the first \( \lceil ( m + 1 ) / 2 \rceil \), and itself, and the largest key of the new leaf goes into the parent next to it; a parent with \( m + 1 \) children splits in the same way, the middle key going one level up, up to the root, which may split and give a new root with two children. The book describes the removal only as a series of cases similar to those of the insertion; remove() is that of exercise 14.8 of Morin's book: the key goes out of its leaf, and a leaf left with fewer than \( \lceil m / 2 \rceil \) keys takes a key from a sibling that has more, through their parent, whose separator becomes the largest key of the left one, or is merged with it, the parent losing a child; an internal node left with fewer than \( \lceil m / 2 \rceil \) children does the same with a sibling, the separator in the parent going down between the two, and a root left with one child gives its place to it. A separator need not be a key that is still there, only a bound of the keys on its two sides, so that a removal changes no separator but those of the borrows and of the merges. lowerBound( x ) gives the smallest key \( \ge x \) (the find( x ) of the exercise) and findRange( x , y ) the keys in \( ( x , y ] \), the operations of the exercise, the latter along the list of the leaves in \( O( \log_m n + k ) \) for \( k \) keys. The separators repeat a key per leaf but the last, at most \( n / \lceil m / 2 \rceil \) keys of \( n \), a share that vanishes as \( m \) grows (the last point of the exercise).

The contracts [see Contract.h]: the order is \( m > 2 \), as the book requires, and the invariant is the shape of the tree, at least \( \lceil m / 2 \rceil \) pairs in a leaf but the root, checked after each insertion and removal [see shaped()]. The operations are defined in BPlusTree.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 6.3, https://algoritmicamente.it/materiali/libro/capitoli/cap06.pdf
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, exercise 14.8
R. Bayer, E. M. McCreight, Organization and maintenance of large ordered indexes, Acta Informatica 1(3), 173-189, 1972, doi:10.1007/BF00288683
D. Comer, The ubiquitous B-tree, ACM Computing Surveys 11(2), 121-137, 1979, doi:10.1145/356770.356776
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa