Documentation

CslibFrontier.DataStructure.BTree.Comparisons.Erase

Comparison costs of B-tree erasure #

eraseQ, eraseMinQ and eraseMaxQ instantiate eraseM, eraseMinM and eraseMaxM at FreeM (LEQuery α) with the query interface LEQuery.ask and visit := pure (), in the comparison-query model of Cslib.DataStructure.BTree.Comparisons.Query.

Erasure compares keys only while it scans the nodes of its search path, one node per level: at most one comparison per triple passed and a second at the triple where the scan stops, so at most rest.length + 1 at a node. Everything else it does is free of comparisons. The repairs rebalanceRightM and rebalanceLastM and the removal popMinM of the successor of an interior key take no comparator, and neither do minM and maxM, which find the entry that eraseMinM and eraseMaxM remove. The comparison bound of all three operations is therefore that of insertion, 2 * t * height, and not the node-visit counts 2 * height and 3 * height times the per-node bound 2 * t: the siblings that a repair visits, the path to the successor and the pass to the extreme entry are visited without comparing keys.

The bounds hold under every oracle, but only on well-formed trees: a node of a raw tree may hold more than 2 * t - 1 keys.

Main definitions #

Main results #

References #

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

Erasure, counting comparisons.

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

eraseQ is eraseM with LEQuery.ask and free visits.

def Cslib.DataStructure.BTree.eraseMinQ {α β : Type} (t : ℕ) (tr : BTree α β) :
FreeM (Query.LEQuery α) (Option ((α × β) × BTree α β))

Removal of the least entry, counting comparisons.

eraseMinQ is eraseMinM with LEQuery.ask and free visits.

def Cslib.DataStructure.BTree.eraseMaxQ {α β : Type} (t : ℕ) (tr : BTree α β) :
FreeM (Query.LEQuery α) (Option ((α × β) × BTree α β))

Removal of the greatest entry, counting comparisons.

eraseMaxQ is eraseMaxM with LEQuery.ask and free visits.

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

Under the order's oracle, eraseQ evaluates to the pure erasure erase.

@[simp]
theorem Cslib.DataStructure.BTree.eval_eraseMinQ {α β : Type} {t : ℕ} {tr : BTree α β} [LinearOrder α] :
FreeM.eval (fun {ι : Type} => orderOracle) (eraseMinQ t tr) = eraseMin? t tr

Under the order's oracle, eraseMinQ evaluates to eraseMin?.

@[simp]
theorem Cslib.DataStructure.BTree.eval_eraseMaxQ {α β : Type} {t : ℕ} {tr : BTree α β} [LinearOrder α] :
FreeM.eval (fun {ι : Type} => orderOracle) (eraseMaxQ t tr) = eraseMax? t tr

Under the order's oracle, eraseMaxQ evaluates to eraseMax?.

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

Erasure from the empty tree makes no comparisons.

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

Removing the least entry of the empty tree makes no comparisons.

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

Removing the greatest entry of the empty tree makes no comparisons.

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

Erasure makes at most 2 * t * height comparisons: it scans one node per level, and its repairs and the removal of a successor compare no keys.

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

Removing the least entry makes at most 2 * t * height comparisons: finding it makes none, and the rest is the erasure of its key.

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

Removing the greatest entry makes at most 2 * t * height comparisons: finding it makes none, and the rest is the erasure of its key.

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

Erasure from a well-formed tree makes logarithmically many comparisons.

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

Removing the least entry of a well-formed tree makes logarithmically many comparisons.

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

Removing the greatest entry of a well-formed tree makes logarithmically many comparisons.

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

UpperBound packaging for eraseQ, on well-formed trees.

theorem Cslib.DataStructure.BTree.eraseMinQ_upperBound {α β : Type} {t : ℕ} [LinearOrder α] :
Query.UpperBound (fun (p : { tr : BTree α β // IsWF t tr }) => eraseMinQ t ↑p) (fun (p : { tr : BTree α β // IsWF t tr }) => (↑p).height) fun (h : ℕ) => 2 * t * h

UpperBound packaging for eraseMinQ, on well-formed trees.

theorem Cslib.DataStructure.BTree.eraseMaxQ_upperBound {α β : Type} {t : ℕ} [LinearOrder α] :
Query.UpperBound (fun (p : { tr : BTree α β // IsWF t tr }) => eraseMaxQ t ↑p) (fun (p : { tr : BTree α β // IsWF t tr }) => (↑p).height) fun (h : ℕ) => 2 * t * h

UpperBound packaging for eraseMaxQ, on well-formed trees.