3.8.8 · D5String Algorithms
Question bank — Suffix tree (conceptual)
This bank sharpens your model of the suffix tree: what its rules actually guarantee, where beginners slip, and what happens at the weird edges (empty string, one letter, repeated letters, the $ marker).
True or false — justify
Every "answer" below is a reason, never a bare yes/no.
TF1. Every internal node (except the root) has at least two children.
True. That is rule 2 of the definition — internal nodes are branching points; a node with a single child would carry no decision and gets compressed away.
TF2. A suffix tree of a length- string can have more than total nodes.
False. With leaves and each internal node having children, internal nodes , so total .
TF3. Storing the edge label anana$ costs 6 characters of memory.
False. Labels are stored as an ==index pair == meaning — just two integers — so the memory is constant per edge regardless of label length.
TF4. Without the $ marker, a suffix tree of banana still has exactly leaves.
False. Without
$, a is a prefix of ana, so a ends inside an edge, not at its own leaf — some suffixes hide inside others and you get fewer than leaves.TF5. Two edges leaving the same node may both start with the letter a.
False. Rule 4 forbids it — distinct first characters keep the walk deterministic, so matching a pattern picks at most one edge per step.
TF6. Every substring of corresponds to some path starting at the root (not necessarily ending at a node).
True. A substring is a prefix of some suffix, and reading a prefix of a suffix means walking part-way down that suffix's root-to-leaf path — you may stop in the middle of an edge.
TF7. Every path from the root to a leaf spells a distinct suffix.
True. The
$ marker guarantees each suffix ends at its own leaf, and no suffix equals another, so the leaves label distinct suffixes.TF8. The number of leaves in the subtree below a matched pattern equals the number of times that pattern occurs.
True. Each leaf is one suffix that starts with , and a suffix starting with is exactly one occurrence of — so leaf count .
TF9. A suffix tree and a suffix trie store the same information.
True. Compression only removes non-branching intermediate nodes; every suffix and every substring is still spelled by exactly one path — no information is lost.
TF10. The deepest leaf (by character depth) tells you the longest repeated substring.
False. You want the deepest internal (branching) node, not the deepest leaf. Branching means continuations, hence occurrences; a leaf is a single suffix and need not repeat.
Spot the error
Each line states a flawed claim; the reveal names the flaw.
SE1. "Searching for pattern takes time because the tree has nodes."
The flaw: search cost depends on , not . You read 's characters walking down, so it is — independent of .
SE2. "The suffix tree is because summing all edge-label lengths gives ."
The flaw: labels are never copied as text. Each edge holds two integers ; with edges that is space — the text lives once in .
SE3. "Every node in the tree has at least two children."
The flaw: the root is exempt (it can have one child, e.g.
aaaa$), and leaves have zero children. The "" rule is only for internal non-root nodes.SE4. "Because a occurs 3 times in banana, there must be an internal node whose path string is a with 3 children."
The flaw: leaf count, not child count, gives occurrences. The node for
a needs enough leaves below it to total 3 — those may sit under further branching, not as 3 direct children.SE5. "Compression can merge two edges even if they leave the same node."
The flaw: compression only collapses a chain of single-child nodes into one edge. Sibling edges are never merged — they represent different branching choices.
SE6. "To count occurrences of we count the internal nodes below the match."
The flaw: occurrences are counted by leaves below the match point, not internal nodes. Each leaf is one starting position of .
SE7. "The $ must be an ordinary letter of the alphabet so it fits in existing edges."
The flaw:
$ must be a symbol not in the alphabet and unique. If it were an ordinary letter it couldn't force each suffix to a distinct leaf.SE8. "A suffix tree lets you search any pattern; therefore it stores all substrings explicitly as strings."
The flaw: substrings are stored implicitly as root paths, not explicitly. There are substrings but only nodes — you read them off paths on demand.
Why questions
WQ1. Why is a tree used instead of just a sorted list of suffixes?
Many suffixes share prefixes (
ana, anana), and a tree merges those shared beginnings into one path, so matching is instead of scanning every suffix.WQ2. Why must every suffix end at a leaf rather than inside an edge?
So that leaf count cleanly equals occurrence count and every suffix has a unique endpoint — the
$ marker enforces this by making no suffix a prefix of another.WQ3. Why does a branching node (not any node) mark a repeated substring?
A branch means the path string has different next-characters, so that string is followed by two different things, i.e. it appears in at least two places in .
WQ4. Why store edge labels as index pairs instead of the substrings themselves?
Copying substrings sums to characters; two integers per edge keeps total space at while still pointing into the single stored copy of .
WQ5. Why is "substring of = prefix of some suffix of " the founding idea?
Any contiguous chunk of begins at some position and is exactly the front of suffix ; so a structure holding all suffixes automatically holds all substrings as their prefixes.
WQ6. Why must edges from one node begin with distinct characters?
It makes the walk deterministic — given the next character of , at most one edge can match, so you never branch-search and stay at .
WQ7. Why can a suffix trie have nodes while the suffix tree has only ?
The trie keeps every single-character step as its own node; the tree compresses non-branching chains, and with leaves the branching structure allows at most internal nodes.
Edge cases
EC1. What does the suffix tree of the empty string (with $) look like?
Just a root and one leaf reached by the edge
$ — there is exactly suffix (the empty suffix, i.e. just $), so a single leaf.EC2. What is the shape for a single distinct letter repeated, e.g. aaa$?
A skewed tree: the root has one branch on
a and one on $; along the a side each further step branches into "more as" versus "$". It is the worst case for depth — the longest repeated substring aa sits at the deepest branching node.EC3. For a string of all distinct characters, e.g. abcd$, how many internal nodes are there?
Only the root — no two suffixes share a first character, so nothing branches below the root; you get leaves hanging directly off it and zero non-root internal nodes.
EC4. Can the root ever be a leaf?
No — the root is where all suffixes begin; it always has children (unless the string is truly empty of everything, which we don't allow since we always append
$).EC5. If a pattern matches only part-way into an edge and then ends, is still a substring?
Yes — reaching depth anywhere, including mid-edge, means was spelled successfully; you need not land exactly on a node to conclude occurs.
EC6. What happens if is longer than any suffix (longer than )?
The walk runs out of characters to match before finishing , so is not a substring — you fall off the tree, which correctly reports zero occurrences.
EC7. In banana$, why does the deepest branching node give ana and not anana?
anana occurs only once (it does not branch — a single continuation to a leaf), while ana is followed by two different things (na$ and $), so ana branches and is the longest string that repeats.EC8. For a string with no repeats at all, what is the longest repeated substring?
The empty string — the only branching node is the root at depth 0, so the "deepest branch" has path length 0, correctly meaning nothing repeats.
Connections
- Suffix tree (conceptual) — the parent concept these traps stress-test.
- Trie — the uncompressed structure many of these errors confuse with the tree.
- Suffix Array — the memory-lighter cousin; contrast its leaf-ordering idea.
- Ukkonen's Algorithm — how the tree is actually built in .
- Longest Common Substring — extends the "deepest branching node" trap to two strings.