Bulk loading of classic B-trees #
Bottom-up bulk loading of the B-trees of [BMcC72]: a tree is built from a list of entries in one pass, level by level from the bottom. The shape invariants are those of [CLRS09], Chapter 18.
A level is a first tree followed by (key, value, tree) triples, all of its trees of one height;
the bottom level is the empty leaf and one triple per entry, each with an empty child. A round
cuts the triples of a level into groups of t - 1 to 2 * t - 1 triples with one triple between
consecutive groups: each group becomes a node one level taller, and each triple between groups
moves up, its child becoming the first child of the next node. groupSizes chooses the sizes of
all groups but the last, with one look ahead: a group of 2 * t - 1 unless that would leave
fewer than t - 1 triples for the rest, then one of t - 1. packM cuts a level by a list of
sizes, and buildM runs rounds until at most 2 * t - 1 triples are left, which become the
root. Below 2 the loader uses minimum degree 2, so that every node built holds a key.
Each operation is defined once over an arbitrary monad m, with a node-visit action
visit : m Unit that fires once per node built; no comparator is needed, since the input order
is kept. The pure operations are the Id instantiations with visit := pure (). All recursion is
structural, on a fuel argument or on the list of sizes, so the definitions are @[expose]d and
decide evaluates bulk loading on concrete inputs; when the fuel runs out, the remaining level
becomes a single node, which keeps the entries. Costs are not stated here.
Main definitions #
groupSizes: the sizes of the groups a round cuts.packM,buildM,ofSortedListM,ofSortedList: one round, all rounds, and bulk loading.IsGrouping: the shape of the group sizes.
Main results #
Cslib.IsMonadHom.map_ofSortedListM: a monad morphism commutes with bulk loading.toList_ofSortedList: bulk loading keeps the entries in order, for every list and everyt.height_ofSortedList_le: for2 ≤ t, the height bound ofIsWF.height_le_log, for every list.isWF_ofSortedList: on a sorted list, the result is well-formed.exists_isWF_toList_map_snd_eq: over an infinite key type, every list of values is the value list of a well-formed tree of that many entries with distinct keys.lookup_ofSortedList: on a sorted list, lookup isList.lookup.
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
Ported from cslib-community/EpsilonEridani at commit 1966ab476:
EpsilonEridani/Algorithms/Lean/BTree/OfSortedList.lean.
The sizes of all groups but the last when the r triples of a level are cut into groups of
t - 1 to 2 * t - 1 triples with one triple between groups, with recursion bounded by fuel.
A group has 2 * t - 1 triples unless that would leave fewer than t - 1, then t - 1.
Out of fuel, no group is cut.
At most 2 * t - 1 triples form the last group, and no group is cut.
More than 2 * t - 1 triples: cut a group of t - 1 when one of 2 * t - 1 would leave
fewer than t - 1 triples, and otherwise one of 2 * t - 1.
Cut a level by a list of group sizes: the node over the first group, and the next level's
triples, each a key moved up with the node over the following group. The last group takes the
remaining triples. visit fires once per node built.
Rounds of bulk loading at minimum degree t, at most fuel of them: a level with no
triples is its first tree, a level with at most 2 * t - 1 triples becomes the root, and a
longer one is cut by groupSizes into the next level. Out of fuel, the level becomes one
node.
A round of bulk loading on a level with triples.
A monad morphism commutes with cutting a level.
A monad morphism commutes with the rounds of bulk loading.
ofSortedList is the Id instantiation of ofSortedListM.
A group of s triples becomes a node, and the triple after it moves up over the rest.
Bulk loading the empty list builds the empty tree.
Entries #
Bulk loading keeps the entries in order, for every list and every t.
Bulk loading keeps the number of entries.
An entry belongs to a bulk-loaded tree exactly when it belongs to the list. Not @[simp]:
simp proves it from mem_def and toList_ofSortedList.
Group sizes #
IsGrouping t r ss: the r triples of a level are cut into groups of t - 1 to
2 * t - 1 triples with one triple between consecutive groups, the groups but the last having
the sizes ss.
groupSizes cuts r triples, at least t - 1 of them, into a grouping, given fuel at
least r.
At minimum degree at least 2, a grouping has fewer than half as many sizes as triples: every
group but the last takes at least one triple and the one after it, and the last at least one.
Shape #
At a minimum degree of at least 2, the children of the root of a bulk-loaded tree are
occupied, for every list.
Bulk loading a sorted list builds a well-formed tree at a minimum degree of at least 2.
Every list of values vs is the value list of a well-formed B-tree of minimum degree t ≥ 2
with vs.length entries and distinct keys, over an infinite linearly ordered key type: bulk load
vs under any vs.length keys in increasing order.
Lookup in a bulk-loaded sorted list is lookup in the list.