3.8.8 · D4String Algorithms

Exercises — Suffix tree (conceptual)

2,437 words11 min readBack to topic

We will build ONE reference tree and reuse it. Below is the suffix tree of banana$ — keep looking back at it.

Figure — Suffix tree (conceptual)

Level 1 — Recognition

Goal: read the tree and recite the definitions correctly.

L1.1

List all suffixes of banana$ with their start indices.

Recall Solution

Chop the front letter off repeatedly, keeping the $ on the tail:

index suffix
0 banana$
1 anana$
2 nana$
3 ana$
4 na$
5 a$
6 $

That is suffixes. Why 7 and not 6? the lone $ is itself a suffix.

L1.2

How many leaves does the suffix tree of banana$ have, and why exactly that many?

Recall Solution

Exactly ==== leaves — one per suffix. The $ guarantees no suffix is a prefix of another, so each suffix ends at its own leaf instead of stopping partway down an edge.

L1.3

True/False: every internal node of a suffix tree (other than the root) has at least 2 children.

Recall Solution

True. A node with only one child carries no decision — during compression it gets swallowed into a single edge. So every surviving internal node is a branching point with children.


Level 2 — Application

Goal: run the standard walks (search, count) on the tree.

L2.1

Does nan occur in banana? Trace the walk.

Recall Solution

Start at the root. The first edge whose label starts with n is the one labelled na. Walk it:

  • match n ✔, match a ✔ — we are now at the branching node after na.
  • next character of is n. From that node one outgoing edge starts with n (the na$ edge). Match n ✔.

We spelled all of nan. Yes, nan is a substring. Cost character-steps.

L2.2

Count the occurrences of ana in banana.

Recall Solution

Walk ana. This lands you exactly at the branching node whose root-path is ana (look at the yellow node in the figure). Now count the leaves in its subtree. Below ana hang two leaves: one continues na$ (leaf 1, suffix anana$) and one continues $ (leaf 3, suffix ana$). So ana occurs 2 times — at start indices and . Check: b(anana) → positions 1 and 3, and indeed banana = b an an a, ana sits at index 1 (anana) and index 3 (ana). ✔

L2.3

Does nana$ reach a leaf, and which start index does it identify?

Recall Solution

Walk n,a,n,a,$. You end at a leaf, leaf number — because the full suffix nana$ starts at index . Reaching a leaf (not stopping mid-edge) means you spelled an entire suffix.


Level 3 — Analysis

Goal: reason about structure, depth, and why the bounds hold.

L3.1

What is the longest repeated substring of banana, and how does the tree reveal it?

Recall Solution

Find the deepest internal (branching) node measured by string-depth. Branching means different continuations exist, so its root-path string occurs times. In our tree the deepest branching node has string-depth 3, spelling ana. Therefore the longest repeated substring is ana (it appears at indices 1 and 3, overlapping). Length .

L3.2

A suffix tree for a string of length (over the terminated string, so leaves) has at most how many internal nodes, and hence at most how many total nodes? Derive it.

Recall Solution

A rooted tree with leaves in which every internal node has children has at most internal nodes. Here , so internal nodes . Total nodes . Why the -children rule matters: if single-child chains survived, the count would balloon. Compression enforces the rule, capping the tree at linear size.

L3.3

Explain why the suffix tree occupies space even though the sum of all edge-label string lengths is .

Recall Solution

Each edge stores its label as two integers meaning "the substring " — never the copied text. There are edges (one per non-root node), and integers per edge, giving words of storage. The conceptual label may be long (like nana$), but we only ever keep the two endpoints into .


Level 4 — Synthesis

Goal: combine ideas and design small structures.

L4.1

Draw (describe) the top-level edges out of the root of banana$. What are the first characters, and how many root edges are there?

Recall Solution

Rule 4: no two edges out of a node start with the same character. The distinct first characters among the suffixes of banana$ are b, a, n, $. So the root has exactly 4 children:

  • banana$ → leaf 0 (starts with b, unique, so one long edge to a leaf).
  • a… → branching node (suffixes anana$, ana$, a$ all start with a).
  • na… → branching node (nana$, na$).
  • $ → leaf 6.

L4.2

Consider aaaa$ (length ). How many leaves does its suffix tree have, and what is its longest repeated substring?

Recall Solution

Suffixes: aaaa$(0), aaa$(1), aa$(2), a$(3), $(4) → leaves. Longest repeated substring: the deepest branching node. The string aaa occurs at indices 0 and 1 (twice), and aaaa occurs once. So the longest repeat is aaa, length . (For a run of identical letters, the longest repeat is that run minus one letter.)

L4.3

You are given the suffix tree of . Design an test that answers "is a substring of ?" — and state one edge case where a naive tester breaks.

Recall Solution

Algorithm: start at root, current position = beginning of . Repeatedly: pick the outgoing edge whose label's first character equals 's current character (at most one exists, by rule 4); walk along that edge comparing character-by-character. If any comparison fails, or you run off the end of an edge with no matching next edge, answer no. If you exhaust all of (whether you stop mid-edge or at a node), answer yes. Cost: you read each character of at most once → , independent of . Edge case that breaks naive testers: stopping inside an edge. nan ends in the middle of an edge but is still a valid substring. A tester that only says "yes" when it lands exactly on a node would wrongly reject nan. Fix: "yes" whenever is fully consumed, node or mid-edge.


Level 5 — Mastery

Goal: transfer to related structures and defend a design choice.

L5.1

Two occurrences of ana in banana overlap (indices 1 and 3, but ana spans 1–3 and 3–5). Does the suffix-tree occurrence count include overlapping matches? Justify from the leaf definition.

Recall Solution

Yes, overlaps are counted. The count = number of leaves below the match = number of suffixes that start with . Suffix anana$ (index 1) and suffix ana$ (index 3) both start with ana, so both leaves are counted — regardless of whether the spans overlap in . The tree counts starting positions, and overlapping matches have distinct starting positions. Answer for ana: 2.

L5.2

Compare, in one line each, when you'd reach for a suffix tree versus a Suffix Array versus running KMP Algorithm for a single search.

Recall Solution
  • KMP Algorithm: one pattern, one text, scanned once — , tiny memory. Use when you search a known text once.
  • Suffix Array: same substring power as a suffix tree but a fraction of the memory; search is . Use when the text is fixed and queried many times and memory matters.
  • Suffix tree: richest structure — search, longest-repeat, Longest Common Substring via a generalized tree — but heaviest memory (large constant). Use when you need those advanced queries and can afford the space.

L5.3

Using the " internal nodes" bound, prove that the total number of edges in the suffix tree of banana$ is at most , and give the exact edge count for our tree.

Recall Solution

In any rooted tree, #edges #nodes . We proved total nodes , so edges . For banana$ (): the actual tree has leaves plus internal nodes (root, the a-branch node, the na-branch node… plus the deeper ana branch) — count them in the figure. Nodes ? Let's count precisely: leaves = 7; internal nodes = root, node after a , node after ana, node after na = 4. Total , edges , comfortably . ✔


Active Recall

How many leaves in the suffix tree of a length- string (terminated)?
Exactly .
occ of equals the number of what?
Leaves in the subtree below the point where the last character of was matched.
Longest repeated substring corresponds to which node?
The deepest internal (branching) node, by string-depth.
Why is a leaf's string never a repeated substring?
It is a full suffix ending in $, so it occurs exactly once.
Root children count equals what?
The number of distinct first characters among the suffixes.
Does the suffix tree count overlapping occurrences?
Yes — it counts distinct starting positions.

Connections

  • Suffix tree (conceptual) — the parent concept these exercises drill.
  • Trie — the uncompressed structure behind L1/L3 counting.
  • Suffix Array — the memory-lean alternative compared in L5.
  • Ukkonen's Algorithm — how the tree is actually built in .
  • KMP Algorithm — the single-search contrast in L5.
  • Longest Common Substring — the generalized-tree application referenced in L5.
  • Burrows-Wheeler Transform — related suffix structure.