Erasure from classic B-trees #
Erasure in the B-trees of Cslib.DataStructure.BTree, by the bottom-up algorithm of the
functional literature, the mirror image of Cslib.DataStructure.BTree.Insert. The repair follows
the functional deletion of [Mun21]: mergeSplit merges two nodes and splits the result
with the insertion splitter, as its node⇩i does, and rebalanceRightM and rebalanceLastM are
its rebalance_middle_tree and rebalance_last_tree. [Mun21] replaces an interior key by
its predecessor; this version uses the successor, which sits in the same triple of the
interleaved representation.
Erasure from a subtree may leave it one key short (underflows); the parent repairs such a child
against an adjacent sibling by merging the two around their separator and splitting the result
again when it has more than 2 * t - 1 keys (mergeSplit, through InsertResult.ofNode). That
single repair covers both borrowing and merging in [CLRS09], Chapter 18. A child is repaired
against its right sibling, and the last child against its left one. An empty root collapses into
its only child (collapse), so the height falls only there. A key found in an interior node is
replaced by its successor, the least entry of the child after it, which popMinM removes with
the same repairs; erasure is not restricted to leaves.
Each operation is defined once over an arbitrary monad m, with a comparator
le : α → α → m Bool and a node-visit action visit : m Unit. eraseSubtreeM and eraseRestM
are the recursion of eraseM, with the scan and the equality test of lookupM. visit fires
on entering each node of the search path, including the path of popMinM to the successor, and
once more for each sibling a repair inspects, so a descent visits at most two nodes per level.
The pure operations are the Id instantiations with leI and visit := pure ().
eraseRestM takes the child before the triples 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 from other modules, the monadic
constructor equations hold by rfl, 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,popMinM: repairs and removal of the least entry.eraseSubtreeM,eraseRestM,eraseM,erase: erasure.Underfull,EraseFits: the shape of a child after erasure and the invariant of a result.
Main results #
Cslib.IsMonadHom.map_eraseM: a monad morphism commutes with erasure.toList_erase_of_isSortedAssoc,toList_erase: the model equation,toListof the erasure isSortedAssoc.eraseontoList.eraseFits_eraseSubtree: the shape invariant.IsWF.erase: erasure preserves well-formedness.height_erase: the height stays or falls by one.size_erase,lookup_erase: size and lookup after erasure.
References #
- T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein, Introduction to Algorithms, Chapter 18
- N. Mündler, A Verified Imperative Implementation of B-Trees
Ported from cslib-community/EpsilonEridani at commit 1966ab476:
EpsilonEridani/Algorithms/Lean/BTree/Erase/Basic.lean.
Merge two adjacent siblings around their separator into one node, split again by
InsertResult.ofNode when it has more than 2 * t - 1 keys.
Drop an empty root: a node without keys is replaced by its only child.
Repair a child c followed by its right sibling s with separator (k, v) and the
triples rest: if c underflows, inspect s (one visit) and merge them.
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;
an emptied root collapses into its only child.
A node is visited once on entry, then its triples are searched.
At a triple: on equality, replace the entry by the least entry of its child and repair the child; below the key, erase from the preceding child and repair it against this child; above it, move on, repairing the last child against its left sibling.
A node is visited once on entry, its first child searched, and the result repaired.
A monad morphism commutes with the repair against the right sibling.
A monad morphism commutes with the repair against the left sibling.
A monad morphism commutes with the removal of the least entry of a subtree.
A monad morphism commutes with erasure from a subtree.
A monad morphism commutes with erasure among the triples of a node.
A monad morphism commutes with erasure.
rebalanceRight is the Id instantiation of rebalanceRightM.
rebalanceLast is the Id instantiation of rebalanceLastM.
Pure repair against the left sibling merges only an underflowing last child.
Removing the least entry from a node: from its first child, repaired, or the node's first entry when the first child has none.
Repair against the right sibling keeps the entries.
Repair against the left sibling keeps the entries.
Removing the least entry: none exactly on a tree without entries, and otherwise the
first entry followed by the entries of the rest.
Shape #
A subtree of height h that may be one key short: balanced, with occupied children, at
most 2 * t - 1 keys, and at least t - 2 keys when it is a node.
- balanced : c.Balanced
The subtree is balanced.
The subtree has height
h.Every child of the root is occupied.
The root has at most
2 * t - 1keys.A node root has at least
t - 2keys.
The shape of the result c of erasing from tr: balanced, of the same height, with
occupied children, and at most one root key fewer.
- balanced : c.Balanced
The result is balanced.
The result has the height of
tr.Every child of the result's root is occupied.
The result's root has no more keys than that of
tr.The result's root has at most one key fewer than that of
tr.
A repaired child, or a repaired pair of children around a separator, all good.
Only a node underflows.
Erasure from a good subtree leaves it at most one key short, given the shape of the erasure under the occupancy facts a good subtree provides.
Re-splitting a node of between t - 1 and 2 * t - 1 + t + 1 keys (3 * t when 1 ≤ t)
over good children gives good results.
Placing a repaired result among good triples keeps them good, adding at most one key.
Merging two adjacent nodes of height h + 1 with between t - 2 and 3 * t - 3 keys in
total gives good results.
Repair against the right sibling: the results are good, with one key fewer or none.
Repair of a last child against its left sibling: the results are good, with at most one triple.
Shape of the removal of the least entry.
Pure erasure from a subtree.
Remove the entry with key k, if any, from a B-tree of minimum degree t: the Id
instantiation of eraseM. An emptied root collapses into its only child.
eraseSubtree is the Id instantiation of eraseSubtreeM.
eraseRest is the Id instantiation of eraseRestM, with prevErased wrapped in a
constant thunk.
Erasure from the empty tree.
Erasure from the empty tree is the empty tree.
Erasure from a node is erasure among its triples.
Pure erasure at a triple.
Erasure is erasure from the root subtree, then collapsing an empty root.
The search among the triples after prev, against the model: on sorted entries, given
that prevErased is the erasure of prev, the result has the entries of the model erasure.
Model equation of erasure from a subtree with sorted entries.
Model equation of erasure on a tree with sorted entries.
Shape of the erasure among the triples after a good child prev, whose erasure pe is at
most one key short: the results are good, with at most one key fewer.
Shape invariant of erasure from a subtree.
Model equation of erasure on well-formed trees.
Erasure preserves well-formedness.
On a tree with sorted entries, erasure removes one entry when the key is present and none otherwise.
On a tree with sorted entries, lookup after erasure fails at the erased key and is unchanged elsewhere.
On a tree with sorted entries, erasing an absent key leaves the entries unchanged.