Documentation

CslibFrontier.DataStructure.BTree.Erase.MinMax

Removing the least and greatest entries of classic B-trees #

eraseMinM and eraseMaxM remove and return the least and greatest entries of a B-tree: they find the extreme entry with minM or maxM and erase its key with eraseM, over an arbitrary monad with a comparator and a node-visit action. The pure operations are the Id instantiations with leI and visit := pure (). Costs are not stated here.

Main definitions #

Main results #

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

def Cslib.DataStructure.BTree.eraseMinM {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (tr : BTree α β) :
m (Option ((α × β) × BTree α β))

Remove and return the least entry: find it with minM, then erase its key.

theorem Cslib.DataStructure.BTree.eraseMinM_def {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (tr : BTree α β) :
eraseMinM t le visit tr = do let __do_lift ← minM visit tr match __do_lift with | none => pure none | some p => do let __do_lift ← eraseM t le visit p.1 tr pure (some (p, __do_lift))

Removing the least entry finds it with minM, then erases its key.

def Cslib.DataStructure.BTree.eraseMaxM {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (tr : BTree α β) :
m (Option ((α × β) × BTree α β))

Remove and return the greatest entry: find it with maxM, then erase its key.

theorem Cslib.DataStructure.BTree.eraseMaxM_def {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (tr : BTree α β) :
eraseMaxM t le visit tr = do let __do_lift ← maxM visit tr match __do_lift with | none => pure none | some p => do let __do_lift ← eraseM t le visit p.1 tr pure (some (p, __do_lift))

Removing the greatest entry finds it with maxM, then erases its key.

@[simp]
theorem Cslib.DataStructure.BTree.eraseMinM_leaf {α β : Type} {m : Type → Type w} [Monad m] [LawfulMonad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) :
eraseMinM t le visit leaf = pure none

The empty tree has no least entry to remove.

@[simp]
theorem Cslib.DataStructure.BTree.eraseMaxM_leaf {α β : Type} {m : Type → Type w} [Monad m] [LawfulMonad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) :
eraseMaxM t le visit leaf = pure none

The empty tree has no greatest entry to remove.

theorem Cslib.IsMonadHom.map_eraseMinM {α β : 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) (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (tr : DataStructure.BTree α β) :
f (DataStructure.BTree.eraseMinM t le visit tr) = DataStructure.BTree.eraseMinM t (fun (a b : α) => f (le a b)) (f visit) tr

A monad morphism commutes with the removal of the least entry.

theorem Cslib.IsMonadHom.map_eraseMaxM {α β : 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) (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (tr : DataStructure.BTree α β) :
f (DataStructure.BTree.eraseMaxM t le visit tr) = DataStructure.BTree.eraseMaxM t (fun (a b : α) => f (le a b)) (f visit) tr

A monad morphism commutes with the removal of the greatest entry.

def Cslib.DataStructure.BTree.eraseMin? {α β : Type} [LinearOrder α] (t : ℕ) (tr : BTree α β) :
Option ((α × β) × BTree α β)

Remove and return the least entry, pure.

def Cslib.DataStructure.BTree.eraseMax? {α β : Type} [LinearOrder α] (t : ℕ) (tr : BTree α β) :
Option ((α × β) × BTree α β)

Remove and return the greatest entry, pure.

eraseMin? is the Id instantiation of eraseMinM.

eraseMax? is the Id instantiation of eraseMaxM.

theorem Cslib.DataStructure.BTree.eraseMin?_eq_map_min? {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} :
eraseMin? t tr = Option.map (fun (p : α × β) => (p, erase t p.1 tr)) tr.min?

eraseMin? is the minimum together with the erasure of its key.

theorem Cslib.DataStructure.BTree.eraseMax?_eq_map_max? {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} :
eraseMax? t tr = Option.map (fun (p : α × β) => (p, erase t p.1 tr)) tr.max?

eraseMax? is the maximum together with the erasure of its key.

@[simp]

The empty tree has no least entry to remove.

@[simp]

The empty tree has no greatest entry to remove.

theorem Cslib.DataStructure.BTree.eraseMin?_eq {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} :
eraseMin? t tr = Option.map (fun (p : α × β) => (p, erase t p.1 tr)) tr.toList.head?

Model equation of eraseMin?: the first entry and the tree with its key erased.

theorem Cslib.DataStructure.BTree.eraseMax?_eq {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} :
eraseMax? t tr = Option.map (fun (p : α × β) => (p, erase t p.1 tr)) tr.toList.getLast?

Model equation of eraseMax?: the last entry and the tree with its key erased.

theorem Cslib.DataStructure.BTree.eraseMin?_eq_some_iff {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} {p : α × β} {tr' : BTree α β} :
eraseMin? t tr = some (p, tr') ↔ tr.toList.head? = some p ∧ tr' = erase t p.1 tr

eraseMin? returns p and tr' exactly when p is the first entry and tr' the tree with its key erased.

theorem Cslib.DataStructure.BTree.eraseMax?_eq_some_iff {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} {p : α × β} {tr' : BTree α β} :
eraseMax? t tr = some (p, tr') ↔ tr.toList.getLast? = some p ∧ tr' = erase t p.1 tr

eraseMax? returns p and tr' exactly when p is the last entry and tr' the tree with its key erased.

theorem Cslib.DataStructure.BTree.toList_of_eraseMin?_eq_some_of_isSortedAssoc {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : tr.toList.IsSortedAssoc) {p : α × β} {tr' : BTree α β} (he : eraseMin? t tr = some (p, tr')) :
tr.toList = p :: tr'.toList

On a tree with sorted entries, eraseMin? splits the entries into the first and the rest.

theorem Cslib.DataStructure.BTree.toList_of_eraseMax?_eq_some_of_isSortedAssoc {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : tr.toList.IsSortedAssoc) {p : α × β} {tr' : BTree α β} (he : eraseMax? t tr = some (p, tr')) :
tr.toList = tr'.toList ++ [p]

On a tree with sorted entries, eraseMax? splits the entries into the rest and the last.

theorem Cslib.DataStructure.BTree.toList_of_eraseMin?_eq_some {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) {p : α × β} {tr' : BTree α β} (he : eraseMin? t tr = some (p, tr')) :
tr.toList = p :: tr'.toList ∧ IsWF t tr'

On a well-formed tree, eraseMin? splits the entries into the first and the rest, and the rest is well-formed.

theorem Cslib.DataStructure.BTree.toList_of_eraseMax?_eq_some {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) {p : α × β} {tr' : BTree α β} (he : eraseMax? t tr = some (p, tr')) :
tr.toList = tr'.toList ++ [p] ∧ IsWF t tr'

On a well-formed tree, eraseMax? splits the entries into the rest and the last, and the rest is well-formed.