Predecessor and successor queries in B+-trees #
predM and succM return the entry with the greatest key strictly below the query, respectively
the least key strictly above it. Each follows the search of lookupM to a leaf and scans it with
Cslib.SortedAssoc.predM, respectively Cslib.SortedAssoc.succM.
The leaf that the search reaches need not hold the answer. Every key of that leaf may be at
least the query, and then the predecessor is the last entry of the leaf before it; every key may
be at most the query, and then the successor is the first entry of the leaf after it. The
operations here handle this by a sibling fallback: at the node where the descent into a child
yields nothing, they take the greatest entry of the child before it, respectively the least entry
of the child after it, with maxM and minM. In a well-formed tree that sibling is not empty,
so the fallback runs at most once, and the walk it adds visits at most one node on each level
below that node.
An implementation with chained leaves would instead step from the leaf to its neighbour. Modelling that, by passing the neighbouring leaf down the descent and charging one visit when it is consulted, is left as future work.
Main definitions #
Main results #
Strict predecessor. Pass the separators that are at most k and descend into the child
introduced by the last of them; in the leaf, scan the entries. Where the part of a node after a
separator yields nothing, return the greatest entry of the child before that separator. visit
fires once at each node on the path, and once at each node of the fallback walk.
Strict successor. Pass the separators that are at most k and descend into the child
introduced by the last of them; in the leaf, scan the entries. Where the child before a separator
yields nothing, return the least entry of the child that the separator introduces. visit fires
once at each node on the path, and once at each node of the fallback walk.
A monad morphism commutes with the predecessor query.
A monad morphism commutes with the successor query.
Pure strict predecessor: the Id instantiation of predM with visit := pure ().
Pure strict successor: the Id instantiation of succM with visit := pure ().
The predecessor in a leaf is the scan of its entries.
The predecessor at a node's first separator: when it is at most the query, look after it and fall back on the greatest entry of the child before it; otherwise descend into that child.
The successor in a leaf is the scan of its entries.
The successor at a node's first separator: when it is at most the query, look after it; otherwise descend into the child before it and fall back on the least entry of the child that the separator introduces.
Model equation of the predecessor on well-formed trees.
Model equation of the successor on well-formed trees.