Proofs: Suffix tree
These are the human proofs of Suffix tree, at full length.
The naive build gives the suffix tree, and LOCUS finds exactly the substrings full
This proof shows: correctness, termination.
Let , where occurs only at index , and . For a node , is the concatenation of the labels from the root to . Say that a string is spelled if is a prefix of for some node ; then the position where ends is unique, at a node or inside an edge, when (T1) holds.
Lemma 1 (no suffix is a prefix of another). For , is not a prefix of . Proof. has its at offset , and has at offset the symbol with , which is not .
Claim. After the pass of line L1 for , the tree satisfies (T1), (T3), and (T2') the leaves are exactly one leaf for each , with ; moreover, the spelled strings are exactly the prefixes of .
Proof by induction on . Before the first pass the tree is the root alone; it spells only the empty string, and (T1), (T3) hold.
Consider the pass for . The loop keeps: , so it is spelled. At line L3, if has no child edge with first symbol , then is not spelled, and line L4 adds a leaf whose label is nonempty (as ), with a new first symbol; the new leaf has . Otherwise line L6 matches symbols of the label of . If , the whole label matches, , and the loop goes on with . This cannot go on past with the label ending at : if equalled for an existing node , then would be a prefix of some , , by the induction hypothesis, which Lemma 1 forbids. So line L6 compares at most to the end of , and when , the symbols and exist and differ. Line L8 splits the edge into a node with and label , nonempty because the first symbols agree (). Then has two children, with the label and the new leaf with label ; their first symbols differ.
So (T1) holds after the pass: labels are nonempty, first symbols at each node are distinct, the new inner node has two children and other nodes lose no children. (T3) holds since depths are set as the parent depth plus the label length, and the split does not change of any old node. The spelled strings are the old ones plus the prefixes of , since the new leaf extends the spelled prefix of to all of it. The new leaf is the only new leaf, so (T2') holds. Each pass ends: increases by in each repetition of line L7 (labels are nonempty) and stays at most .
The final tree. After , (T2') is (T2). The spelled strings are the prefixes of the suffixes of , which are the substrings of . An inner node has two children with different first symbols , so and are both substrings of , and is right-branching. Conversely, if is a substring of followed in by two different symbols , the paths of and separate exactly where ends, which by (T1) can only be at a node.
LOCUS. By (T1), at each node at most one child edge starts with a given symbol, so the walk of lines L10 to L12 follows the unique path that spells if is spelled, and returns NIL at the first symbol where no continuation exists. Since does not contain , is spelled if and only if it is a prefix of some with , that is, if and only if occurs in . When LOCUS returns , is a prefix of and is a proper prefix of , so a node has as a prefix of its if and only if it is or below . So the leaves below are exactly the leaves whose starts with . By (T2), these are the suffixes that start with , which are the occurrences. Excluding the leaf , which starts with , changes nothing for a nonempty and gives the suffixes of for the empty .
Size. The tree has leaves. Each inner node other than the root has at least two children, so by counting edges, the number of inner nodes is at most the number of leaves minus 1 when the root has at least two children, which holds for (it has the children and ). So there are at most nodes for , and 2 nodes for .