Comparison costs of B+-tree point queries #
The point queries of Cslib.DataStructure.BPlusTree are defined once over a monad, with a
comparator and a node-visit action. This file instantiates them in CSLib's query-complexity
framework, at FreeM (LEQuery α) with the comparison query LEQuery.ask and the trivial
visit := pure (), so that every key comparison is a query and node visits are free: lookupQ,
containsQ, minQ, maxQ, predQ, succQ and rankQ. Counting is structural: a program
parametric in the oracle cannot compare keys without asking.
Applied to FreeM.isMonadHom_pure_eval, the transfer lemmas map_lookupM and its siblings give
eval_lookupQ and its siblings: under BTree.orderOracle, the oracle of the order on the keys,
each query evaluates to the pure query. The minimum and maximum take no comparator, so they
evaluate to min? and max? under every oracle.
The constants #
A leaf has height 1, so a descent in a tree of height h passes h - 1 interior nodes and
ends in one leaf.
- At an interior node the search scans the separators, asking one comparison for each separator
it passes and one where it stops: at most one per separator (
countQueries_slotM_leis the same count for the separator search on its own). Separators are not entries, so there is no equality test. - In a leaf, lookup asks one comparison per entry up to the first key that is not below the query and then one more to test that key for equality: at most one per entry and one more. Rank, predecessor and successor ask at most one per entry.
- The minimum and maximum ask nothing, and the sibling fallback of the predecessor and the successor is a maximum or minimum search, so it adds nothing to the descent.
The root and every other node of a well-formed tree have at most 2 * t - 1 separators or
entries. le_mul_height_add_of_scan turns the per-node counts into (2 * t - 1) * height + 1
for lookup and the membership test and (2 * t - 1) * height for rank, predecessor and
successor. The bounds hold under every oracle, since the shape of the tree does not depend on
the answers; only the values returned depend on the oracle being that of the order.
The lookup and rank bounds are attained: the test module
CslibFrontierTests.BPlusTreeComparisons has a tree of height 2 at t = 2, full along its
rightmost path, on which looking up the greatest key asks 7 comparisons and the rank of a key
above all others asks 6.
The classic B-tree's bound is 2 * t * height for all five queries
(BTree.countQueries_lookupQ_le): there a node holds entries, lookup tests for equality at
every node, and the empty leaf is free. Here the equality test happens once, in the leaf, which
is a level of its own.
Main definitions #
lookupQ,containsQ,minQ,maxQ,predQ,succQ,rankQ: B+-tree point queries counting comparisons.WFInput: a well-formed tree with a query key, the input of theUpperBoundstatements.
Main results #
eval_lookupQ,eval_containsQ,eval_predQ,eval_succQ,eval_rankQ: the value of each query at the order's oracle is that of the pure query.eval_minQ,eval_maxQ,countQueries_minQ,countQueries_maxQ: under every oracle the minimum and maximum searches returnmin?andmax?and ask nothing.countQueries_lookupQ_le,countQueries_containsQ_le: at most(2 * t - 1) * height + 1comparisons on a well-formed tree, under every oracle.countQueries_rankQ_le,countQueries_predQ_le,countQueries_succQ_le: at most(2 * t - 1) * height.lookupQ_upperBoundand its siblings: the same bounds asCslib.Query.UpperBoundover well-formed inputs.
References #
- T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein, Introduction to Algorithms, Chapter 18. Section 18.2 there analyses B-TREE-SEARCH on classic B-trees; the counts here are for B+-trees and are not taken from a source.
Lookup, issuing one query per key comparison.
Membership test, issuing one query per key comparison.
Least entry in the comparison model, where it asks nothing.
Greatest entry in the comparison model, where it asks nothing.
Strict predecessor, issuing one query per key comparison.
Strict successor, issuing one query per key comparison.
Rank, issuing one query per key comparison.
Transfer: eval at the order's oracle is the pure operation #
Under any oracle, minQ evaluates to min?.
Under any oracle, maxQ evaluates to max?.
Under the order's oracle, lookupQ evaluates to lookup.
Under the order's oracle, containsQ evaluates to contains.
Under the order's oracle, predQ evaluates to pred?.
Under the order's oracle, succQ evaluates to succ?.
Under the order's oracle, rankQ evaluates to rank.
Comparison counts #
The within-node separator search makes at most one comparison per separator.
The minimum search makes no comparison.
The maximum search makes no comparison.
The membership test makes as many comparisons as lookup.
Comparison bounds #
Lookup in a well-formed tree makes at most 2 * t - 1 comparisons per level and one more,
under every oracle.
The membership test in a well-formed tree makes at most 2 * t - 1 comparisons per level and
one more, under every oracle.
Predecessor search in a well-formed tree makes at most 2 * t - 1 comparisons per level,
under every oracle.
Successor search in a well-formed tree makes at most 2 * t - 1 comparisons per level,
under every oracle.
Rank in a well-formed tree makes at most 2 * t - 1 comparisons per level, under every
oracle.
Upper bounds over all oracles #
A well-formed B+-tree of minimum degree t and a query key: the inputs of the bounded point
queries.
Lookup makes at most 2 * t - 1 comparisons at each of height levels, and one more.
The membership test makes at most 2 * t - 1 comparisons at each of height levels, and
one more.
Predecessor search makes at most 2 * t - 1 comparisons at each of height levels.
Successor search makes at most 2 * t - 1 comparisons at each of height levels.
Rank makes at most 2 * t - 1 comparisons at each of height levels.