Documentation

CslibFrontier.DataStructure.BTree.Erase.Basic

Erasure from classic B-trees #

Erasure in the B-trees of Cslib.DataStructure.BTree, by the bottom-up algorithm of the functional literature, the mirror image of Cslib.DataStructure.BTree.Insert. The repair follows the functional deletion of [Mun21]: mergeSplit merges two nodes and splits the result with the insertion splitter, as its node⇩i does, and rebalanceRightM and rebalanceLastM are its rebalance_middle_tree and rebalance_last_tree. [Mun21] replaces an interior key by its predecessor; this version uses the successor, which sits in the same triple of the interleaved representation.

Erasure from a subtree may leave it one key short (underflows); the parent repairs such a child against an adjacent sibling by merging the two around their separator and splitting the result again when it has more than 2 * t - 1 keys (mergeSplit, through InsertResult.ofNode). That single repair covers both borrowing and merging in [CLRS09], Chapter 18. A child is repaired against its right sibling, and the last child against its left one. An empty root collapses into its only child (collapse), so the height falls only there. A key found in an interior node is replaced by its successor, the least entry of the child after it, which popMinM removes with the same repairs; erasure is not restricted to leaves.

Each operation is defined once over an arbitrary monad m, with a comparator le : α → α → m Bool and a node-visit action visit : m Unit. eraseSubtreeM and eraseRestM are the recursion of eraseM, with the scan and the equality test of lookupM. visit fires on entering each node of the search path, including the path of popMinM to the successor, and once more for each sibling a repair inspects, so a descent visits at most two nodes per level. The pure operations are the Id instantiations with leI and visit := pure (). eraseRestM takes the child before the triples together with its erasure as a delayed computation, a function from Unit: the recursion is then structural, so the definitions are @[expose]d and decide evaluates erasure on concrete trees from other modules, the monadic constructor equations hold by rfl, and the erasure of a child is run only where the search descends, in Id as in any other monad. Costs are not stated here.

Main definitions #

Main results #

References #

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

def Cslib.DataStructure.BTree.underflows {α β : Type} (t : ℕ) :
BTree α β → Bool

A non-empty node with fewer than t - 1 keys: a child that erasure left one key short.

def Cslib.DataStructure.BTree.mergeSplit {α β : Type} (t : ℕ) :
BTree α β → α → β → BTree α β → InsertResult α β

Merge two adjacent siblings around their separator into one node, split again by InsertResult.ofNode when it has more than 2 * t - 1 keys.

def Cslib.DataStructure.BTree.collapse {α β : Type} :
BTree α β → BTree α β

Drop an empty root: a node without keys is replaced by its only child.

def Cslib.DataStructure.BTree.rebalanceRightM {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) (c : BTree α β) (k : α) (v : β) (s : BTree α β) (rest : List (Triple α β)) :
m (BTree α β × List (Triple α β))

Repair a child c followed by its right sibling s with separator (k, v) and the triples rest: if c underflows, inspect s (one visit) and merge them.

def Cslib.DataStructure.BTree.rebalanceLastM {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) (prev : BTree α β) (k : α) (v : β) (c : BTree α β) :
m (BTree α β × List (Triple α β))

Repair a last child c after its left sibling prev with separator (k, v): if c underflows, inspect prev (one visit) and merge them.

theorem Cslib.DataStructure.BTree.rebalanceRightM_def {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) (c : BTree α β) (k : α) (v : β) (s : BTree α β) (rest : List (Triple α β)) :
rebalanceRightM t visit c k v s rest = if underflows t c = true then do visit pure ((mergeSplit t c k v s).withRest rest) else pure (c, (k, v, s) :: rest)

Repair against the right sibling visits and merges exactly when the child underflows.

theorem Cslib.DataStructure.BTree.rebalanceLastM_def {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) (prev : BTree α β) (k : α) (v : β) (c : BTree α β) :
rebalanceLastM t visit prev k v c = if underflows t c = true then do visit pure ((mergeSplit t prev k v c).withRest []) else pure (prev, [(k, v, c)])

Repair against the left sibling visits and merges exactly when the last child underflows.

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

Remove the least entry of a subtree, repairing on the way back up; the result may underflow by one key.

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

Erasure from a subtree; the result may underflow by one key.

def Cslib.DataStructure.BTree.eraseRestM {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (prev : BTree α β) (prevErased : Unit → m (BTree α β)) :
List (Triple α β) → m (BTree α β × List (Triple α β))

Erasure among the triples after the child prev, whose erasure is prevErased ().

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

Remove the entry with key k, if any, from a B-tree of minimum degree t, comparing with le and calling visit on each node of the search path and on each sibling a repair inspects; an emptied root collapses into its only child.

theorem Cslib.DataStructure.BTree.eraseM_def {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (tr : BTree α β) :
eraseM t le visit k tr = do let __do_lift ← eraseSubtreeM t le visit k tr pure __do_lift.collapse

Erasure is erasure from the root subtree followed by collapsing an empty root.

@[simp]
theorem Cslib.DataStructure.BTree.eraseSubtreeM_leaf {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) :

Erasure from the empty tree is the empty tree, without a visit.

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

Erasure from the empty tree is the empty tree, without a visit.

theorem Cslib.DataStructure.BTree.eraseSubtreeM_node {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (first : BTree α β) (rest : List (Triple α β)) :
eraseSubtreeM t le visit k (first.node rest) = do visit let p ← eraseRestM t le visit k first (fun (x : Unit) => eraseSubtreeM t le visit k first) rest pure (p.1.node p.2)

A node is visited once on entry, then its triples are searched.

theorem Cslib.DataStructure.BTree.eraseRestM_nil {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (prev : BTree α β) (prevErased : Unit → m (BTree α β)) :
eraseRestM t le visit k prev prevErased [] = do let __do_lift ← prevErased () pure (__do_lift, [])

Past the last triple, the erasure of the preceding child is the result.

theorem Cslib.DataStructure.BTree.eraseRestM_cons {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (prev : BTree α β) (prevErased : Unit → m (BTree α β)) (k' : α) (v' : β) (c : BTree α β) (rest : List (Triple α β)) :
eraseRestM t le visit k prev prevErased ((k', v', c) :: rest) = do let __do_lift ← le k k' if __do_lift = true then do let __do_lift ← le k' k if __do_lift = true then do let __do_lift ← popMinM t visit c match __do_lift with | none => pure (prev, rest) | some ((k₂, v₂), c') => match rest with | [] => rebalanceLastM t visit prev k₂ v₂ c' | (k₃, v₃, c₃) :: rest₃ => do let p ← rebalanceRightM t visit c' k₃ v₃ c₃ rest₃ pure (prev, (k₂, v₂, p.1) :: p.2) else do let __do_lift ← prevErased () rebalanceRightM t visit __do_lift k' v' c rest else match rest with | [] => do let __do_lift ← eraseSubtreeM t le visit k c rebalanceLastM t visit prev k' v' __do_lift | head :: tail => do let p ← eraseRestM t le visit k c (fun (x : Unit) => eraseSubtreeM t le visit k c) rest pure (prev, (k', v', p.1) :: p.2)

At a triple: on equality, replace the entry by the least entry of its child and repair the child; below the key, erase from the preceding child and repair it against this child; above it, move on, repairing the last child against its left sibling.

@[simp]
theorem Cslib.DataStructure.BTree.popMinM_leaf {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) :

The least entry of the empty tree does not exist, and nothing is visited.

theorem Cslib.DataStructure.BTree.popMinM_node {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) (first : BTree α β) (rest : List (Triple α β)) :
popMinM t visit (first.node rest) = do visit let __do_lift ← popMinM t visit first match __do_lift, rest with | none, [] => pure none | none, (k, v, c) :: rest' => pure (some ((k, v), c.node rest')) | some (e, first'), [] => pure (some (e, first'.node [])) | some (e, first'), (k, v, c) :: rest' => do let p ← rebalanceRightM t visit first' k v c rest' pure (some (e, p.1.node p.2))

A node is visited once on entry, its first child searched, and the result repaired.

theorem Cslib.IsMonadHom.map_rebalanceRightM {α β : 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 : ℕ) (visit : m Unit) (c : DataStructure.BTree α β) (k : α) (v : β) (s : DataStructure.BTree α β) (rest : List (DataStructure.BTree.Triple α β)) :
f (DataStructure.BTree.rebalanceRightM t visit c k v s rest) = DataStructure.BTree.rebalanceRightM t (f visit) c k v s rest

A monad morphism commutes with the repair against the right sibling.

theorem Cslib.IsMonadHom.map_rebalanceLastM {α β : 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 : ℕ) (visit : m Unit) (prev : DataStructure.BTree α β) (k : α) (v : β) (c : DataStructure.BTree α β) :
f (DataStructure.BTree.rebalanceLastM t visit prev k v c) = DataStructure.BTree.rebalanceLastM t (f visit) prev k v c

A monad morphism commutes with the repair against the left sibling.

theorem Cslib.IsMonadHom.map_popMinM {α β : 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 : ℕ) (visit : m Unit) (tr : DataStructure.BTree α β) :

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

theorem Cslib.IsMonadHom.map_eraseSubtreeM {α β : 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) (k : α) (tr : DataStructure.BTree α β) :
f (DataStructure.BTree.eraseSubtreeM t le visit k tr) = DataStructure.BTree.eraseSubtreeM t (fun (a b : α) => f (le a b)) (f visit) k tr

A monad morphism commutes with erasure from a subtree.

theorem Cslib.IsMonadHom.map_eraseRestM {α β : 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) (k : α) (prev : DataStructure.BTree α β) (prevErased : Unit → m (DataStructure.BTree α β)) (rest : List (DataStructure.BTree.Triple α β)) :
f (DataStructure.BTree.eraseRestM t le visit k prev prevErased rest) = DataStructure.BTree.eraseRestM t (fun (a b : α) => f (le a b)) (f visit) k prev (fun (u : Unit) => f (prevErased u)) rest

A monad morphism commutes with erasure among the triples of a node.

theorem Cslib.IsMonadHom.map_eraseM {α β : 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) (k : α) (tr : DataStructure.BTree α β) :
f (DataStructure.BTree.eraseM t le visit k tr) = DataStructure.BTree.eraseM t (fun (a b : α) => f (le a b)) (f visit) k tr

A monad morphism commutes with erasure.

def Cslib.DataStructure.BTree.rebalanceRight {α β : Type} (t : ℕ) (c : BTree α β) (k : α) (v : β) (s : BTree α β) (rest : List (Triple α β)) :
BTree α β × List (Triple α β)

Pure repair of a child against its right sibling.

def Cslib.DataStructure.BTree.rebalanceLast {α β : Type} (t : ℕ) (prev : BTree α β) (k : α) (v : β) (c : BTree α β) :
BTree α β × List (Triple α β)

Pure repair of a last child against its left sibling.

def Cslib.DataStructure.BTree.popMin {α β : Type} (t : ℕ) (tr : BTree α β) :
Option ((α × β) × BTree α β)

Pure removal of the least entry.

theorem Cslib.DataStructure.BTree.rebalanceRight_def {α β : Type} {t : ℕ} (c : BTree α β) (k : α) (v : β) (s : BTree α β) (rest : List (Triple α β)) :
rebalanceRight t c k v s rest = (rebalanceRightM t (pure ()) c k v s rest).run

rebalanceRight is the Id instantiation of rebalanceRightM.

theorem Cslib.DataStructure.BTree.rebalanceLast_def {α β : Type} {t : ℕ} (prev : BTree α β) (k : α) (v : β) (c : BTree α β) :
rebalanceLast t prev k v c = (rebalanceLastM t (pure ()) prev k v c).run

rebalanceLast is the Id instantiation of rebalanceLastM.

theorem Cslib.DataStructure.BTree.popMin_def {α β : Type} {t : ℕ} (tr : BTree α β) :
popMin t tr = (popMinM t (pure ()) tr).run

popMin is the Id instantiation of popMinM.

theorem Cslib.DataStructure.BTree.rebalanceRight_eq_ite {α β : Type} {t : ℕ} (c : BTree α β) (k : α) (v : β) (s : BTree α β) (rest : List (Triple α β)) :
rebalanceRight t c k v s rest = if underflows t c = true then (mergeSplit t c k v s).withRest rest else (c, (k, v, s) :: rest)

Pure repair against the right sibling merges only an underflowing child.

theorem Cslib.DataStructure.BTree.rebalanceLast_eq_ite {α β : Type} {t : ℕ} (prev : BTree α β) (k : α) (v : β) (c : BTree α β) :
rebalanceLast t prev k v c = if underflows t c = true then (mergeSplit t prev k v c).withRest [] else (prev, [(k, v, c)])

Pure repair against the left sibling merges only an underflowing last child.

@[simp]

The empty tree has no least entry.

theorem Cslib.DataStructure.BTree.popMin_node {α β : Type} {t : ℕ} (first : BTree α β) (rest : List (Triple α β)) :
popMin t (first.node rest) = match popMin t first, rest with | none, [] => none | none, (k, v, c) :: rest' => some ((k, v), c.node rest') | some (e, first'), [] => some (e, first'.node []) | some (e, first'), (k, v, c) :: rest' => have p := rebalanceRight t first' k v c rest'; some (e, p.1.node p.2)

Removing the least entry from a node: from its first child, repaired, or the node's first entry when the first child has none.

@[simp]
theorem Cslib.DataStructure.BTree.toList_mergeSplit {α β : Type} {t : ℕ} (l : BTree α β) (k : α) (v : β) (r : BTree α β) :
(mergeSplit t l k v r).toList = l.toList ++ (k, v) :: r.toList

Merging keeps the entries of both siblings and their separator.

theorem Cslib.DataStructure.BTree.toList_rebalanceRight {α β : Type} {t : ℕ} (c : BTree α β) (k : α) (v : β) (s : BTree α β) (rest : List (Triple α β)) :
(rebalanceRight t c k v s rest).1.toList ++ toListRest (rebalanceRight t c k v s rest).2 = c.toList ++ toListRest ((k, v, s) :: rest)

Repair against the right sibling keeps the entries.

theorem Cslib.DataStructure.BTree.toList_rebalanceLast {α β : Type} {t : ℕ} (prev : BTree α β) (k : α) (v : β) (c : BTree α β) :
(rebalanceLast t prev k v c).1.toList ++ toListRest (rebalanceLast t prev k v c).2 = prev.toList ++ toListRest [(k, v, c)]

Repair against the left sibling keeps the entries.

theorem Cslib.DataStructure.BTree.toList_popMin {α β : Type} {t : ℕ} {tr : BTree α β} :
(popMin t tr = none → tr.toList = []) ∧ ∀ (e : α × β) (tr' : BTree α β), popMin t tr = some (e, tr') → tr.toList = e :: tr'.toList

Removing the least entry: none exactly on a tree without entries, and otherwise the first entry followed by the entries of the rest.

@[simp]

Collapsing an empty root keeps the entries.

Shape #

structure Cslib.DataStructure.BTree.Underfull {α β : Type} (t h : ℕ) (c : BTree α β) :

A subtree of height h that may be one key short: balanced, with occupied children, at most 2 * t - 1 keys, and at least t - 2 keys when it is a node.

  • balanced : c.Balanced

    The subtree is balanced.

  • height_eq : c.height = h

    The subtree has height h.

  • children_occupied (x : BTree α β) : x ∈ c.children → Occupied t x

    Every child of the root is occupied.

  • rootKeys_le : c.rootKeys ≤ 2 * t - 1

    The root has at most 2 * t - 1 keys.

  • le_rootKeys : c ≠ leaf → t - 2 ≤ c.rootKeys

    A node root has at least t - 2 keys.

structure Cslib.DataStructure.BTree.EraseFits {α β : Type} (t : ℕ) (tr c : BTree α β) :

The shape of the result c of erasing from tr: balanced, of the same height, with occupied children, and at most one root key fewer.

  • balanced : c.Balanced

    The result is balanced.

  • height_eq : c.height = tr.height

    The result has the height of tr.

  • children_occupied (x : BTree α β) : x ∈ c.children → Occupied t x

    Every child of the result's root is occupied.

  • rootKeys_le : c.rootKeys ≤ tr.rootKeys

    The result's root has no more keys than that of tr.

  • rootKeys_le_succ : tr.rootKeys ≤ c.rootKeys + 1

    The result's root has at most one key fewer than that of tr.

A repaired child, or a repaired pair of children around a separator, all good.

theorem Cslib.DataStructure.BTree.Good.underfull {α β : Type} {t : ℕ} {c : BTree α β} {h : ℕ} (hc : Good t h c) :
Underfull t h c

A good subtree is at most one key short.

theorem Cslib.DataStructure.BTree.Good.ne_leaf {α β : Type} {t : ℕ} {c : BTree α β} {h : ℕ} (hc : Good t h c) (hh : 1 ≤ h) :

A good tree of positive height is a node.

theorem Cslib.DataStructure.BTree.Underfull.occupied {α β : Type} {t : ℕ} {c : BTree α β} {h : ℕ} (hc : Underfull t h c) (hu : underflows t c = false) :

A subtree that does not underflow is occupied.

theorem Cslib.DataStructure.BTree.Underfull.good {α β : Type} {t : ℕ} {c : BTree α β} {h : ℕ} (hc : Underfull t h c) (hu : underflows t c = false) :
Good t h c

A subtree that does not underflow is good.

theorem Cslib.DataStructure.BTree.ne_leaf_of_underflows {α β : Type} {t : ℕ} {c : BTree α β} (hu : underflows t c = true) :

Only a node underflows.

theorem Cslib.DataStructure.BTree.Underfull.rootKeys_eq_of_underflows {α β : Type} {t : ℕ} {c : BTree α β} {h : ℕ} (hc : Underfull t h c) (hu : underflows t c = true) :
c.rootKeys = t - 2

A subtree one key short that underflows has exactly t - 2 keys.

theorem Cslib.DataStructure.BTree.Underfull.one_le_height_of_underflows {α β : Type} {t : ℕ} {c : BTree α β} {h : ℕ} (hc : Underfull t h c) (hu : underflows t c = true) :
1 ≤ h

A subtree that underflows has positive height.

theorem Cslib.DataStructure.BTree.EraseFits.underfull {α β : Type} {t : ℕ} {tr c : BTree α β} (hf : EraseFits t tr c) (ho : Occupied t tr) :

Erasure from an occupied subtree leaves it at most one key short.

theorem Cslib.DataStructure.BTree.Good.underfull_of_eraseFits {α β : Type} {t : ℕ} (ht : 2 ≤ t) {c c' : BTree α β} {h : ℕ} (hc : Good t h c) (hf : (∀ x ∈ c.children, Occupied t x) → (c ≠ leaf → 1 ≤ c.rootKeys) → EraseFits t c c') :
Underfull t h c'

Erasure from a good subtree leaves it at most one key short, given the shape of the erasure under the occupancy facts a good subtree provides.

theorem Cslib.DataStructure.BTree.InsertResult.mended_ofNode {α β : Type} {t : ℕ} {first : BTree α β} {rest : List (Triple α β)} {h : ℕ} (hf : Good t h first) (hr : ∀ e ∈ rest, Good t h e.child) (hlo : t - 1 ≤ rest.length) (hhi : rest.length ≤ 2 * t - 1 + t + 1) :
Mended t (h + 1) (ofNode t first rest)

Re-splitting a node of between t - 1 and 2 * t - 1 + t + 1 keys (3 * t when 1 ≤ t) over good children gives good results.

theorem Cslib.DataStructure.BTree.InsertResult.mended_withRest {α β : Type} {t : ℕ} {x : InsertResult α β} {h : ℕ} (hx : Mended t h x) {rest : List (Triple α β)} (hr : ∀ e ∈ rest, Good t h e.child) :
Good t h (x.withRest rest).1 ∧ (∀ e ∈ (x.withRest rest).2, Good t h e.child) ∧ rest.length ≤ (x.withRest rest).2.length ∧ (x.withRest rest).2.length ≤ rest.length + 1

Placing a repaired result among good triples keeps them good, adding at most one key.

theorem Cslib.DataStructure.BTree.mended_mergeSplit {α β : Type} {t : ℕ} {l r : BTree α β} {h : ℕ} (hl : Underfull t (h + 1) l) (hr : Underfull t (h + 1) r) (hl₀ : l ≠ leaf) (hr₀ : r ≠ leaf) (k : α) (v : β) (hlo : t - 1 ≤ l.rootKeys + r.rootKeys + 1) (hhi : l.rootKeys + r.rootKeys + 1 ≤ 3 * t - 2) :
InsertResult.Mended t (h + 1) (mergeSplit t l k v r)

Merging two adjacent nodes of height h + 1 with between t - 2 and 3 * t - 3 keys in total gives good results.

theorem Cslib.DataStructure.BTree.good_rebalanceRight {α β : Type} {t : ℕ} (ht : 2 ≤ t) {c s : BTree α β} {h : ℕ} (hc : Underfull t h c) (hs : Good t h s) (k : α) (v : β) {rest : List (Triple α β)} (hr : ∀ e ∈ rest, Good t h e.child) :
Good t h (rebalanceRight t c k v s rest).1 ∧ (∀ e ∈ (rebalanceRight t c k v s rest).2, Good t h e.child) ∧ rest.length ≤ (rebalanceRight t c k v s rest).2.length ∧ (rebalanceRight t c k v s rest).2.length ≤ rest.length + 1

Repair against the right sibling: the results are good, with one key fewer or none.

theorem Cslib.DataStructure.BTree.good_rebalanceLast {α β : Type} {t : ℕ} (ht : 2 ≤ t) {prev c : BTree α β} {h : ℕ} (hp : Good t h prev) (hc : Underfull t h c) (k : α) (v : β) :
Good t h (rebalanceLast t prev k v c).1 ∧ (∀ e ∈ (rebalanceLast t prev k v c).2, Good t h e.child) ∧ (rebalanceLast t prev k v c).2.length ≤ 1

Repair of a last child against its left sibling: the results are good, with at most one triple.

theorem Cslib.DataStructure.BTree.eraseFits_popMin {α β : Type} {t : ℕ} (ht : 2 ≤ t) {tr : BTree α β} (hb : tr.Balanced) (ho : ∀ c ∈ tr.children, Occupied t c) (hpos : tr ≠ leaf → 1 ≤ tr.rootKeys) {e : α × β} {tr' : BTree α β} (hp : popMin t tr = some (e, tr')) :
EraseFits t tr tr'

Shape of the removal of the least entry.

def Cslib.DataStructure.BTree.eraseSubtree {α β : Type} [LinearOrder α] (t : ℕ) (k : α) (tr : BTree α β) :
BTree α β

Pure erasure from a subtree.

def Cslib.DataStructure.BTree.eraseRest {α β : Type} [LinearOrder α] (t : ℕ) (k : α) (prev prevErased : BTree α β) (rest : List (Triple α β)) :
BTree α β × List (Triple α β)

Pure erasure among the triples after prev, whose erasure is prevErased.

def Cslib.DataStructure.BTree.erase {α β : Type} [LinearOrder α] (t : ℕ) (k : α) (tr : BTree α β) :
BTree α β

Remove the entry with key k, if any, from a B-tree of minimum degree t: the Id instantiation of eraseM. An emptied root collapses into its only child.

theorem Cslib.DataStructure.BTree.eraseSubtree_def {α β : Type} [LinearOrder α] {t : ℕ} {k : α} (tr : BTree α β) :

eraseSubtree is the Id instantiation of eraseSubtreeM.

theorem Cslib.DataStructure.BTree.eraseRest_def {α β : Type} [LinearOrder α] {t : ℕ} {k : α} (prev pe : BTree α β) (rest : List (Triple α β)) :
eraseRest t k prev pe rest = (eraseRestM t SortedAssoc.leI (pure ()) k prev (fun (x : Unit) => pure pe) rest).run

eraseRest is the Id instantiation of eraseRestM, with prevErased wrapped in a constant thunk.

theorem Cslib.DataStructure.BTree.erase_def {α β : Type} [LinearOrder α] {t : ℕ} {k : α} (tr : BTree α β) :
erase t k tr = (eraseM t SortedAssoc.leI (pure ()) k tr).run

erase is the Id instantiation of eraseM.

@[simp]

Erasure from the empty tree.

@[simp]
theorem Cslib.DataStructure.BTree.erase_leaf {α β : Type} [LinearOrder α] {t : ℕ} {k : α} :

Erasure from the empty tree is the empty tree.

theorem Cslib.DataStructure.BTree.eraseSubtree_node {α β : Type} [LinearOrder α] {t : ℕ} {k : α} (first : BTree α β) (rest : List (Triple α β)) :
eraseSubtree t k (first.node rest) = (eraseRest t k first (eraseSubtree t k first) rest).1.node (eraseRest t k first (eraseSubtree t k first) rest).2

Erasure from a node is erasure among its triples.

@[simp]
theorem Cslib.DataStructure.BTree.eraseRest_nil {α β : Type} [LinearOrder α] {t : ℕ} {k : α} (prev pe : BTree α β) :
eraseRest t k prev pe [] = (pe, [])

Past the last triple, the erasure of the preceding child.

theorem Cslib.DataStructure.BTree.eraseRest_cons {α β : Type} [LinearOrder α] {t : ℕ} {k : α} (prev pe : BTree α β) (k' : α) (v' : β) (c : BTree α β) (rest : List (Triple α β)) :
eraseRest t k prev pe ((k', v', c) :: rest) = if k ≤ k' then if k' ≤ k then match popMin t c with | none => (prev, rest) | some ((k₂, v₂), c') => match rest with | [] => rebalanceLast t prev k₂ v₂ c' | (k₃, v₃, c₃) :: rest₃ => have p := rebalanceRight t c' k₃ v₃ c₃ rest₃; (prev, (k₂, v₂, p.1) :: p.2) else rebalanceRight t pe k' v' c rest else match rest with | [] => rebalanceLast t prev k' v' (eraseSubtree t k c) | head :: tail => have p := eraseRest t k c (eraseSubtree t k c) rest; (prev, (k', v', p.1) :: p.2)

Pure erasure at a triple.

theorem Cslib.DataStructure.BTree.erase_eq_collapse {α β : Type} [LinearOrder α] {t : ℕ} {k : α} (tr : BTree α β) :
erase t k tr = (eraseSubtree t k tr).collapse

Erasure is erasure from the root subtree, then collapsing an empty root.

theorem Cslib.DataStructure.BTree.eraseRest_spec {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {rest : List (Triple α β)} (hc : ∀ e ∈ rest, e.child.toList.IsSortedAssoc → (eraseSubtree t k e.child).toList = SortedAssoc.erase k e.child.toList) (prev pe : BTree α β) (hs : (prev.toList ++ toListRest rest).IsSortedAssoc) (hpe : prev.toList.IsSortedAssoc → pe.toList = SortedAssoc.erase k prev.toList) :
(eraseRest t k prev pe rest).1.toList ++ toListRest (eraseRest t k prev pe rest).2 = SortedAssoc.erase k (prev.toList ++ toListRest rest)

The search among the triples after prev, against the model: on sorted entries, given that prevErased is the erasure of prev, the result has the entries of the model erasure.

Model equation of erasure from a subtree with sorted entries.

Model equation of erasure on a tree with sorted entries.

theorem Cslib.DataStructure.BTree.good_eraseRest {α β : Type} [LinearOrder α] {t : ℕ} {k : α} (ht : 2 ≤ t) {h : ℕ} {rest : List (Triple α β)} (hc : ∀ e ∈ rest, Good t h e.child ∧ Underfull t h (eraseSubtree t k e.child)) {prev pe : BTree α β} (hp : Good t h prev) (hpe : Underfull t h pe) (hne : rest ≠ []) :
Good t h (eraseRest t k prev pe rest).1 ∧ (∀ e ∈ (eraseRest t k prev pe rest).2, Good t h e.child) ∧ (eraseRest t k prev pe rest).2.length ≤ rest.length ∧ rest.length ≤ (eraseRest t k prev pe rest).2.length + 1

Shape of the erasure among the triples after a good child prev, whose erasure pe is at most one key short: the results are good, with at most one key fewer.

theorem Cslib.DataStructure.BTree.eraseFits_eraseSubtree {α β : Type} [LinearOrder α] {t : ℕ} {k : α} (ht : 2 ≤ t) {tr : BTree α β} (hb : tr.Balanced) (ho : ∀ c ∈ tr.children, Occupied t c) (hpos : tr ≠ leaf → 1 ≤ tr.rootKeys) :
EraseFits t tr (eraseSubtree t k tr)

Shape invariant of erasure from a subtree.

theorem Cslib.DataStructure.BTree.toList_erase {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) :

Model equation of erasure on well-formed trees.

theorem Cslib.DataStructure.BTree.IsWF.erase {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) :
IsWF t (BTree.erase t k tr)

Erasure preserves well-formedness.

theorem Cslib.DataStructure.BTree.height_erase {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) :
(erase t k tr).height = tr.height ∨ (erase t k tr).height + 1 = tr.height

On a well-formed tree, erasure either keeps the height or lowers it by one.

theorem Cslib.DataStructure.BTree.height_erase_le {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) :
(erase t k tr).height ≤ tr.height

Erasure never raises the height.

theorem Cslib.DataStructure.BTree.height_le_height_erase_add_one {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) :
tr.height ≤ (erase t k tr).height + 1

Erasure lowers the height by at most one.

theorem Cslib.DataStructure.BTree.size_erase_of_isSortedAssoc {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : tr.toList.IsSortedAssoc) (k : α) :
(erase t k tr).size = if (lookup k tr).isSome = true then tr.size - 1 else tr.size

On a tree with sorted entries, erasure removes one entry when the key is present and none otherwise.

theorem Cslib.DataStructure.BTree.size_erase {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) :
(erase t k tr).size = if (lookup k tr).isSome = true then tr.size - 1 else tr.size

Erasure removes one entry when the key is present and none otherwise.

@[simp]
theorem Cslib.DataStructure.BTree.lookup_erase_of_isSortedAssoc {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : tr.toList.IsSortedAssoc) (k k' : α) :
lookup k' (erase t k tr) = if k' = k then none else lookup k' tr

On a tree with sorted entries, lookup after erasure fails at the erased key and is unchanged elsewhere.

@[simp]
theorem Cslib.DataStructure.BTree.lookup_erase {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k k' : α) :
lookup k' (erase t k tr) = if k' = k then none else lookup k' tr

Lookup after erasure fails at the erased key and is unchanged elsewhere.

theorem Cslib.DataStructure.BTree.toList_erase_of_lookup_eq_none_of_isSortedAssoc {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : tr.toList.IsSortedAssoc) {k : α} (hk : lookup k tr = none) :
(erase t k tr).toList = tr.toList

On a tree with sorted entries, erasing an absent key leaves the entries unchanged.

theorem Cslib.DataStructure.BTree.toList_erase_of_lookup_eq_none {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) {k : α} (hk : lookup k tr = none) :
(erase t k tr).toList = tr.toList

Erasing an absent key from a well-formed tree leaves the entries unchanged.