Insertion into classic B-trees #
Insert-or-replace in the B-trees of Cslib.DataStructure.BTree, by the bottom-up algorithm of
the functional literature: a recursive call returns either a tree of the same height or an
overflow split around a median entry (InsertResult), and the parent absorbs the overflow,
splitting in turn when it then has 2 * t keys (InsertResult.ofNode). An overflow at the root
grows a new root (InsertResult.toTree), so the height grows only there. The insertion into the
empty tree overflows at once, which makes the bottom level uniform with the rest. This is the
shape of the classic algorithm of [BMcC72]; [CLRS09], Chapter 18, splits full
nodes pre-emptively on the way down instead. The tree-or-split result follows the functional
insertion of [Mun21], whose up⇩i it mirrors.
Each operation is defined once over an arbitrary monad m, with a comparator
le : α → α → m Bool and a node-visit action visit : m Unit, as in
Cslib.DataStructure.BTree.Lookup. insertOrSplitM inserts into a subtree and insertRestM
searches among the triples of a node, with the scan and the equality test of lookupM. visit
fires once on entering each node of the search path, and never at the empty leaf. The pure
operations are the Id instantiations with leI and visit := pure (). The definitions are
structurally recursive and @[expose]d, so decide evaluates insertion on concrete trees from
other modules, and the monadic constructor equations hold by rfl. Costs are not stated here.
Main definitions #
InsertResult: a tree of the same height, or an overflow split around a median entry.Good: a balanced, occupied subtree of a given height, the shape of every child of a well-formed node.InsertResult.Fits: the shape invariant of a result relative to the subtree it replaces.insertOrSplitM,insertRestM,insertM,insert: insert-or-replace.
Main results #
Cslib.IsMonadHom.map_insertM: a monad morphism commutes with insertion.toList_insert_of_isSortedAssoc,toList_insert: the model equation,toListof the insertion isSortedAssoc.insertontoList.fits_insertOrSplit: the shape invariant on balanced trees with occupied children.IsWF.insert: insertion preserves well-formedness.height_insert: the height stays or grows by one.size_insert,lookup_insert: size and lookup after insertion.
References #
- R. Bayer and E. McCreight, Organization and Maintenance of Large Ordered Indexes
- 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/Insert.lean.
The result of inserting into a subtree: a tree of the same height, or an overflow split into two trees of the same height around a median entry.
- tree {α β : Type} (tr : BTree α β) : InsertResult α β
The subtree absorbed the entry.
- split {α β : Type} (left : BTree α β) (k : α) (v : β) (right : BTree α β) : InsertResult α β
The subtree overflowed and split around the entry
(k, v).
The entries of a result in order.
Put a result in the place of a child followed by the triples rest.
A node with contents first and rest, split when it has more than 2 * t - 1 keys:
the first t keys stay on the left, the next one is the median, and the rest go right. The
[] branch is unreachable for t ≥ 1.
The tree of a result at the root: an overflow grows a new root.
Growing a new root keeps the entries.
Placing a result in the place of a child keeps the entries, followed by those of rest.
Shape #
A subtree of height h that is balanced and occupied: what every child of a well-formed
node is.
A node with t - 1 to 2 * t - 1 keys over good children of height h is good, of height
h + 1.
The children of a balanced node whose children are occupied are good, of the height of its first child.
A node over good children of height h with more than 2 * t - 1 keys, and at most t + 1
more, splits into two good trees of height h + 1: the first t keys go left, the next one is
the median, and the rest go right.
The shape invariant of the result of inserting into tr: either a tree of the same
height with at least as many root keys, still within 2 * t - 1, or two good trees of the
same height.
A node over good children of height h with at most one key more than the
2 * t - 1 allowed, after the overflow split, fits any non-empty tree of height h + 1 that had
no more keys.
Placing a fitting result in the place of an occupied child of height h among good
triples keeps every child good, and adds at most one key.
Bottom-up insertion into a subtree: the tree with the entry inserted, or its split when it overflows. A node is visited once, on entry; the empty leaf is not visited and overflows at once into the entry between two empty leaves.
Insertion among the triples of a node, scanning them in order with the comparisons of
lookupM: none defers to the child before the triples, because the key is below the first
of them; some rest' is the new list of triples, with the value replaced at an equal key or
the result of the descent placed in the child after the last key below the query.
A node is visited once, before its triples are searched; the result is split when it overflows.
At a triple: replace on equality, defer to the preceding child when the key is below, and otherwise move on, descending into this triple's child when every later key is above.
Insertion is the bottom-up insertion with an overflow at the root grown into a new root.
A monad morphism commutes with the bottom-up insertion into a subtree.
A monad morphism commutes with the insertion among the triples of a node.
A monad morphism commutes with insertion.
Pure bottom-up insertion into a subtree.
Pure insertion among the triples of a node.
Pure insertion.
insertOrSplit is the Id instantiation of insertOrSplitM.
insertRest is the Id instantiation of insertRestM.
Insertion is the bottom-up insertion with an overflow grown into a new root.
Inserting into the empty tree overflows at once.
Insertion into a node: search its triples, descending into the first child when the key is below them, then split on overflow.
The search past the last triple defers to the child before it.
The search at a triple: replace on equality, defer when the key is below, and otherwise move on, descending into this triple's child when every later key is above.
The search among the triples of a node, against the model: when it defers to the child before the triples, the key is below all their entries; otherwise it performs the model insertion after any sorted prefix of smaller keys.
Model equation of the bottom-up insertion on a subtree with sorted entries.
Model equation of insertion on a tree with sorted entries.
The search among the triples of a node keeps every child good, and adds at most one key.
Shape invariant of the bottom-up insertion.
Model equation of insertion on well-formed trees.
Insertion preserves well-formedness.
On a tree with sorted entries, insertion keeps the size when the key is present and adds one otherwise.
On a tree with sorted entries, lookup after insertion finds the new value at its key and is unchanged elsewhere.
Lookup after insertion finds the new value at its key and is unchanged elsewhere.