Documentation

CslibFrontier.DataStructure.BTree.Insert

Insertion into classic B-trees #

Insert-or-replace in the B-trees of Cslib.DataStructure.BTree, by the bottom-up algorithm of the functional literature: a recursive call returns either a tree of the same height or an overflow split around a median entry (InsertResult), and the parent absorbs the overflow, splitting in turn when it then has 2 * t keys (InsertResult.ofNode). An overflow at the root grows a new root (InsertResult.toTree), so the height grows only there. The insertion into the empty tree overflows at once, which makes the bottom level uniform with the rest. This is the shape of the classic algorithm of [BMcC72]; [CLRS09], Chapter 18, splits full nodes pre-emptively on the way down instead. The tree-or-split result follows the functional insertion of [Mun21], whose up⇩i it mirrors.

Each operation is defined once over an arbitrary monad m, with a comparator le : α → α → m Bool and a node-visit action visit : m Unit, as in Cslib.DataStructure.BTree.Lookup. insertOrSplitM inserts into a subtree and insertRestM searches among the triples of a node, with the scan and the equality test of lookupM. visit fires once on entering each node of the search path, and never at the empty leaf. The pure operations are the Id instantiations with leI and visit := pure (). The definitions are structurally recursive and @[expose]d, so decide evaluates insertion on concrete trees from other modules, and the monadic constructor equations hold by rfl. Costs are not stated here.

Main definitions #

Main results #

References #

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

The result of inserting into a subtree: a tree of the same height, or an overflow split into two trees of the same height around a median entry.

  • tree {α β : Type} (tr : BTree α β) : InsertResult α β

    The subtree absorbed the entry.

  • split {α β : Type} (left : BTree α β) (k : α) (v : β) (right : BTree α β) : InsertResult α β

    The subtree overflowed and split around the entry (k, v).

The entries of a result in order.

Put a result in the place of a child followed by the triples rest.

def Cslib.DataStructure.BTree.InsertResult.ofNode {α β : Type} (t : ℕ) (first : BTree α β) (rest : List (Triple α β)) :

A node with contents first and rest, split when it has more than 2 * t - 1 keys: the first t keys stay on the left, the next one is the median, and the rest go right. The [] branch is unreachable for t ≥ 1.

The tree of a result at the root: an overflow grows a new root.

@[simp]

The entries of a tree result are the tree's.

@[simp]
theorem Cslib.DataStructure.BTree.InsertResult.toList_split {α β : Type} (l r : BTree α β) (k : α) (v : β) :
(split l k v r).toList = l.toList ++ (k, v) :: r.toList

The entries of a split are the left entries, the median, then the right entries.

@[simp]

Growing a new root keeps the entries.

theorem Cslib.DataStructure.BTree.InsertResult.toList_withRest {α β : Type} (x : InsertResult α β) (rest : List (Triple α β)) :
x.toList ++ toListRest rest = (x.withRest rest).1.toList ++ toListRest (x.withRest rest).2

Placing a result in the place of a child keeps the entries, followed by those of rest.

@[simp]
theorem Cslib.DataStructure.BTree.InsertResult.toList_ofNode {α β : Type} {t : ℕ} (first : BTree α β) (rest : List (Triple α β)) :
(ofNode t first rest).toList = (first.node rest).toList

Splitting an overflowing node keeps its entries.

Shape #

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

A subtree of height h that is balanced and occupied: what every child of a well-formed node is.

  • balanced : c.Balanced

    The subtree is balanced.

  • height_eq : c.height = h

    The subtree has height h.

  • occupied : Occupied t c

    Every node of the subtree is occupied.

theorem Cslib.DataStructure.BTree.node_of_good {α β : Type} {t : ℕ} {first : BTree α β} {rest : List (Triple α β)} {h : ℕ} (hf : Good t h first) (hr : ∀ e ∈ rest, Good t h e.child) :
(first.node rest).Balanced ∧ (first.node rest).height = h + 1 ∧ ∀ x ∈ (first.node rest).children, Occupied t x

A node over good children of height h is balanced, of height h + 1, with occupied children.

theorem Cslib.DataStructure.BTree.good_node {α β : Type} {t : ℕ} {first : BTree α β} {rest : List (Triple α β)} {h : ℕ} (hf : Good t h first) (hr : ∀ e ∈ rest, Good t h e.child) (h₁ : t - 1 ≤ rest.length) (h₂ : rest.length ≤ 2 * t - 1) :
Good t (h + 1) (first.node rest)

A node with t - 1 to 2 * t - 1 keys over good children of height h is good, of height h + 1.

theorem Cslib.DataStructure.BTree.good_of_balanced_node {α β : Type} {t : ℕ} {first : BTree α β} {rest : List (Triple α β)} (hb : (first.node rest).Balanced) (ho : ∀ c ∈ (first.node rest).children, Occupied t c) :
Good t first.height first ∧ ∀ e ∈ rest, Good t first.height e.child

The children of a balanced node whose children are occupied are good, of the height of its first child.

theorem Cslib.DataStructure.BTree.InsertResult.balanced_toTree_split {α β : Type} {l r : BTree α β} (k : α) (v : β) (hl : l.Balanced) (hr : r.Balanced) (hh : r.height = l.height) :
(split l k v r).toTree.Balanced ∧ (split l k v r).toTree.height = l.height + 1

A new root grown from a split of two balanced trees of equal height is balanced and one taller.

theorem Cslib.DataStructure.BTree.InsertResult.exists_good_split_ofNode {α β : Type} {t : ℕ} {first : BTree α β} {rest : List (Triple α β)} {h : ℕ} (hf : Good t h first) (hr : ∀ e ∈ rest, Good t h e.child) (hlo : 2 * t - 1 < rest.length) (hhi : rest.length ≤ 2 * t - 1 + t + 1) :
∃ (l : BTree α β) (k : α) (v : β) (r : BTree α β), ofNode t first rest = split l k v r ∧ Good t (h + 1) l ∧ Good t (h + 1) r

A node over good children of height h with more than 2 * t - 1 keys, and at most t + 1 more, splits into two good trees of height h + 1: the first t keys go left, the next one is the median, and the rest go right.

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

The shape invariant of the result of inserting into tr: either a tree of the same height with at least as many root keys, still within 2 * t - 1, or two good trees of the same height.

theorem Cslib.DataStructure.BTree.InsertResult.fits_ofNode {α β : Type} {t : ℕ} {tr first : BTree α β} {rest : List (Triple α β)} {h : ℕ} (hf : Good t h first) (hr : ∀ e ∈ rest, Good t h e.child) (hlen : rest.length ≤ 2 * t - 1 + 1) (htr : tr ≠ leaf) (hh : tr.height = h + 1) (hk : tr.rootKeys ≤ rest.length) :
Fits t tr (ofNode t first rest)

A node over good children of height h with at most one key more than the 2 * t - 1 allowed, after the overflow split, fits any non-empty tree of height h + 1 that had no more keys.

theorem Cslib.DataStructure.BTree.InsertResult.fits_withRest {α β : Type} {t : ℕ} {c : BTree α β} {x : InsertResult α β} (hx : Fits t c x) (hc : Occupied t c) {h : ℕ} (hch : c.height = h) {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 fitting result in the place of an occupied child of height h among good triples keeps every child good, and adds at most one key.

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

Bottom-up insertion into a subtree: the tree with the entry inserted, or its split when it overflows. A node is visited once, on entry; the empty leaf is not visited and overflows at once into the entry between two empty leaves.

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

Insertion among the triples of a node, scanning them in order with the comparisons of lookupM: none defers to the child before the triples, because the key is below the first of them; some rest' is the new list of triples, with the value replaced at an equal key or the result of the descent placed in the child after the last key below the query.

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

Inserting into the empty tree overflows at once, without a visit: the parent absorbs the new entry.

theorem Cslib.DataStructure.BTree.insertOrSplitM_node {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (v : β) (first : BTree α β) (rest : List (Triple α β)) :
insertOrSplitM t le visit k v (first.node rest) = do visit let __do_lift ← insertRestM t le visit k v rest match __do_lift with | some rest' => pure (InsertResult.ofNode t first rest') | none => do let __do_lift ← insertOrSplitM t le visit k v first have p : BTree α β × List (Triple α β) := __do_lift.withRest rest pure (InsertResult.ofNode t p.1 p.2)

A node is visited once, before its triples are searched; the result is split when it overflows.

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

The search past the last triple defers to the child before it.

theorem Cslib.DataStructure.BTree.insertRestM_cons {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (v : β) (k' : α) (v' : β) (c : BTree α β) (rest : List (Triple α β)) :
insertRestM t le visit k v ((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 pure (some ((k, v, c) :: rest)) else pure none else do let __do_lift ← insertRestM t le visit k v rest match __do_lift with | some rest' => pure (some ((k', v', c) :: rest')) | none => do let __do_lift ← insertOrSplitM t le visit k v c have p : BTree α β × List (Triple α β) := __do_lift.withRest rest pure (some ((k', v', p.1) :: p.2))

At a triple: replace on equality, defer to the preceding child when the key is below, and otherwise move on, descending into this triple's child when every later key is above.

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

Insert-or-replace: the bottom-up insertion, growing a new root on overflow.

theorem Cslib.DataStructure.BTree.insertM_def {α β : Type} {m : Type → Type w} [Monad m] (t : ℕ) (le : α → α → m Bool) (visit : m Unit) (k : α) (v : β) (tr : BTree α β) :
insertM t le visit k v tr = do let __do_lift ← insertOrSplitM t le visit k v tr pure __do_lift.toTree

Insertion is the bottom-up insertion with an overflow at the root grown into a new root.

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

A monad morphism commutes with the bottom-up insertion into a subtree.

theorem Cslib.IsMonadHom.map_insertRestM {α β : 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 : α) (v : β) (rest : List (DataStructure.BTree.Triple α β)) :
f (DataStructure.BTree.insertRestM t le visit k v rest) = DataStructure.BTree.insertRestM t (fun (a b : α) => f (le a b)) (f visit) k v rest

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

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

A monad morphism commutes with insertion.

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

Pure bottom-up insertion into a subtree.

def Cslib.DataStructure.BTree.insertRest {α β : Type} [LinearOrder α] (t : ℕ) (k : α) (v : β) (rest : List (Triple α β)) :
Option (List (Triple α β))

Pure insertion among the triples of a node.

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

Pure insertion.

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

insertOrSplit is the Id instantiation of insertOrSplitM.

theorem Cslib.DataStructure.BTree.insertRest_def {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} (rest : List (Triple α β)) :
insertRest t k v rest = (insertRestM t SortedAssoc.leI (pure ()) k v rest).run

insertRest is the Id instantiation of insertRestM.

theorem Cslib.DataStructure.BTree.insert_def {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} (tr : BTree α β) :
insert t k v tr = (insertM t SortedAssoc.leI (pure ()) k v tr).run

insert is the Id instantiation of insertM.

theorem Cslib.DataStructure.BTree.insert_eq_toTree {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} (tr : BTree α β) :
insert t k v tr = (insertOrSplit t k v tr).toTree

Insertion is the bottom-up insertion with an overflow grown into a new root.

@[simp]

Inserting into the empty tree overflows at once.

theorem Cslib.DataStructure.BTree.insertOrSplit_node {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} (first : BTree α β) (rest : List (Triple α β)) :
insertOrSplit t k v (first.node rest) = match insertRest t k v rest with | some rest' => InsertResult.ofNode t first rest' | none => have p := (insertOrSplit t k v first).withRest rest; InsertResult.ofNode t p.1 p.2

Insertion into a node: search its triples, descending into the first child when the key is below them, then split on overflow.

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

The search past the last triple defers to the child before it.

theorem Cslib.DataStructure.BTree.insertRest_cons {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} (k' : α) (v' : β) (c : BTree α β) (rest : List (Triple α β)) :
insertRest t k v ((k', v', c) :: rest) = if k ≤ k' then if k' ≤ k then some ((k, v, c) :: rest) else none else match insertRest t k v rest with | some rest' => some ((k', v', c) :: rest') | none => have p := (insertOrSplit t k v c).withRest rest; some ((k', v', p.1) :: p.2)

The search at a triple: replace on equality, defer when the key is below, and otherwise move on, descending into this triple's child when every later key is above.

theorem Cslib.DataStructure.BTree.insertRest_spec {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} {rest : List (Triple α β)} (hc : ∀ e ∈ rest, e.child.toList.IsSortedAssoc → (insertOrSplit t k v e.child).toList = SortedAssoc.insert k v e.child.toList) (pre : List (α × β)) (hs : (pre ++ toListRest rest).IsSortedAssoc) :
(insertRest t k v rest = none → ∀ p ∈ toListRest rest, k < p.1) ∧ ∀ (r : List (Triple α β)), insertRest t k v rest = some r → pre ++ toListRest r = SortedAssoc.insert k v (pre ++ toListRest rest)

The search among the triples of a node, against the model: when it defers to the child before the triples, the key is below all their entries; otherwise it performs the model insertion after any sorted prefix of smaller keys.

Model equation of the bottom-up insertion on a subtree with sorted entries.

theorem Cslib.DataStructure.BTree.toList_insert_of_isSortedAssoc {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} {tr : BTree α β} (h : tr.toList.IsSortedAssoc) :

Model equation of insertion on a tree with sorted entries.

theorem Cslib.DataStructure.BTree.fits_insertRest {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} {rest : List (Triple α β)} {h : ℕ} (hc : ∀ e ∈ rest, Good t h e.child ∧ InsertResult.Fits t e.child (insertOrSplit t k v e.child)) {r : List (Triple α β)} (hr : insertRest t k v rest = some r) :
(∀ e ∈ r, Good t h e.child) ∧ rest.length ≤ r.length ∧ r.length ≤ rest.length + 1

The search among the triples of a node keeps every child good, and adds at most one key.

theorem Cslib.DataStructure.BTree.fits_insertOrSplit {α β : Type} [LinearOrder α] {t : ℕ} {k : α} {v : β} {tr : BTree α β} (hb : tr.Balanced) (ho : ∀ c ∈ tr.children, Occupied t c) (hk : tr.rootKeys ≤ 2 * t - 1) :

Shape invariant of the bottom-up insertion.

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

Model equation of insertion on well-formed trees.

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

Insertion preserves well-formedness.

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

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

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

Insertion raises the height by at most one.

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

Insertion never lowers the height.

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

On a tree with sorted entries, insertion keeps the size when the key is present and adds one otherwise.

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

Insertion keeps the size when the key is present and adds one otherwise.

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

On a tree with sorted entries, lookup after insertion finds the new value at its key and is unchanged elsewhere.

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

Lookup after insertion finds the new value at its key and is unchanged elsewhere.