Documentation

CslibFrontier.DataStructure.BTree.Comparisons.Insert

Comparison costs of B-tree insertion #

insertQ instantiates insertM at FreeM (LEQuery α) with the query interface LEQuery.ask and visit := pure (), in the comparison-query model of Cslib.DataStructure.BTree.Comparisons.Query.

Insertion queries the order of keys to find the correct subtree during the downward search. The splits on the way back up and the new root grown on overflow make zero comparisons, because they only rebuild nodes using keys already compared and sorted. At each node, scanning its rest contributes at most rest.length + 1 comparisons: one per passed triple, with a possible second comparison at the stopping separator.

Main definitions #

Main results #

References #

def Cslib.DataStructure.BTree.insertQ {α β : Type} (t : ℕ) (k : α) (v : β) (tr : BTree α β) :

Counted B-tree insert operation.

theorem Cslib.DataStructure.BTree.insertQ_def {α β : Type} {t : ℕ} {k : α} {v : β} {tr : BTree α β} :

insertQ is insertM with LEQuery.ask and pure ().

@[simp]
theorem Cslib.DataStructure.BTree.eval_insertQ {α β : Type} {t : ℕ} {k : α} {v : β} {tr : BTree α β} [LinearOrder α] :
FreeM.eval (fun {ι : Type} => orderOracle) (insertQ t k v tr) = insert t k v tr

Under the order's oracle, insertQ evaluates to the pure insertion insert.

@[simp]
theorem Cslib.DataStructure.BTree.countQueries_insertQ_leaf {α β : Type} {t : ℕ} {k : α} {v : β} (oracle : {ι : Type} → Query.LEQuery α ι → ι) :
FreeM.countQueries (fun {ι : Type} => oracle) (insertQ t k v leaf) = 0

Insertion into the empty tree makes zero comparisons.

theorem Cslib.DataStructure.BTree.countQueries_insertQ_le {α β : Type} {t : ℕ} {k : α} {v : β} {tr : BTree α β} [LinearOrder α] (oracle : {ι : Type} → Query.LEQuery α ι → ι) (h : IsWF t tr) :
FreeM.countQueries (fun {ι : Type} => oracle) (insertQ t k v tr) ≤ 2 * t * tr.height

Insertion makes at most 2 * t * height comparisons.

theorem Cslib.DataStructure.BTree.countQueries_insertQ_le_log {α β : Type} {t : ℕ} {k : α} {v : β} {tr : BTree α β} [LinearOrder α] (oracle : {ι : Type} → Query.LEQuery α ι → ι) (h : IsWF t tr) :
FreeM.countQueries (fun {ι : Type} => oracle) (insertQ t k v tr) ≤ 2 * t * (Nat.log t ((tr.size + 1) / 2) + 1)

Insertion into a well-formed tree makes logarithmically many comparisons.

theorem Cslib.DataStructure.BTree.insertQ_upperBound {α β : Type} {t : ℕ} [LinearOrder α] (v : β) :
Query.UpperBound (fun (p : WFInput α β t) => insertQ t (↑p).2 v (↑p).1) (fun (p : WFInput α β t) => (↑p).1.height) fun (h : ℕ) => 2 * t * h

UpperBound packaging for insertQ.