Proofs: Hybrid ternary search trie with R^2 root branching
These are the human proofs of Hybrid ternary search trie with R^2 root branching, at full length.
The hybrid TST operations keep the invariant and answer the string symbol table queries full
This proof shows: correctness.
We use the pseudocode and the invariant (H1) to (H4) of the page. For a key , its place is: if ; the value field of the cell of if ; and the TST of the cell of , with the key , if . Each string has exactly one place, fixed by its length and its first two symbols, and two different strings with the same place in a TST differ in the suffix , because they share the first two symbols.
Lemma 1 (reduction). Under (H1) to (H3), is a key with value if and only if its place holds .
Proof. For this is (H1). For , (H2) says that the cell of is NIL exactly when no key starts with , in which case is not a key, and else the cell value is the value of . For , (H2) says that the TST of the cell holds exactly when is a key with value , and if the cell is NIL then no key starts with .
GET. Lines L1 to L4 read the place of when , and return NIL when the cell is missing. Line L5 runs TST-GET on the TST of the cell, which by (H3) and the correctness of the TST search returns the value of in that TST or NIL. By Lemma 1, GET returns the value of or NIL.
PUT. Line L6 changes only , so (H1) holds for the new set of keys and (H2), (H3) do not change. For , line L7 makes sure the cell of exists; now some key starts with (namely , after this call), so (H2) holds for the existence of the cell. Line L8 sets the value of the key , and line L9 runs TST-PUT, which keeps (H3) and makes the TST hold while the other pairs stay. Each line adds 1 to exactly when was not a key before, by Lemma 1, so (H4) holds.
DELETE. Line L10 is the case of a short key, as for PUT. If the cell is NIL at line L11, no key starts with , so is not a key and nothing changes. Lines L12 and L13 clear the place of , by setting the cell value to NIL or by TST-DELETE, which keeps (H3) and removes exactly the pair of . decreases exactly when was a key. Now (H2) can fail only in its first part: the cell exists, but it may hold no key. A cell holds a key exactly when its value is not NIL or its TST is not empty, because by (H3) a nonempty TST has no dead node and so holds at least one key (a leaf of the TST has no middle child, so it has a value). Line L14 frees the cell in exactly the case where it holds no key, so (H2) holds.
KEYS-WITH-PREFIX. We show that the call on returns the keys with prefix in lexicographic order. Every key with lies, by Lemma 1, at its place.
- If , all keys with prefix start with and have length at least 2, so they are in the cell of . If the cell is NIL there are none (line L17). If , the keys are itself, when the cell value is not NIL, and for the keys of the TST, which the TST walk lists in order; comes first because it is a prefix of the others. If , the keys are for the keys of the TST with prefix , which TST-KEYS-WITH-PREFIX lists in order (line L18). Adding a common prefix keeps the order.
- If , the keys with prefix are itself and, for each symbol , the keys with prefix . Line L16 lists first, then the blocks for in increasing order, each block given by the case above. A key in the block of is smaller than a key in the block of , because they first differ at position 1. So the list is sorted.
- If , the keys are , when it is a key, and the keys with prefix for each symbol in increasing order. Line L15 lists them in this order, and the same argument on position 0 shows that the list is sorted.
LONGEST-PREFIX-OF. The keys that are prefixes of are: and , read from ; , read from the cell value; and for the keys of the cell TST that are prefixes of . The TST walk along meets exactly the nodes of the prefixes of that have a node, by the correctness of the TST search, and records the longest one with a value. Taking the longest of all these candidates gives the answer.