Erasure from B+-trees #
Erasure for the B+-trees of Cslib.DataStructure.BPlusTree, bottom-up, with the shape of the
classic Cslib.DataStructure.BTree.eraseM: the entry is removed from a leaf, and a leaf or node
left one item short (underflows) is repaired by its parent against an adjacent sibling.
The repair merges the two siblings and splits the result again with the insertion splitters when
it has more than 2 * t - 1 items (mergeSplit), which covers both borrowing and merging. Two
leaves are concatenated and their separator is dropped; if they split again, the first key of the
new right leaf is copied up. Two nodes are joined around their separator, which is pulled down;
if they split again, a separator moves up. A child is repaired against its right sibling, and
the last child against its left one. A root left without separators collapses into its only
child (collapse), so the height falls only there.
Every entry is in a leaf, so there is no interior case: the search descends by the separators to
one leaf and removes the entry there. A separator changes only in mergeSplit: a separator equal
to the erased key stays where it is, and need not be a key afterwards, unless a repair drops or
replaces it.
Each operation is defined once over an arbitrary monad m, with a comparator
le : α → α → m Bool and a node-visit action visit : m Unit, and no order instance in scope.
The pure operations are the Id instantiations with the comparator Cslib.SortedAssoc.leI of a
linear order and visit := pure (). visit fires once at each node on the path from the root to
the leaf, and once more for each sibling a repair inspects. At an interior node the separators
are scanned as in slotM, one comparison per separator passed and one more where the scan stops.
In the leaf the entry is removed by Cslib.SortedAssoc.eraseM. eraseRestM takes the child
before the branches together with its erasure as a delayed computation, a function from Unit:
the recursion is then structural, so the definitions are @[expose]d and decide evaluates
erasure on concrete trees, and the erasure of a child is run only where the search descends, in
Id as in any other monad. Costs are not stated here.
Main definitions #
underflows,mergeSplit,collapse: underflow, the sibling repair and the root collapse.rebalanceRightM,rebalanceLastM: the repairs against the right and the left sibling.eraseSubtreeM,eraseRestM,eraseM: erasure, polymorphic.erase: the pure operation.
Main results #
map_eraseM: a monad morphism commutes with erasure.toList_erase: on a well-formed tree, the entries after erasure areCslib.SortedAssoc.eraseof the entries.IsWF.erase: erasure preserves well-formedness.height_erase: the height stays or falls by one.size_erase: the size falls by one exactly when the key was present.toList_erase_of_lookup_eq_none: erasing an absent key keeps the entries.
The model equation needs more than sorted entries, as for insertion: the search trusts each separator to bound the later children from below, which holds through a key of the child the separator introduces, so the children must not be empty. Well-formedness provides that.
References #
- R. Bayer and E. McCreight, Organization and Maintenance of Large Ordered Indexes
- D. Comer, The Ubiquitous B-Tree
- N. Mündler, A Verified Imperative Implementation of B-Trees
The repair by merging and splitting again follows the functional deletion of [Mun21] for classic B-trees. The statements here are not taken from a source.
Merge two adjacent siblings, split again by the insertion splitters when the result has more
than 2 * t - 1 items. Two leaves are concatenated and the separator is dropped; two nodes are
joined around the separator. Siblings of a balanced tree are both leaves or both nodes; the
last case is unreachable there.
Drop an empty root: a node without separators is replaced by its only child.
Repair a child c followed by its right sibling r under the separator s and the branches
rest: if c underflows, inspect r (one visit) and merge them.
Erasure among the branches after the child prev, whose erasure is prevErased (), scanning
the separators in order with the comparisons of slotM. At a separator above the key, prev is
erased from and repaired against its right sibling. Past the last separator, the last child is
erased from and repaired against its left sibling.
Remove the entry with key k, if any, from a B+-tree of minimum degree t, comparing with
le and calling visit on each node of the search path and on each sibling a repair inspects;
a root left without separators collapses into its only child.
A leaf is visited once, and the entry is removed by the scan of its entries.
A node is visited once on entry, then its branches are searched.
At a branch: when its separator is above the key, erase from the preceding child and repair it against this child; otherwise move on, erasing from the last child and repairing it against its left sibling.
A monad morphism commutes with erasure.
Shape #
Pure erasure.
The model equation #
The shape of the result #
The separator convention of the result #
Erasure on well-formed trees #
Model equation of erasure on well-formed trees.
Erasure preserves well-formedness.