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 #
slotBinM,slotBin: within-node binary search for the first key not below the query.lookupBinM,lookupBin: lookup usingslotBinMat each node.rankBinM,rankBin: rank usingslotBinMat each node.
Main results #
Cslib.IsMonadHom.map_slotBinM,Cslib.IsMonadHom.map_lookupBinM,Cslib.IsMonadHom.map_rankBinM: a monad morphism commutes with the operations.slotBin_eq_slot: on strictly increasing keys, binary and linear in-node search agree.lookupBin_eq_lookup,rankBin_eq_rank: on a tree with sorted entries, lookup and rank by binary in-node search arelookupandrank.
References #
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.
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.
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.
Lookup at a node: visit it, search its keys, test the key found for equality, and otherwise descend into the child before it.
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.
A monad morphism commutes with the within-node binary search.
A monad morphism commutes with lookup by binary in-node search.
A monad morphism commutes with rank by binary in-node search.
Pure within-node binary search: the Id instantiation of slotBinM.
Pure lookup by binary in-node search: the Id instantiation of lookupBinM.
lookupBin is the Id instantiation of lookupBinM.
Lookup by binary in-node search in the empty tree fails.
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.
Rank by binary in-node search in the empty tree is zero.
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 #
On strictly increasing keys, binary search finds the position that linear search finds. On other lists it is unspecified.
On a tree with sorted entries, lookup by binary in-node search is lookup.
On a tree with sorted entries, rank by binary in-node search is rank.