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 #
predM,pred?: the entry with the greatest key strictly below the query.succM,succ?: the entry with the least key strictly above the query.
Main results #
Cslib.IsMonadHom.map_predM,Cslib.IsMonadHom.map_succM: a monad morphism commutes with the queries.pred?_eq_of_isSortedAssoc,succ?_eq_of_isSortedAssoc: on a tree with sorted entries, the queries areSortedAssoc.pred?andSortedAssoc.succ?ontoList;pred?_eqandsucc?_eqspecialize them to well-formed trees.pred?_eq_some_iff,succ?_eq_some_iff: on a well-formed tree, the greatest entry below and the least entry above the query.
References #
Ported from cslib-community/EpsilonEridani at commit 1966ab476:
EpsilonEridani/Algorithms/Lean/BTree/PredSucc.lean.
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.
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.
A predecessor scan either descends before the first separator or continues with that separator as a fallback, without an additional visit.
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.
A monad morphism commutes with the predecessor search.
A monad morphism commutes with the successor search.
Pure strict predecessor: the Id instantiation of predM with a trivial visit action.
Pure strict successor: the Id instantiation of succM with a trivial visit action.
The empty tree has no predecessor.
The predecessor lies before the first separator at least k; a smaller separator is a
fallback for the search in the remaining suffix.
The empty tree has no successor.
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.
Model equation for strict predecessor on well-formed trees.
Model equation for strict successor on well-formed trees.
On a tree with sorted entries, there is no predecessor exactly when every key is at least the query.
On a tree with sorted entries, there is no successor exactly when every key is at most the query.
On a tree with sorted entries, the predecessor is exactly the greatest entry whose key is strictly below the query.
On a tree with sorted entries, the successor is exactly the least entry whose key is strictly above the query.