Documentation

CslibFrontier.DataStructure.BPlusTree.Erase.Basic

Erasure from B+-trees #

Erasure for the B+-trees of Cslib.DataStructure.BPlusTree, bottom-up, with the shape of the classic Cslib.DataStructure.BTree.eraseM: the entry is removed from a leaf, and a leaf or node left one item short (underflows) is repaired by its parent against an adjacent sibling.

The repair merges the two siblings and splits the result again with the insertion splitters when it has more than 2 * t - 1 items (mergeSplit), which covers both borrowing and merging. Two leaves are concatenated and their separator is dropped; if they split again, the first key of the new right leaf is copied up. Two nodes are joined around their separator, which is pulled down; if they split again, a separator moves up. A child is repaired against its right sibling, and the last child against its left one. A root left without separators collapses into its only child (collapse), so the height falls only there.

Every entry is in a leaf, so there is no interior case: the search descends by the separators to one leaf and removes the entry there. A separator changes only in mergeSplit: a separator equal to the erased key stays where it is, and need not be a key afterwards, unless a repair drops or replaces it.

Each operation is defined once over an arbitrary monad m, with a comparator le : α → α → m Bool and a node-visit action visit : m Unit, and no order instance in scope. The pure operations are the Id instantiations with the comparator Cslib.SortedAssoc.leI of a linear order and visit := pure (). visit fires once at each node on the path from the root to the leaf, and once more for each sibling a repair inspects. At an interior node the separators are scanned as in slotM, one comparison per separator passed and one more where the scan stops. In the leaf the entry is removed by Cslib.SortedAssoc.eraseM. eraseRestM takes the child before the branches 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, 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 #

The model equation needs more than sorted entries, as for insertion: the search trusts each separator to bound the later children from below, which holds through a key of the child the separator introduces, so the children must not be empty. Well-formedness provides that.

References #

The repair by merging and splitting again follows the functional deletion of [Mun21] for classic B-trees. The statements here are not taken from a source.

A leaf with fewer than t - 1 entries, or a node with fewer than t - 1 separators: a child that erasure left one item short.

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

Merge two adjacent siblings, split again by the insertion splitters when the result has more than 2 * t - 1 items. Two leaves are concatenated and the separator is dropped; two nodes are joined around the separator. Siblings of a balanced tree are both leaves or both nodes; the last case is unreachable there.

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

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

Repair a child c followed by its right sibling r under the separator s and the branches rest: if c underflows, inspect r (one visit) and merge them.

def Cslib.DataStructure.BPlusTree.rebalanceLastM {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (visit : m Unit) (prev : BPlusTree α β) (s : α) (c : BPlusTree α β) :
m (BPlusTree α β × List (Branch α β))

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

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

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

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

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

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

Erasure from a subtree; the result may underflow by one item. A node is visited once, on entry.

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

Erasure among the branches after the child prev, whose erasure is prevErased (), scanning the separators in order with the comparisons of slotM. At a separator above the key, prev is erased from and repaired against its right sibling. Past the last separator, the last child is erased from and repaired against its left sibling.

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

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; a root left without separators collapses into its only child.

theorem Cslib.DataStructure.BPlusTree.eraseM_def {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (tr : BPlusTree α β) :
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.

theorem Cslib.DataStructure.BPlusTree.eraseSubtreeM_leaf {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (entries : List (α × β)) :
eraseSubtreeM t le visit k (leaf entries) = do visit let __do_lift ← SortedAssoc.eraseM le k entries pure (leaf __do_lift)

A leaf is visited once, and the entry is removed by the scan of its entries.

theorem Cslib.DataStructure.BPlusTree.eraseSubtreeM_node {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (first : BPlusTree α β) (rest : List (Branch α β)) :
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 branches are searched.

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

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

theorem Cslib.DataStructure.BPlusTree.eraseRestM_cons {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (prev : BPlusTree α β) (prevErased : Unit → m (BPlusTree α β)) (s : α) (c : BPlusTree α β) (rest : List (Branch α β)) :
eraseRestM t le visit k prev prevErased ((s, c) :: rest) = do let __do_lift ← le s k if __do_lift = true then match rest with | [] => do let __do_lift ← eraseSubtreeM t le visit k c rebalanceLastM t visit prev s __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, (s, p.1) :: p.2) else do let __do_lift ← prevErased () rebalanceRightM t visit __do_lift s c rest

At a branch: when its separator is above the key, erase from the preceding child and repair it against this child; otherwise move on, erasing from the last child and repairing it against its left sibling.

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

A monad morphism commutes with erasure.

@[simp]
theorem Cslib.DataStructure.BPlusTree.toList_mergeSplit {α β : Type} {t : ℕ} (l : BPlusTree α β) (s : α) (r : BPlusTree α β) :

Merging keeps the entries of both siblings; the separator is not an entry.

@[simp]

Collapsing an empty root keeps the entries.

Shape #

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

Pure erasure.

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

erase is the Id instantiation of eraseM.

The model equation #

The shape of the result #

The separator convention of the result #

Erasure on well-formed trees #

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

Model equation of erasure on well-formed trees.

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

Erasure preserves well-formedness.

theorem Cslib.DataStructure.BPlusTree.height_erase {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (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.BPlusTree.size_erase {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (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.

theorem Cslib.DataStructure.BPlusTree.toList_erase_of_lookup_eq_none {α β : Type} [LinearOrder α] {t : ℕ} {tr : BPlusTree α β} (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.