3.8.8 · D3String Algorithms

Worked examples — Suffix tree (conceptual)

3,838 words17 min readBack to topic

This page is the "put it into your hands" companion to Suffix tree (conceptual). We walk every kind of question a suffix tree can face — from a normal substring search to the weird degenerate cases (empty pattern, single repeated letter, pattern that ends inside an edge) — and solve each one fully.


The scenario matrix

Every question a suffix tree throws at you falls into one of these case classes. Our job is to hit every row.

# Case class What makes it tricky Covered by
A Pattern present, ends at a node clean walk, stop on a branching point Ex 1
B Pattern present, ends inside an edge you stop mid-label, not at a node Ex 2, Ex 2b
C Pattern absent — mismatch mid-edge wrong letter inside a compressed edge Ex 3
D Pattern absent — no matching edge at a node no child starts with the next letter Ex 3
E Counting occurrences (leaves below) must count subtree leaves, not stop point Ex 4
F Degenerate: empty pattern / single char zero-length, whole-alphabet answers Ex 5
G Degenerate: all-same string aaaa$ maximal sharing, deepest-chain repeats Ex 6
H Limiting: longest repeated substring deepest internal node by char depth Ex 7
I Real-world word problem DNA / log search framing Ex 8
J Exam twist: why the $ changes leaf count reasoning, not walking Ex 9

Here is the tree we will read from for banana$. Study it before the examples — everything below "looks at" this picture.

Figure — Suffix tree (conceptual)

Ex 1 — Case A: pattern present, ends at a node

Figure — Suffix tree (conceptual)

Ex 2 — Case B: pattern present, ends inside an edge


Ex 2b — Case B at the very first edge (the simplest mid-edge match)


Ex 3 — Cases C & D: two ways to be absent


Ex 4 — Case E: counting occurrences


Ex 5 — Case F: degenerate patterns (empty and single char)


Ex 6 — Case G: the all-same string aaaa

Figure — Suffix tree (conceptual)

Ex 7 — Case H: longest repeated substring (limiting query)

Figure — Suffix tree (conceptual)

Ex 8 — Case I: real-world word problem


Ex 9 — Case J: exam twist (reason, don't walk)


Recall Self-test on the matrix

Which case class does each hit? nan in banana ::: Case B — present, ends inside an edge. b in banana ::: Case B — present, ends inside the very first (root) edge. bx in banana ::: Case C — mismatch strictly inside a compressed edge. nax in banana ::: Case D — no matching child while standing on a node. Occurrences of empty string in a length- string ::: Case F — degenerate, answer . Longest repeated substring of banana ::: Case H — ana, deepest internal node, depth 3. Leaves of the tree for abab$ ::: Case J — exactly 5 (). What is ? ::: The length of the original string (before the $ is appended).


Connections

  • Suffix tree (conceptual) — the parent concept these examples exercise.
  • Trie — where the uncompressed walk logic comes from.
  • Suffix Array — same queries with less memory; occurrences become a range.
  • Ukkonen's Algorithm — how the tree we read from is built in .
  • Longest Common Substring — extends Ex 7 to two strings.