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

The B-tree of chapter 14 of Morin's book, beyond the book, on a BlockStore [see BlockStore.h]: a SORTED SET whose nodes are blocks of the external memory, each read and written by one I/O, and whose lowerBound( x ) (the smallest key \( \ge x \), the find( x ) of the SSet of the book), insert() and remove() take \( O( \log_B n ) \) I/Os, \( O( B \log_B n ) \) time with a binary search in each node. More...

#include "ADS/Contract.h"
#include "ADS/external/BlockStore.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <optional>
#include <tuple>
#include <utility>
#include <vector>
#include "ADS/external/impl/BlockBTree.tpp"

Go to the source code of this file.

Classes

class  ADS::BlockBTree< K >
 a B-tree of keys whose nodes are the blocks of a BlockStore More...

Namespaces

namespace  ADS

Enumerations

enum class  ADS::BTreePolicy { ADS::BTreePolicy::STANDARD , ADS::BTreePolicy::WIDE , ADS::BTreePolicy::THREE }
 how a BlockBTree keeps its nodes within their bounds More...

Detailed description

The B-tree of chapter 14 of Morin's book, beyond the book, on a BlockStore [see BlockStore.h]: a SORTED SET whose nodes are blocks of the external memory, each read and written by one I/O, and whose lowerBound( x ) (the smallest key \( \ge x \), the find( x ) of the SSet of the book), insert() and remove() take \( O( \log_B n ) \) I/Os, \( O( B \log_B n ) \) time with a binary search in each node.

A node holds from \( B - 1 \) to \( 2 B - 1 \) keys (the root from 0 or 1) and an internal node one child more than its keys, all the leaves at the same depth. insert() puts the key into its leaf and splits a node of \( 2 B \) keys into two of \( B \) and \( B - 1 \), the middle key going up to the parent, up to the root, which may split and give a new root; remove() takes the key out of its leaf, or replaces it in an internal node by its successor taken out of a leaf, and a node left with \( B - 2 \) keys borrows from its sibling on the left (on the right for the first child), which takes so many keys that both have about half, or is merged with it, when the sibling has \( B - 1 \) keys only. A sequence of \( m \) operations from the empty tree makes at most \( 3 m / 2 \) splits, merges and borrows (Lemma 14.1 of Morin's book), \( \Theta( m ) \) in the worst case, e.g., an insertion and a removal alternating at a full leaf. The two variants of the exercises make them \( O( m / B ) \), by leaving each node that they touch at \( \Omega( B ) \) keys from both of its bounds [see BTreePolicy]:

  • WIDE (exercise 14.6): from \( B \) to \( 3 B \) children, a node of \( 3 B \) keys split into two of about \( 1.5 B \), and a node in underflow merged with its sibling whenever the two have at most \( 2.5 B \) keys, before it is needed, so that the merged node is \( B / 2 \) keys below the top, the two otherwise sharing their keys at about \( 1.25 B \) or more each;
  • THREE (exercise 14.7): the bounds of the book, a node in overflow sharing its keys with a sibling, into two nodes if the two have at most about \( 3.5 B \) keys and into three otherwise, and a node in underflow sharing them with two siblings, into two nodes or three in the same way, so that every node touched has between about \( 7 B / 6 \) and \( 7 B / 4 \) keys; a tree of two leaves, where these moves lack the siblings, lets its leaves hold down to \( \max( 1 , \lfloor B / 2 \rfloor ) \) keys, and a leaf below that is merged with the other one into a root leaf if the merged leaf has at most \( 2 B - 1 - \lfloor B / 4 \rfloor \) keys, and shares its keys with it otherwise, so that the split of a root leaf, into two leaves of about \( B \) keys, and this merge also leave \( \Omega( B ) \) keys from the bounds.

In both, a node touched needs \( \Omega( B ) \) operations below it before it is touched again, so that a potential of the distance of each node from its bounds pays the restructurings, \( O( m / B ) \) in all (see The B-tree on the blocks of Morin's book); with THREE the restructurings at the internal children of a root of two children follow the rules of the book, but each of them is caused by a restructuring below, and so they are \( O( m / B ) \) too.

The internal memory is \( O( B + \log_B n ) \), the claim of the introduction of chapter 14 that the recursive code of the book does not keep (exercise 14.4), since its recursion holds a node per level, \( \Theta( B \log_B n ) \): here the descent keeps the path as the indices of its blocks and of the children taken, \( O( \log_B n ) \) words, and at most five nodes counted in memory at once (a node, its parent and the at most three children whose keys a restructuring gathers, the node among them); a node is read again on the way up only when a split, a merge, a borrow or the replacement of a removed key reaches it, so that the I/Os stay \( O( \log_B n ) \). peakNodes() gives the most nodes held at once, and restructures() the splits, merges and borrows.

Every restructuring is one routine, which gathers the keys of \( k \) consecutive children of a node and the separators between them and deals them out again to \( k' \) children, the first ones one key more when they do not divide evenly: a split is \( 1 \to 2 \), a borrow \( 2 \to 2 \), a merge \( 2 \to 1 \), and the three-node moves \( 2 \to 3 \), \( 3 \to 3 \) and \( 3 \to 2 \).

Against the code of Morin's book: the memory above; a borrow from a sibling of \( B \) keys, as the text says, where the code of the book merges unless the sibling has more than \( B \); the keys of any type with an order, where the book marks the empty places by a null key; and the blocks copied by their I/Os [see BlockStore.h].

The contracts [see Contract.h]: \( B \ge 2 \); insert() ensures that the key is there and remove() that it is not; the invariant (the order of the keys, their number in each node, the depth of the leaves and size()) is checked with no I/O at the level 3 while \( n \le 1024 \). The operations are defined in BlockBTree.tpp.

See also
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, chapter 14 and exercises 14.1 to 14.7
R. Bayer, E. M. McCreight, Organization and maintenance of large ordered indexes, Acta Informatica 1(3), 173-189, 1972, doi:10.1007/BF00288683
S. Huddleston, K. Mehlhorn, A new data structure for representing sorted lists, Acta Informatica 17(2), 157-184, 1982, doi:10.1007/BF00288968
D. E. Knuth, The Art of Computer Programming, vol. 3, 2nd ed., Addison-Wesley, 1998, section 6.2.4 (the B*-trees, whose overflow is shared with a sibling)
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa