Insertion into B+-trees #
Insert-or-replace for the B+-trees of Cslib.DataStructure.BPlusTree, bottom-up: the entry is
inserted into a leaf, and a leaf or node that overflows splits, handing a separator to its
parent, which may overflow in turn; an overflow at the root grows a new root.
The two splits differ. A leaf with more than 2 * t - 1 entries keeps its first t entries, and
the rest form a new leaf whose first key is copied up as the separator: every entry stays in a
leaf. A node with more than 2 * t - 1 separators keeps its first t, the next separator moves
up, and its child becomes the first child of the new node.
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, so
that every comparison of keys goes through le. 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. At an interior node the
separators are scanned as in slotM, one comparison per separator passed and one more where the
scan stops, and a separator equal to the key does not end the search: the key goes into the child
that the separator introduces. In the leaf the entry is placed by Cslib.SortedAssoc.insertM.
The recursion is structural, so the definitions are @[expose]d and decide evaluates insertion
on concrete trees. Costs are not stated here.
Main definitions #
InsertResult: a tree, or the two halves of a split with the separator between them.InsertResult.ofLeaf,InsertResult.ofNode: the leaf and the node split.insertOrSplitM,insertRestM,insertM: bottom-up insertion, polymorphic.insertOrSplit,insertRest,insert: the pure operations.Good: a balanced, occupied subtree of a given height, which is what a split produces (InsertResult.exists_good_split_ofLeaf,InsertResult.exists_good_split_ofNode).Linked,Sep: the separator convention between neighbouring children, and that of a result (separated_node_of_linked,sep_ofLeaf,sep_ofNode,linked_withRest_of_sep).
Main results #
map_insertM: a monad morphism commutes with insertion.toList_insert: on a well-formed tree, the entries after insertion areCslib.SortedAssoc.insertof the entries.IsWF.insert: insertion preserves well-formedness.height_insert,height_insert_le,height_le_height_insert: the height grows by one exactly when the root splits.size_insert: the size grows by one exactly when the key was absent.
The model equation needs more than sorted entries: 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. The separator convention is preserved because, when no child is empty, it is a condition between neighbouring children, and an insertion changes one child or puts two in its place.
References #
- R. Bayer and E. McCreight, Organization and Maintenance of Large Ordered Indexes
- D. Comer, The Ubiquitous B-Tree
- T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein, Introduction to Algorithms, Chapter 18
The statements here are not taken from a source.
The result of inserting into a subtree: a tree of the same height, or an overflow split into two trees of the same height with the separator that goes between them.
- tree {α β : Type} (tr : BPlusTree α β) : InsertResult α β
The subtree absorbed the entry.
- split {α β : Type} (left : BPlusTree α β) (sep : α) (right : BPlusTree α β) : InsertResult α β
The subtree overflowed and split, with
sepseparating the halves.
The entries of a result in order. A separator is not an entry.
Put a result in the place of a child followed by the branches rest.
The tree of a result at the root: an overflow grows a new root.
A leaf with the given entries, split when it has more than 2 * t - 1 of them: the first
t entries stay on the left, the rest go right, and the first key on the right is copied up as
the separator. The [] branch is unreachable.
A node with contents first and rest, split when it has more than 2 * t - 1 separators:
the first t branches stay on the left, the separator of the next one moves up, and its child
starts the right node. The [] branch is unreachable.
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 separators 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 leaf with more than 2 * t - 1 entries, and at most t more, splits into two good
leaves: the first t entries go left and the rest go right.
A node over good children of height h with more than 2 * t - 1 separators, and at most
t + 1 more, splits into two good trees of height h + 1: the first t branches go left, the
separator of the next one moves up, and the rest go right.
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.
Insertion among the branches of a node, scanning the separators in order with the
comparisons of slotM: none defers to the child before the branches, because the key is below
the first separator; some rest' is the new list of branches, with the result of the descent
placed in the child after the last separator that is at most the key.
A leaf is visited once; the entry is placed by the scan of its entries, and the leaf is split when it overflows.
A node is visited once, before its separators are searched; the result is split when it overflows.
At a branch: defer to the preceding child when its separator is above the key, and otherwise move on, descending into this branch's child when the later branches defer.
Insertion is the bottom-up insertion with an overflow at the root grown into a new root.
A monad morphism commutes with insertion.
Pure bottom-up insertion into a subtree.
Pure insertion among the branches 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.
Insertion into a leaf places the entry in its entry list and splits on overflow.
Insertion into a node: search its separators, descending into the first child when the key is below them, then split on overflow.
The search past the last branch defers to the child before it.
The search at a branch: defer when its separator is above the key, and otherwise move on, descending into this branch's child when the later branches defer.
The model equation #
The shape of the result #
The separator convention of the result #
The separator convention between neighbours: each separator is above the keys of the child
before it and at most the keys of its own child. When no child is empty this is the whole
convention at a node (separated_node_of_linked), and insertion changes it at one place.
The separator convention at a node holds between its neighbouring children.
When no child among the branches is empty, the separator convention between neighbouring children, over separated children, is the convention at the node.
The separator convention of a result: a separated tree, or two separated trees with the keys of the left one below the separator and those of the right one at least it.
The split of a leaf with sorted entries has the separator convention.
The split of a separated node without empty children among its branches has the separator convention.
The convention between neighbours looks at a child only through the bound of the next separator on its keys.
Placing a result with the separator convention in the place of a child linked to rest
keeps the convention between neighbours, when the keys of the result are below the next
separator.
Insertion on well-formed trees #
Model equation of insertion on well-formed trees.
Insertion preserves well-formedness.
On a well-formed tree, insertion keeps the height unless the root splits, and then raises it by one.
Insertion keeps the size when the key is present and adds one otherwise.