Documentation

CslibFrontier.DataStructure.BTree.MinMax

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 #

References #

Ported from cslib-community/EpsilonEridani at commit 1966ab476: EpsilonEridani/Algorithms/Lean/BTree/MinMax.lean.

def Cslib.DataStructure.BTree.minM {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) :
BTree α β → m (Option (α × β))

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.

@[irreducible]
def Cslib.DataStructure.BTree.maxM {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) :
BTree α β → m (Option (α × β))

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.

theorem Cslib.DataStructure.BTree.minM_leaf {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) :

The empty tree has no minimum entry and is not visited.

theorem Cslib.DataStructure.BTree.minM_node {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) (first : BTree α β) (rest : List (Triple α β)) :
minM visit (first.node rest) = do visit let __do_lift ← minM visit first match __do_lift with | some p => pure (some p) | none => pure (Option.map (fun (e : Triple α β) => (e.1, e.val)) rest.head?)

At a node, visit fires before the first child's minimum, which takes priority over the node's first entry.

theorem Cslib.DataStructure.BTree.maxM_leaf {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) :

The empty tree has no maximum entry and is not visited.

theorem Cslib.DataStructure.BTree.maxM_node_nil {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) (first : BTree α β) :
maxM visit (first.node []) = do visit maxM visit first

A node without keys is visited, then the maximum of its only child is returned.

theorem Cslib.DataStructure.BTree.maxM_node_singleton {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) (first c : BTree α β) (k : α) (v : β) :
maxM visit (first.node [(k, v, c)]) = do visit let __do_lift ← maxM visit c match __do_lift with | some p => pure (some p) | none => pure (some (k, v))

At the last triple, the node is visited, then the last child's maximum takes priority over the last entry.

theorem Cslib.DataStructure.BTree.maxM_node_cons_cons {α β : Type} {m : Type → Type w} [Monad m] (visit : m Unit) (first c : BTree α β) (k : α) (v : β) (e : Triple α β) (rest : List (Triple α β)) :
maxM visit (first.node ((k, v, c) :: e :: rest)) = maxM visit (c.node (e :: rest))

Walking past a triple is free: the maximum continues on the remaining suffix of the node, with no visit.

theorem Cslib.IsMonadHom.map_minM {α β : Type} {m : Type → Type u_1} {n : Type → Type u_2} [Monad m] [Monad n] {f : {γ : Type} → m γ → n γ} (hf : IsMonadHom m n fun {α : Type} => f) (visit : m Unit) (tr : DataStructure.BTree α β) :

A monad morphism commutes with the minimum search.

theorem Cslib.IsMonadHom.map_maxM {α β : Type} {m : Type → Type u_1} {n : Type → Type u_2} [Monad m] [Monad n] {f : {γ : Type} → m γ → n γ} (hf : IsMonadHom m n fun {α : Type} => f) (visit : m Unit) (tr : DataStructure.BTree α β) :

A monad morphism commutes with the maximum search.

def Cslib.DataStructure.BTree.min? {α β : Type} (tr : BTree α β) :
Option (α × β)

Pure minimum query: the Id instantiation of minM with a trivial visit action.

def Cslib.DataStructure.BTree.max? {α β : Type} (tr : BTree α β) :
Option (α × β)

Pure maximum query: the Id instantiation of maxM with a trivial visit action.

theorem Cslib.DataStructure.BTree.min?_def {α β : Type} {tr : BTree α β} :
tr.min? = (minM (pure ()) tr).run

min? is the Id instantiation of minM.

theorem Cslib.DataStructure.BTree.max?_def {α β : Type} {tr : BTree α β} :
tr.max? = (maxM (pure ()) tr).run

max? is the Id instantiation of maxM.

@[simp]

The empty tree has no minimum entry.

@[simp]

The empty tree has no maximum entry.

@[simp]
theorem Cslib.DataStructure.BTree.min?_node {α β : Type} (first : BTree α β) (rest : List (Triple α β)) :
(first.node rest).min? = first.min?.or (Option.map (fun (e : Triple α β) => (e.1, e.val)) rest.head?)

The first child's minimum takes priority over the node's first entry.

@[simp]
theorem Cslib.DataStructure.BTree.max?_node_nil {α β : Type} (first : BTree α β) :
(first.node []).max? = first.max?

A node without keys has the maximum of its only child.

@[simp]
theorem Cslib.DataStructure.BTree.max?_node_singleton {α β : Type} (first c : BTree α β) (k : α) (v : β) :
(first.node [(k, v, c)]).max? = c.max?.or (some (k, v))

At the last triple, the last child's maximum takes priority over the last entry.

@[simp]
theorem Cslib.DataStructure.BTree.max?_node_cons_cons {α β : Type} (first c : BTree α β) (k : α) (v : β) (e : Triple α β) (rest : List (Triple α β)) :
(first.node ((k, v, c) :: e :: rest)).max? = (c.node (e :: rest)).max?

Scanning past a triple leaves the maximum query on the remaining suffix of the node.

theorem Cslib.DataStructure.BTree.min?_eq {α β : Type} {tr : BTree α β} :

Model equation: the minimum query returns the first entry of toList, on any tree.

theorem Cslib.DataStructure.BTree.max?_eq {α β : Type} {tr : BTree α β} :

Model equation: the maximum query returns the last entry of toList, on any tree.

@[simp]
theorem Cslib.DataStructure.BTree.min?_eq_none_iff {α β : Type} {tr : BTree α β} :
tr.min? = none ↔ tr.size = 0

The minimum query fails exactly when there are no entries.

@[simp]
theorem Cslib.DataStructure.BTree.max?_eq_none_iff {α β : Type} {tr : BTree α β} :
tr.max? = none ↔ tr.size = 0

The maximum query fails exactly when there are no entries.

theorem Cslib.DataStructure.BTree.min?_eq_some_iff_of_isSortedAssoc {α β : Type} {tr : BTree α β} [LinearOrder α] {p : α × β} (h : tr.toList.IsSortedAssoc) :
tr.min? = some p ↔ p ∈ tr ∧ ∀ q ∈ tr, p.1 ≤ q.1

On a tree with sorted entries, min? returns exactly the entry of least key.

theorem Cslib.DataStructure.BTree.max?_eq_some_iff_of_isSortedAssoc {α β : Type} {tr : BTree α β} [LinearOrder α] {p : α × β} (h : tr.toList.IsSortedAssoc) :
tr.max? = some p ↔ p ∈ tr ∧ ∀ q ∈ tr, q.1 ≤ p.1

On a tree with sorted entries, max? returns exactly the entry of greatest key.

theorem Cslib.DataStructure.BTree.min?_eq_some_iff {α β : Type} {tr : BTree α β} [LinearOrder α] {t : ℕ} {p : α × β} (h : IsWF t tr) :
tr.min? = some p ↔ p ∈ tr ∧ ∀ q ∈ tr, p.1 ≤ q.1

In a well-formed tree, min? returns exactly the entry of least key.

theorem Cslib.DataStructure.BTree.max?_eq_some_iff {α β : Type} {tr : BTree α β} [LinearOrder α] {t : ℕ} {p : α × β} (h : IsWF t tr) :
tr.max? = some p ↔ p ∈ tr ∧ ∀ q ∈ tr, q.1 ≤ p.1

In a well-formed tree, max? returns exactly the entry of greatest key.