Proofs: String B-tree
These are the human proofs of String B-tree, at full length.
Blind search, one comparison and the passed lcp locate a pattern in the String B-tree full
This proof shows: correctness, complexity.
We use the pseudocode, the representation and the invariant (S1) to (S4) of the page. Strings are compared lexicographically, with the end of a string below every symbol; is the length of the longest common prefix, and for is the end symbol. For sorted strings and , (Fact 0, the standard property of sorted strings).
Lemma 1 (blind search). Lines L1 and L2 return a key with .
Proof. Let . We show that the range kept by the loop always contains a key with . It holds at the start. Take a range with this property, and let be the minimum of in it, so all its keys share their first symbols (Fact 0), and the parts after the split are the groups of keys with the same symbol at position , with distinct symbols. If , all keys of the range have the same lcp with , namely for any of them, which is ; so every part keeps the property. If , a best key agrees with at position , so is the symbol of its part, and line L2 keeps that part. When the range has one key, it is a best key.
Lemma 2 (the order from one comparison). Let be a key with maximal among the keys. Then for every key , , and line L4 decides correctly.
Proof. Let . If : and agree up to , so and first differ at , with the same symbols as and ; thus and exactly when , that is, when comes before in the node. If : agrees with beyond , so and compares with like does, by against . If : and, by the maximality of , ; then is decided by against , and is a branching symbol of the trie: if is after , it is for the last with (the keys from to share more than symbols, by Fact 0), and symmetrically for the first with if is before . The two symbols and are equal only if both are the end symbol, and then , which counts as .
Lemma 3 (the passed lcp). In RANK, the value passed to a node is at most over the keys of that node, so line L3, which starts reading at , computes correctly.
Proof. At the root . At an internal node , if , then by (S3) the keys of are (first and last strings of the children), and exactly are . So . The value passed is , computed exactly by Lemma 2. Both and are keys of child : for a leaf they are its first and last strings, and for an internal child they are the first key of its first child and the last key of its last child, by (S3) applied to the child. So is at most the maximum over the child's keys. By Lemma 1 the blind search in the child returns a key with at least that maximum, hence at least , and the symbols before need not be read.
RANK is correct. We show that the value returned is the number of stored strings . At a node reached with equal to the number of strings in the leaves to the left of 's subtree, all of which are , and with all strings to the right of the subtree : by Lemma 2, is the number of keys of that are . If is a leaf, its keys are its strings, so the answer is . Otherwise, let . If , then , so by (S2) all strings of children are and all strings of children are ; the answer is plus their counts. If , then : the children before are all , those after are all , and the descent into child keeps the claim. Lemma 3 makes each comparison correct. The loop ends at a leaf at the latest.
PREFIX-RANGE. With above every symbol, a string is exactly when or is a prefix of . So the strings in the returned range of ranks are those with prefix , which by sorted order are exactly the strings with prefix .
INSERT keeps the invariant. The descent finds a leaf such that inserting at its rank in the leaf keeps the global order (S2): if falls inside the range of child , it must go into that child; if it falls between children and , the end of child (or the start of child 0) is a valid place. Line L9 inserts it at its rank among the leaf's strings, computed by Lemma 2. Line L10 restores the B-tree sizes (S1) by splitting an overfull child into two halves of at least entries, in order, and recomputes the boundary keys (S3), the counts and the tries (S4) of every node on the path. Nodes off the path keep their strings, hence their boundaries and tries. Line L11 adds a level only at the root, so all leaves keep the same depth.
The number of symbols read. Let be the value of at the -th level of a search and the value passed to it. Lemma 3 gives , and since and are the neighbors of among the keys of the parent, the best key of the parent is one of them, so . The comparison at level reads matching symbols and at most one mismatching symbol. Summing, the search reads at most symbols for a tree of height .