Documentation

CslibFrontier.DataStructure.BTree.PredSucc

Predecessor and successor in classic B-trees #

Strict predecessor and successor queries scan the keys of a node and descend into one child, retaining a separator entry as a fallback. [CLRS09] leaves the predecessor as Exercise 18.2-3; the descent here follows the search of its Section 18.2. On a tree whose toList is sorted, run with the comparator of the key order, the predecessor is the greatest entry with key strictly below the query and the successor is the least entry with key strictly above it, so a key equal to the query is never returned. For an arbitrary comparator or an unsorted tree, the definitions describe only the descent.

predM and succM take a monadic comparator and a node-visit action, with no order instance. Scanning triples within a node is free: visit fires once when the scan chooses a child, including at nodes with no keys, and never at an empty leaf. Fallbacks require no extra descent. The definitions use well-founded recursion on sizeOf; their equation lemmas are the interface for evaluation and for other monadic instantiations.

Main definitions #

Main results #

References #

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

@[irreducible]
def Cslib.DataStructure.BTree.predM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) :
BTree α β → m (Option (α × β))

Strict predecessor search. Scan past the keys k' for which le k k' fails, retaining each as a fallback; at the first key k' with le k k', visit the node and descend into the child before it. If there is no such key, visit the node and descend into its last child. When le decides the key order and toList is sorted, the result is the greatest entry with key strictly below k; see pred?_eq.

@[irreducible]
def Cslib.DataStructure.BTree.succM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) :
BTree α β → m (Option (α × β))

Strict successor search. Scan past the keys k' with le k' k; at the first key k' for which that fails, visit the node and descend into the child before it, retaining (k', v) as a fallback. If there is no such key, visit the node and descend into its last child. When le decides the key order and toList is sorted, the result is the least entry with key strictly above k; see succ?_eq.

theorem Cslib.DataStructure.BTree.predM_leaf {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) :
predM le visit k leaf = pure none

The empty tree has no predecessor and is not visited.

theorem Cslib.DataStructure.BTree.predM_node_nil {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) (first : BTree α β) :
predM le visit k (first.node []) = do visit predM le visit k first

A node without keys is visited before searching its only child.

theorem Cslib.DataStructure.BTree.predM_node_cons {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k k' : α) (v : β) (first c : BTree α β) (rest : List (Triple α β)) :
predM le visit k (first.node ((k', v, c) :: rest)) = do let __do_lift ← le k k' if __do_lift = true then do visit predM le visit k first else do let __do_lift ← predM le visit k (c.node rest) pure (__do_lift.or (some (k', v)))

A predecessor scan either descends before the first separator or continues with that separator as a fallback, without an additional visit.

theorem Cslib.DataStructure.BTree.succM_leaf {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) :
succM le visit k leaf = pure none

The empty tree has no successor and is not visited.

theorem Cslib.DataStructure.BTree.succM_node_nil {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) (first : BTree α β) :
succM le visit k (first.node []) = do visit succM le visit k first

A node without keys is visited before searching its only child.

theorem Cslib.DataStructure.BTree.succM_node_cons {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k k' : α) (v : β) (first c : BTree α β) (rest : List (Triple α β)) :
succM le visit k (first.node ((k', v, c) :: rest)) = do let __do_lift ← le k' k if __do_lift = true then succM le visit k (c.node rest) else do visit let __do_lift ← succM le visit k first pure (__do_lift.or (some (k', v)))

A successor scan either continues past the first separator for free or visits the node and descends before that separator, retaining it as a fallback.

theorem Cslib.IsMonadHom.map_predM {α β : 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) (le : α → α → m Bool) (visit : m Unit) (k : α) (tr : DataStructure.BTree α β) :
f (DataStructure.BTree.predM le visit k tr) = DataStructure.BTree.predM (fun (a b : α) => f (le a b)) (f visit) k tr

A monad morphism commutes with the predecessor search.

theorem Cslib.IsMonadHom.map_succM {α β : 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) (le : α → α → m Bool) (visit : m Unit) (k : α) (tr : DataStructure.BTree α β) :
f (DataStructure.BTree.succM le visit k tr) = DataStructure.BTree.succM (fun (a b : α) => f (le a b)) (f visit) k tr

A monad morphism commutes with the successor search.

def Cslib.DataStructure.BTree.pred? {α β : Type} [LinearOrder α] (k : α) (tr : BTree α β) :
Option (α × β)

Pure strict predecessor: the Id instantiation of predM with a trivial visit action.

def Cslib.DataStructure.BTree.succ? {α β : Type} [LinearOrder α] (k : α) (tr : BTree α β) :
Option (α × β)

Pure strict successor: the Id instantiation of succM with a trivial visit action.

theorem Cslib.DataStructure.BTree.pred?_def {α β : Type} [LinearOrder α] {tr : BTree α β} (k : α) :

pred? is the Id instantiation of predM.

theorem Cslib.DataStructure.BTree.succ?_def {α β : Type} [LinearOrder α] {tr : BTree α β} (k : α) :

succ? is the Id instantiation of succM.

@[simp]

The empty tree has no predecessor.

@[simp]
theorem Cslib.DataStructure.BTree.pred?_node_nil {α β : Type} [LinearOrder α] (k : α) (first : BTree α β) :
pred? k (first.node []) = pred? k first

A node without keys has the predecessor of its only child.

@[simp]
theorem Cslib.DataStructure.BTree.pred?_node_cons {α β : Type} [LinearOrder α] (k k' : α) (v : β) (first c : BTree α β) (rest : List (Triple α β)) :
pred? k (first.node ((k', v, c) :: rest)) = if k ≤ k' then pred? k first else (pred? k (c.node rest)).or (some (k', v))

The predecessor lies before the first separator at least k; a smaller separator is a fallback for the search in the remaining suffix.

@[simp]

The empty tree has no successor.

@[simp]
theorem Cslib.DataStructure.BTree.succ?_node_nil {α β : Type} [LinearOrder α] (k : α) (first : BTree α β) :
succ? k (first.node []) = succ? k first

A node without keys has the successor of its only child.

@[simp]
theorem Cslib.DataStructure.BTree.succ?_node_cons {α β : Type} [LinearOrder α] (k k' : α) (v : β) (first c : BTree α β) (rest : List (Triple α β)) :
succ? k (first.node ((k', v, c) :: rest)) = if k' ≤ k then succ? k (c.node rest) else (succ? k first).or (some (k', v))

The successor search skips separators at most k; the first larger separator is a fallback for the search in the child before it.

Model equation: on a tree with sorted entries, pred? is the model's strict predecessor in toList.

Model equation: on a tree with sorted entries, succ? is the model's strict successor in toList.

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

Model equation for strict predecessor on well-formed trees.

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

Model equation for strict successor on well-formed trees.

theorem Cslib.DataStructure.BTree.pred?_eq_none_iff_of_isSortedAssoc {α β : Type} [LinearOrder α] {tr : BTree α β} (h : tr.toList.IsSortedAssoc) (k : α) :
pred? k tr = none ↔ ∀ q ∈ tr, k ≤ q.1

On a tree with sorted entries, there is no predecessor exactly when every key is at least the query.

theorem Cslib.DataStructure.BTree.succ?_eq_none_iff_of_isSortedAssoc {α β : Type} [LinearOrder α] {tr : BTree α β} (h : tr.toList.IsSortedAssoc) (k : α) :
succ? k tr = none ↔ ∀ q ∈ tr, q.1 ≤ k

On a tree with sorted entries, there is no successor exactly when every key is at most the query.

theorem Cslib.DataStructure.BTree.pred?_eq_some_iff_of_isSortedAssoc {α β : Type} [LinearOrder α] {tr : BTree α β} (h : tr.toList.IsSortedAssoc) (k : α) {p : α × β} :
pred? k tr = some p ↔ p ∈ tr ∧ p.1 < k ∧ ∀ q ∈ tr, q.1 < k → q.1 ≤ p.1

On a tree with sorted entries, the predecessor is exactly the greatest entry whose key is strictly below the query.

theorem Cslib.DataStructure.BTree.succ?_eq_some_iff_of_isSortedAssoc {α β : Type} [LinearOrder α] {tr : BTree α β} (h : tr.toList.IsSortedAssoc) (k : α) {p : α × β} :
succ? k tr = some p ↔ p ∈ tr ∧ k < p.1 ∧ ∀ q ∈ tr, k < q.1 → p.1 ≤ q.1

On a tree with sorted entries, the successor is exactly the least entry whose key is strictly above the query.

theorem Cslib.DataStructure.BTree.pred?_eq_none_iff {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) :
pred? k tr = none ↔ ∀ q ∈ tr, k ≤ q.1

A well-formed tree has no predecessor of k exactly when all its keys are at least k.

theorem Cslib.DataStructure.BTree.succ?_eq_none_iff {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) :
succ? k tr = none ↔ ∀ q ∈ tr, q.1 ≤ k

A well-formed tree has no successor of k exactly when all its keys are at most k.

theorem Cslib.DataStructure.BTree.pred?_eq_some_iff {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) {p : α × β} :
pred? k tr = some p ↔ p ∈ tr ∧ p.1 < k ∧ ∀ q ∈ tr, q.1 < k → q.1 ≤ p.1

In a well-formed tree, pred? k is exactly the greatest entry strictly below k.

theorem Cslib.DataStructure.BTree.succ?_eq_some_iff {α β : Type} [LinearOrder α] {t : ℕ} {tr : BTree α β} (h : IsWF t tr) (k : α) {p : α × β} :
succ? k tr = some p ↔ p ∈ tr ∧ k < p.1 ∧ ∀ q ∈ tr, k < q.1 → p.1 ≤ q.1

In a well-formed tree, succ? k is exactly the least entry strictly above k.