B+-trees #
B+-trees in the minimum-degree form of [CLRS09], Chapter 18: the variant of the B-trees of
[BMcC72] that keeps every entry in a leaf and uses the interior nodes only to direct
the search ([Com79]). A leaf holds a list of entries. An interior node is a first child
followed by a list of (separator, child) pairs (BPlusTree.Branch), so a node with m
separators has m + 1 children by construction. The tree is a map from keys to values, specified
by toList against the sorted association list model List.IsSortedAssoc.
A separator is a key, not an entry. The keys of the child that a separator introduces are at
least the separator, and the keys of every child before it are strictly below it (Separated).
A separator need not occur as a key in any leaf. The convention with separators bounding their
child strictly from below is not the one used here.
leaves lists the entry lists of the leaves from left to right, and toList is its
concatenation. leaves stands for the chain that links the leaves of a B+-tree: there are no
pointers. height counts the nodes on a root-to-leaf path, so a leaf has height 1; the empty
tree is the empty leaf leaf []. The classic Cslib.DataStructure.BTree differs here, since
its leaf is the empty tree and has height 0.
Well-formedness at minimum degree t is the hypothesis IsWF t tr, never part of the type:
t ≥ 2; every non-root leaf has between t - 1 and 2 * t - 1 entries and every non-root
interior node between t - 1 and 2 * t - 1 separators (Occupied); the root has at most
2 * t - 1 entries or separators, and an interior root at least one separator; all leaves are
at the same depth (Balanced); the separator convention holds at every interior node
(Separated); and toList is key-sorted. width is the number of entries of a leaf or of
separators of an interior node, in which the root rule is stated.
induction is the induction principle for the nested type. The Boolean checkers occupied,
balanced, separated and isWF decide the predicates, so that well-formedness of concrete
trees is checked by decide. The operations and checkers are @[expose]d so that decide and
rfl evaluate them from other modules.
Main definitions #
Cslib.DataStructure.BPlusTree: B+-trees with keys inαand values inβ.BPlusTree.Branch,BPlusTree.Branch.child: a node's(separator, child)pairs.BPlusTree.leaves,BPlusTree.toList,BPlusTree.size,BPlusTree.height,BPlusTree.keys,BPlusTree.map,BPlusTree.foldl: the leaves in order, the entries in order and the basic API.BPlusTree.Occupied,BPlusTree.Balanced,BPlusTree.Separated,BPlusTree.IsWF: non-root occupancy, equal leaf depth, the separator convention and well-formedness at a minimum degree.BPlusTree.occupied,BPlusTree.balanced,BPlusTree.separated,BPlusTree.isWF: Boolean checkers deciding them.
Main results #
BPlusTree.induction: induction over the nested type.BPlusTree.toList_map: mapping values maps the entries.BPlusTree.Separated.lt_of_lt: the separator convention between two positions of a node.BPlusTree.IsWF.of_mem_children: every subtree of a well-formed tree is well-formed.BPlusTree.isWF_iff:isWFdecidesIsWF.
References #
A B+-tree with keys in α and values in β. A leaf holds entries; an interior node is a
first child followed by (separator, child) pairs.
- leaf {α : Type u} {β : Type v} (entries : List (α × β)) : BPlusTree α β
A leaf holding a list of entries.
- node {α : Type u} {β : Type v} (first : BPlusTree α β) (rest : List (α × BPlusTree α β)) : BPlusTree α β
An interior node: a first child and a list of
(separator, child)pairs.
Instances For
A (separator, child) pair of an interior node.
An entry belongs to a tree when it belongs to its toList.
Every node satisfies the occupancy bounds of a non-root node of minimum degree t: a leaf
has between t - 1 and 2 * t - 1 entries, an interior node between t - 1 and 2 * t - 1
separators.
- leaf {α : Type u} {β : Type v} {t : ℕ} {entries : List (α × β)} : t - 1 ≤ entries.length → entries.length ≤ 2 * t - 1 → Occupied t (BPlusTree.leaf entries)
A leaf is occupied when its entry count is in bounds.
- node {α : Type u} {β : Type v} {t : ℕ} {first : BPlusTree α β} {rest : List (Branch α β)} : t - 1 ≤ rest.length →
rest.length ≤ 2 * t - 1 → Occupied t first → (∀ e ∈ rest, Occupied t e.child) → Occupied t (first.node rest)
An interior node is occupied when its separator count is in bounds and all its children are occupied.
Instances For
All leaves at the same depth: every child of a node has the height of the first child.
- leaf {α : Type u} {β : Type v} {entries : List (α × β)} : (BPlusTree.leaf entries).Balanced
A leaf is balanced.
- node {α : Type u} {β : Type v} {first : BPlusTree α β} {rest : List (Branch α β)} : first.Balanced →
(∀ e ∈ rest, e.child.Balanced) → (∀ e ∈ rest, e.child.height = first.height) → (first.node rest).Balanced
A node is balanced when its children are balanced and of equal height.
Instances For
The separator convention at every interior node: the keys of the child a separator introduces are at least the separator, and the keys of every child before it are strictly below it. A separator need not occur as a key.
- leaf {α : Type u} {β : Type v} [LinearOrder α] {entries : List (α × β)} : (BPlusTree.leaf entries).Separated
A leaf has no separators.
- node {α : Type u} {β : Type v} [LinearOrder α] {first : BPlusTree α β} {rest : List (Branch α β)} : first.Separated →
(∀ e ∈ rest, e.child.Separated) →
(∀ e ∈ rest, ∀ p ∈ e.child.toList, e.1 ≤ p.1) →
List.Pairwise (fun (a b : Branch α β) => ∀ p ∈ a.child.toList, p.1 < b.1) rest →
(∀ e ∈ rest, ∀ p ∈ first.toList, p.1 < e.1) → (first.node rest).Separated
At a node: the children are separated, each separator is at most the keys of its child, the keys of each child among the branches are below every later separator, and the keys of the first child are below every separator.
Instances For
Well-formed B+-tree of minimum degree t: root occupancy, non-root occupancy, equal leaf
depth, the separator convention, and key-sortedness of toList.
- two_le : 2 ≤ t
The minimum degree is at least
2. The root has at most
2 * t - 1entries or separators.An interior root has at least one separator.
Every child of the root is occupied.
- balanced : tr.Balanced
All leaves are at the same depth.
- separated : tr.Separated
The separator convention holds at every interior node.
- sorted : tr.toList.IsSortedAssoc
The entries are sorted by strictly increasing key.
Instances For
Boolean check that the keys of each child in a list of branches are below every later separator.
Boolean check of Separated.
Boolean check that every child in a list of branches is separated.
Boolean check of IsWF t.
Equation lemmas #
An empty list of branches has no leaves.
The leaves of a list of branches are those of its children, in order.
An empty list of branches contributes no height.
Induction #
Induction on B+-trees, with a hypothesis for the first child and for every child among the branches. This replaces the raw nested recursor in proofs.
Basic API #
Inversion #
A separator is at most every key of the child it introduces.
The keys of each child among the branches are below every later separator.
The keys of the first child are below every separator.
The separator convention between two positions of a node: the keys of the child at an earlier position are below the separator at a later one.
Every child of a well-formed tree has sorted entries.
An occupied, balanced, separated tree with sorted entries is well-formed when 2 ≤ t: the
occupancy of a non-root node implies the root rule.
Every subtree of a well-formed tree is well-formed: non-root occupancy implies the root rule
when 2 ≤ t.
Decidability #
occupiedRest checks every child among the branches with occupied.
balancedRest n checks that every child among the branches is balanced and of height n.
belowLater decides that the keys of each child are below every later separator.
separatedRest checks every child among the branches with separated.