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

The B-tree of Bayer and McCreight, beyond the book [see BPlusTree.h for the B+-tree of chapter 6, which has no removal]: a search tree whose nodes hold from \( t - 1 \) to \( 2 t - 1 \) keys (the root from 1), \( t \) the minimum degree, a node of \( j \) keys having \( j + 1 \) children, and all the leaves at the same depth, so that the height is at most \( \log_t( ( n + 1 ) / 2 ) \) and a node fills a block of the disk. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/Modification.h"
#include "ADS/Portable.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <iterator>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "ADS/tree/impl/BTree.tpp"

Go to the source code of this file.

Classes

class  ADS::BTree< K, V, Policy >
 a DICTIONARY on a B-tree of minimum degree \( t \), its changes told through Policy More...
class  ADS::BTree< K, V, Policy >::const_iterator
 the iterator on the pairs by increasing keys, with the path from the root More...

Namespaces

namespace  ADS

Detailed description

The B-tree of Bayer and McCreight, beyond the book [see BPlusTree.h for the B+-tree of chapter 6, which has no removal]: a search tree whose nodes hold from \( t - 1 \) to \( 2 t - 1 \) keys (the root from 1), \( t \) the minimum degree, a node of \( j \) keys having \( j + 1 \) children, and all the leaves at the same depth, so that the height is at most \( \log_t( ( n + 1 ) / 2 ) \) and a node fills a block of the disk.

insert() goes down from the root splitting each full node it meets, so that the leaf reached has room; remove() goes down making sure that each node it enters has at least \( t \) keys, by borrowing a key from a sibling or by merging with it, so that a key can always be taken out of a leaf (a key of an internal node is replaced by its predecessor or successor, or the two children around it are merged); both in one pass, \( O( t \log_t n ) \) time. It is a DICTIONARY [see ADT.h], and forEach() visits the pairs by increasing keys.

The tree tells its changes through the subject policy Policy [see Modification.h], NoObservers by default, with which every operation compiles to the code of the plain tree: Inserted by insert() of a key that is not there, Assigned by insert() of a key that is there (the old and the new value), Erased by remove() of a key that is there, told before the removal with its value, Cleared by clear() and Rebuilt at the end of a mute; the handle of an event is its key. With someone listening, an insertion and a removal make one more descent, to read the old value; an observer that refuses an insertion by prepare() leaves the tree as it was, and one that throws from on() of Assigned gets the old value back (the strong guarantee), while one that throws from on() of Inserted leaves the key in the tree, since a removal may allocate. The splits, the borrowings and the merges of the nodes are internal maintenance, and are not told. A tree with observers is neither copied nor moved.

The contracts [see Contract.h]: \( t \ge 2 \); insert() ensures that the key is there and remove() that it is not; the invariant is the order of the keys, their number in each node and the depth of the leaves, an \( O( n ) \) check at the level 3. The operations are defined in BTree.tpp.

See also
R. Bayer, E. M. McCreight, Organization and maintenance of large ordered indexes, Acta Informatica 1(3), 173-189, 1972, doi:10.1007/BF00288683
T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009, chapter 18
D. E. Knuth, The Art of Computer Programming, vol. 3, 2nd ed., Addison-Wesley, 1998, section 6.2.4
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa