Classic B-trees #
The B-trees of [BMcC72], in the minimum-degree form of [CLRS09], Chapter 18. A node
is a first child followed by a list of (key, value, child) triples (BTree.Triple), so a node
with m keys has m + 1 children by construction; leaf is the empty tree. The tree is a map
from keys to values, specified by toList against the sorted association list model
List.IsSortedAssoc. toList traverses any tree in structural in-order (the first child, then
each key-value pair followed by its child); under IsWF (its sorted field) the keys are
strictly increasing, which gives map semantics.
height counts the nodes on a root-to-leaf path: the leaf has height 0 and a node has height
one more than its tallest child. Well-formedness at minimum degree t is the hypothesis
IsWF t tr, never part of the type: t ≥ 2, every non-root node has between t - 1 and
2 * t - 1 keys (Occupied), a non-leaf root has between 1 and 2 * t - 1 keys, all leaves
are at the same depth (Balanced), and toList is key-sorted. Taking t = 2 gives 2-3-4 trees.
induction is the induction principle for the nested type; proofs use it rather than the raw
nested recursor. The Boolean checkers occupied, balanced 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.BTree: classic B-trees with keys inαand values inβ.BTree.Triple,BTree.Triple.val,BTree.Triple.child: a node's(key, value, child)triples and their named components.BTree.toList,BTree.size,BTree.height,BTree.keys,BTree.foldl: entries in order and the basic API.BTree.bimap: map over keys and values, keeping shape;BTree.mapandBTree.mapKeysare its value-only and key-only cases.BTree.Occupied,BTree.Balanced,BTree.IsWF: non-root occupancy, equal leaf depth and well-formedness at a minimum degree.BTree.occupied,BTree.balanced,BTree.isWF: Boolean checkers deciding them.
Main results #
BTree.induction,BTree.induction_children,BTree.induction_isSortedAssoc: induction over the nested type, with one hypothesis for all children of a node, and over trees with sorted entries split at a node's first separator.BTree.toList_bimap: mapping keys and values maps the entries;BTree.toList_mapandBTree.toList_mapKeysare its cases.BTree.isWF_map: mapping values keeps well-formedness.BTree.IsWF.of_mem_children: every subtree of a well-formed tree is well-formed.BTree.isWF_iff:isWFdecidesIsWF.
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/Basic.lean.
A classic B-tree. A node is a first child followed by (key, value, child) triples, so a
node with m keys has m + 1 children by construction. leaf is the empty tree.
- leaf {α : Type u} {β : Type v} : BTree α β
The empty tree.
- node {α : Type u} {β : Type v} (first : BTree α β) (rest : List (α × β × BTree α β)) : BTree α β
A node: its first child, then the keys, values and remaining children in order.
Instances For
A (key, value, child) triple of a node: a key, its value, and the child holding the keys
between this key and the next. The key is the first component e.1; Triple.val and
Triple.child name the other two.
The value of a triple.
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: between
t - 1 and 2 * t - 1 keys.
- leaf {α : Type u} {β : Type v} {t : ℕ} : Occupied t BTree.leaf
The empty tree is occupied.
- node {α : Type u} {β : Type v} {t : ℕ} {first : BTree α β} {rest : List (Triple α β)} : t - 1 ≤ rest.length →
rest.length ≤ 2 * t - 1 → Occupied t first → (∀ e ∈ rest, Occupied t e.child) → Occupied t (first.node rest)
A node is occupied when its key 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} : BTree.leaf.Balanced
The empty tree is balanced.
- node {α : Type u} {β : Type v} {first : BTree α β} {rest : List (Triple α β)} : 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
Well-formed B-tree of minimum degree t: root occupancy, non-root occupancy, equal leaf
depth, and key-sortedness of toList.
- two_le : 2 ≤ t
The minimum degree is at least
2. The root has at most
2 * t - 1keys.A non-empty root has at least one key.
Every child of the root is occupied.
- balanced : tr.Balanced
All leaves are at the same depth.
- sorted : tr.toList.IsSortedAssoc
The entries are sorted by strictly increasing key.
Instances For
Boolean check of IsWF t.
Equation lemmas #
An empty list of triples has no entries.
The entries of a list of triples: each key-value pair followed by its child's entries.
A list of entries, each over an empty child, has those entries. Not @[simp]: the simp
normal form of toListRest is the flatMap of toListRest_eq_flatMap.
An empty list of triples contributes height 0.
The height contribution of a list of triples is the maximum of its children's heights plus
one, or 0 for the empty list.
The first separator splits a node's entries into those of its first child, the separator, and those of the node without its first child and separator.
Membership in a node with keys: in its first child, equal to its first separator, or in the node without its first child and separator.
Induction #
Induction on B-trees, with a hypothesis for the first child and for every child among the triples. This replaces the raw nested recursor in proofs.
Induction on B-trees with one hypothesis for all children of a node.
Basic API #
The entries of a concatenation of triple lists are the concatenated entries.
Inversion lemmas #
A node whose children are balanced and as tall as its first child is balanced, and one taller than its first child.
Splitting a node with occupied children at its first separator: the first child is occupied, and so are the children of the first child and of the node without its first child and separator.
Every child of a well-formed tree has sorted entries.
An occupied, balanced tree with sorted entries is well-formed when 2 ≤ t: the occupancy of a
non-root node implies the root rule.
A node's entries are sorted exactly when those of its first child and of the node without its first child and separator are, with the first separator strictly between them.
Induction on a B-tree with sorted entries, splitting a node with keys at its first separator
into first and node c rest.
Decidability #
occupiedRest checks every child among the triples with occupied.
balancedRest n checks that every child among the triples is balanced and of height n.