Documentation

CslibFrontier.DataStructure.BTree.BinarySearch

Binary in-node search in classic B-trees #

lookupM and rankM scan the keys of a node from the left. This file gives the variant that searches a node by bisection: slotBinM probes the middle key and continues in the keys before or after it, and lookupBinM and rankBinM use the position it finds to stop at a key or to choose the child to descend into. The trees are those of [CLRS09], Chapter 18; the bisection and the proofs here are original to this development.

As in Lookup, each operation is defined once over a monad m, with a comparator le : α → α → m Bool and a node-visit action visit : m Unit, and the pure operations are the Id instantiations. The node's keys are held in a list, so bisection saves comparisons and nothing else: walking to the middle of the list is uncharged in the node-visit and comparison cost models. Costs are stated in Comparisons.BinarySearch.

Bisection is correct only on sorted keys. slotBin_eq_slot shows that it finds the position that the linear slot finds when the keys are strictly increasing, and lookupBin_eq_lookup and rankBin_eq_rank transfer this to trees with sorted entries, in particular well-formed ones. On other inputs the binary operations are unspecified.

The definitions use well-founded recursion and are not exposed. slotBinM_nil and slotBinM_cons, lookupBinM_leaf and lookupBinM_node, and rankBinM_leaf and rankBinM_node are their equations; simp with them evaluates the operations on concrete trees.

Main definitions #

Main results #

References #

@[irreducible]
def Cslib.DataStructure.BTree.slotBinM {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (k : α) (rest : List (Triple α β)) :
m ℕ

Within-node binary search: probe the middle key, continue in the keys before it when the query is not above it, and in the keys after it otherwise. On sorted keys the result is the position of the first key not below k, as for slotM. One comparison per probe.

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

Lookup with binary in-node search. visit fires once at each node. The search finds the position i of the first key not below k, tests that key for equality with one more comparison, and otherwise descends into the child before it, which is the i-th child when the first child is counted as the zeroth.

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

Rank with binary in-node search. visit fires once at each node. The search finds the position i of the first key not below k; the i children before the i-th child, each followed by a separator, hold only keys below k, and the search descends into the i-th child, counting the first child as the zeroth. Sizes of subtrees are uncharged client computation, as in rankM.

@[simp]
theorem Cslib.DataStructure.BTree.slotBinM_nil {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (k : α) :
slotBinM le k [] = pure 0

The search in a node without keys stops at position zero, without a comparison.

theorem Cslib.DataStructure.BTree.slotBinM_cons {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (k : α) (e : Triple α β) (l : List (Triple α β)) :
slotBinM le k (e :: l) = do let __do_lift ← le k (e :: l)[(l.length + 1) / 2].1 if __do_lift = true then slotBinM le k (List.take ((l.length + 1) / 2) (e :: l)) else do let __do_lift ← slotBinM le k (List.drop ((l.length + 1) / 2 + 1) (e :: l)) pure (__do_lift + ((l.length + 1) / 2 + 1))

The search in a node with keys probes the middle key and continues in the keys before it or after it.

theorem Cslib.DataStructure.BTree.lookupBinM_leaf {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) :

Lookup in the empty tree fails, without a visit.

theorem Cslib.DataStructure.BTree.lookupBinM_node {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) (first : BTree α β) (rest : List (Triple α β)) :
lookupBinM le visit k (first.node rest) = do visit let i ← slotBinM le k rest let hit ← match rest[i]? with | some e => le e.1 k | none => pure false if hit = true then pure (Option.map Triple.val rest[i]?) else match (first.node rest).children[i]? with | some c => lookupBinM le visit k c | none => pure none

Lookup at a node: visit it, search its keys, test the key found for equality, and otherwise descend into the child before it.

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

Rank in the empty tree is zero, without a visit.

theorem Cslib.DataStructure.BTree.rankBinM_node {α β : Type} {m : Type → Type w} [Monad m] (le : α → α → m Bool) (visit : m Unit) (k : α) (first : BTree α β) (rest : List (Triple α β)) :
rankBinM le visit k (first.node rest) = do visit let i ← slotBinM le k rest let below ← match (first.node rest).children[i]? with | some c => rankBinM le visit k c | none => pure 0 pure ((List.map (fun (x : BTree α β) => x.size + 1) (List.take i (first.node rest).children)).sum + below)

Rank at a node: visit it, search its keys, count the children and separators before the position found, and descend into the child at that position.

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

A monad morphism commutes with the within-node binary search.

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

A monad morphism commutes with lookup by binary in-node search.

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

A monad morphism commutes with rank by binary in-node search.

def Cslib.DataStructure.BTree.slotBin {α β : Type} [LinearOrder α] (k : α) (rest : List (Triple α β)) :

Pure within-node binary search: the Id instantiation of slotBinM.

def Cslib.DataStructure.BTree.lookupBin {α β : Type} [LinearOrder α] (k : α) (tr : BTree α β) :

Pure lookup by binary in-node search: the Id instantiation of lookupBinM.

def Cslib.DataStructure.BTree.rankBin {α β : Type} [LinearOrder α] (k : α) (tr : BTree α β) :

Pure rank by binary in-node search: the Id instantiation of rankBinM.

theorem Cslib.DataStructure.BTree.slotBin_def {α β : Type} [LinearOrder α] (k : α) (rest : List (Triple α β)) :

slotBin is the Id instantiation of slotBinM.

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

lookupBin is the Id instantiation of lookupBinM.

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

rankBin is the Id instantiation of rankBinM.

@[simp]

Lookup by binary in-node search in the empty tree fails.

theorem Cslib.DataStructure.BTree.lookupBin_node {α β : Type} [LinearOrder α] (k : α) (first : BTree α β) (rest : List (Triple α β)) :
lookupBin k (first.node rest) = if Option.any (fun (e : Triple α β) => decide (e.1 ≤ k)) rest[slotBin k rest]? = true then Option.map Triple.val rest[slotBin k rest]? else (first.node rest).children[slotBin k rest]?.bind (lookupBin k)

Lookup by binary in-node search at a node: stop at the key found by the search when it equals the query, and otherwise descend into the child before it.

@[simp]

Rank by binary in-node search in the empty tree is zero.

theorem Cslib.DataStructure.BTree.rankBin_node {α β : Type} [LinearOrder α] (k : α) (first : BTree α β) (rest : List (Triple α β)) :
rankBin k (first.node rest) = (List.map (fun (x : BTree α β) => x.size + 1) (List.take (slotBin k rest) (first.node rest).children)).sum + (Option.map (rankBin k) (first.node rest).children[slotBin k rest]?).getD 0

Rank by binary in-node search at a node: the children before the position found by the search, each with its separator, and the rank in the child at that position.

Agreement with linear search on sorted keys #

theorem Cslib.DataStructure.BTree.slotBin_eq_slot {α β : Type} [LinearOrder α] (k : α) {rest : List (Triple α β)} (hs : List.Pairwise (fun (x1 x2 : α) => x1 < x2) (List.map (fun (x : Triple α β) => x.1) rest)) :
slotBin k rest = slot k rest

On strictly increasing keys, binary search finds the position that linear search finds. On other lists it is unspecified.

theorem Cslib.DataStructure.BTree.lookupBin_eq_lookup {α β : Type} [LinearOrder α] {tr : BTree α β} (h : tr.toList.IsSortedAssoc) (k : α) :
lookupBin k tr = lookup k tr

On a tree with sorted entries, lookup by binary in-node search is lookup.

theorem Cslib.DataStructure.BTree.rankBin_eq_rank {α β : Type} [LinearOrder α] {tr : BTree α β} (h : tr.toList.IsSortedAssoc) (k : α) :
rankBin k tr = rank k tr

On a tree with sorted entries, rank by binary in-node search is rank.