Minimum and maximum in classic B-trees #
The extremal-entry queries follow the leftmost or rightmost child, as in [CLRS09], Chapter 18.
Each is defined once over an arbitrary monad, with a node-visit action, then instantiated at Id
for the pure query. No comparator or order instance is needed: the queries use the tree's
structural order. A visit is charged once for each node on the descent, leaves excluded and nodes
without keys included; scanning a node's triples for its last child charges no further visits.
The model equations hold on any tree, without sortedness or occupancy, and the queries return
none exactly when the tree has no entries, including malformed trees whose nodes have no keys.
Main definitions #
Main results #
Cslib.IsMonadHom.map_minM,Cslib.IsMonadHom.map_maxM: a monad morphism commutes with the queries.min?_eq,max?_eq: the queries arehead?andgetLast?oftoList, on any tree.min?_eq_some_iff,max?_eq_some_iff: on a well-formed tree, the least and greatest entries by key.
References #
Ported from cslib-community/EpsilonEridani at commit 1966ab476:
EpsilonEridani/Algorithms/Lean/BTree/MinMax.lean.
First entry in structural order: descend through first children, falling back to the
node's first entry when its first child has no entries. visit fires once per node, before
descending into its first child, and no comparisons are needed.
Greatest entry, the last in structural order: walk along each node's triples, which is free,
and descend into the last child, falling back to the last entry when that child has no entries.
visit fires once per node: when the walk reaches the node's last triple, or, at a node without
keys, before descending into its only child.
Walking past a triple is free: the maximum continues on the remaining suffix of the node, with no visit.
A monad morphism commutes with the minimum search.
A monad morphism commutes with the maximum search.