Definition
Suffix Tree
A suffix tree stores all suffixes of a string in one compressed trie. For of length , append a fresh end marker \\notin\SigmaU=T$$. The stored strings are
Suffixes with the same beginning share a path. Chains without branching are compressed into single edges labelled by non-empty substrings of ; outgoing edges start with distinct characters. Reading the labels from the root to leaf spells exactly .
The leaf label is the suffix’s starting position, not its length. The unique end marker prevents any suffix from being a prefix of another, so all suffixes end at distinct leaves. Children may be arranged in lexicographic order, but this order is not required by the definition.
Query
For each non-empty pattern (without the end marker):
- Follow the pattern from the root, comparing edge labels. A missing edge or mismatch means no occurrence. The pattern may end inside an edge.
- Collect the shifts by DFS through the matching subtree. Each leaf stores a suffix-start index, and occurs exactly at suffixes beginning with .
With constant-time child selection,
where counts occurrences. The DFS bound follows because every internal node below the root branches. Reported shifts need not be sorted.
Space Encoding
Let and U=T\$$. The compressed tree has n+1O(n+1)$ vertices and edges.
- Leaf: store one suffix-start index.
- Edge: store two inclusive indices representing , rather than copying the substring.
- Text: store once; edge characters are read directly from it.
This uses machine words with linear-space child storage. Explicitly copied edge labels can instead require space.
Construction Time
- Naïve: insert each suffix separately; their total length gives time.
- Linear: Weiner (1973), McCreight (1976), and Ukkonen (1995) achieve time for a constant-size alphabet.
The tree is built once and reused for every pattern. A constant-size alphabet also permits the constant-time child selection assumed in the query bound; sorted child arrays for a general alphabet add an factor to path traversal.
Examples
Suffixes of
For , append the fresh letter \$$ and build the compressed trie of the suffixes of \text{banana}$$:
The leaf labelled represents the suffix T[3..]\ = \text{ana}$$.
Substring lookup
A string occurs in iff labels a path starting at the root of the suffix tree.
For , the string occurs because the path
exists. The leaves below this path give the starting positions and .