Suffix Trie and Suffix Tree

Context: FIT2004_MOC · a Trie of the suffixes of one string instead of words — every substring becomes a root path · PT-03’s drawing-and-counting subject Parent Framework: Trie

Quick Revision

  • 🎯 Objective: insert every suffix of S$ ➔ substring prefix of a suffix, so substring queries are trie walks; compress every one-child chain into one [start,end] edge ➔ nodes.
  • 📦 Core Components: Trie of suffixes ➔ worst space | suffix tree ➔ leaves, every internal node branches, nodes, space only with index labels | both to build naively.
  • ⚡ Key Constraint: PT-03 counts die on three omissions — the root is a node, $ alone is a suffix, every suffix ends in $. The lecture’s own apple slides drop the lone $.

📝 How It Works

1. The Suffix Trie — Same Trie, Different Input

  • Input ➔ S$ of length has suffixes: apple$, pple$, ple$, le$, e$, $. Insert each exactly as a word ➔ Trie §2.
  • Substring prefix of a suffix (equivalently suffix of a prefix) ➔ every substring is spelled by exactly one root path, and two equal substrings end at the same node.
  • Substring search ➔ walk the pattern from the root, no $ at the end ⟹ .
  • Occurrences of a substring ➔ number of leaves below its node (each leaf is one suffix that starts with it).
  • Longest repeated substring ➔ the deepest node with children (two suffixes share it as a prefix). banana ➔ ana.
  • Build ➔ suffixes, longest characters ⟹ time; space nodes worst case.

2. PT-03 Counting Formulas

(no $) · number of distinct non-empty substrings of · inner node node with children (root included) · height edges root ➔ deepest leaf.

QuantitySuffix trieSuffix treeWhy
Leavesone $-leaf per suffix; compression never merges leaves
Inner nodes (all distinct) … (all same)trie: root one node per distinct substring of · tree: root branching nodes
Total nodes … add the two rows above
Height … trie: the whole of S$ · tree: count compressed edges
edge lengthscompression keeps every character, only drops nodes
  • Distinct substrings of ➔ trie: nodes root the $ nodes · tree: . Counting all non-root nodes gives the distinct substrings of S$ instead — ABCD$ gives , of which belong to ABCD.
  • The extremes flip between the two structures ➔ the suffix trie is smallest on aaaa ( ⟹ nodes) and largest on all-distinct characters ( ⟹ ); the suffix tree is smallest on all-distinct ( nodes, height ) and largest on aaaa ( nodes, height ).

3. The Suffix Tree — Compress, Then Store Indices

  • Compression ➔ merge every chain of one-child nodes into one edge ⟹ every internal node except possibly the root has children. Exam method: draw the suffix trie, then compress.
  • Labels must be [start,end] indices ➔ storing the substring text on edges keeps space (“we still store the characters all”). Edges are contiguous runs of S$, so two integers suffice: apple$ [0,5], p [1,1], ple$ [2,5].
  • Prep notation (i, l) ➔ the prep sheet labels the child node with a 1-based start and a length instead of the lecture’s 0-based [start,end] edge: (i, l) [i-1, i+l-2]. apple$: p [1,1] (2,1), ple$ [2,5] (3,4). Any occurrence of the text is a valid label; state which convention you use.
  • nodes ➔ leaves; a tree whose internal nodes all branch has (leaves ) internal nodes ⟹ total. The lecture’s version: ➔ Geometric Series.
  • Build time is still ➔ every suffix is still inserted character by character; compression saves space, not time.
  • Compression stays only if done carefully ➔ re-slicing strings or copying labels per node reintroduces — the lecture’s “bad practice” warning.

4. Applied Problems — Think in the Trie, Answer in the Tree

  • P3 distinct substrings in ➔ trie answer counts non-$ nodes, ; each tree node absorbs exactly as many trie nodes as its edge has characters ⟹ one traversal summing end-start+1 ➔ §2.
  • P4 longest common substring in ➔ build the tree of s1#s2$ (# stops a match straddling both strings). # occurs once ⟹ it never repeats ⟹ it appears only on leaf edges. Leaf containing # ⟹ a suffix starting in , else . Post-order: set flags from the leaves upward; answer deepest (by string depth) internal node with both flags. Same shape as longest repeated substring, restricted to cross-string repeats.
  • P6 fewest substrings of concatenating to , ➔ greedy: walk down the tree of until it cannot extend, cut, restart at the root from the failing character. ABCCBA, CBAABCA ⟹ CBA ABC A . Why greedy is safe ➔ a suffix of a substring is a substring, so greedy’s -th cut is never left of any solution’s -th cut (stays ahead ➔ Greedy Algorithm). The count is unique, the pieces are not ➔ ABBA, ABA: ABA or ABA.
  • P8 shortest unique substring in ➔ unique exactly one leaf below. Shallowest candidate node with a leaf child whose edge is more than just $; answer length its string depth (an internal node is repeated by definition). cababac ⟹ ca, ac.
  • P9 shortest absent string in ➔ shallowest point missing some alphabet character as a continuation ⟹ its string that character. Positions inside an edge have one continuation only. AAABABBBA over {A,B} ⟹ BAA.
  • P13 suffix-trie size is [D] ➔ a De Bruijn sequence has distinct substrings, one node each ⟹ the bound is tight.

⚖️ Complexity

StructureBuild (naive)SpaceSubstring searchDistinct substrings
Suffix triebest · worst nodes node count
Suffix tree, text labels characters—
Suffix tree, [start,end] labels edge-length sum

📊 Exam Execution Trace & Applied Exercises

Manual Execution Trace — apple$, trie built suffix by suffix, then compressed

Suffix (start)Already in trieNew trie nodesTotal nodesSuffix-tree edge(s)
apple$ (0)—root ➔ [0,5] leaf 0
pple$ (1)—becomes [1,1] ➔ [2,5] leaf 1
ple$ (2)pp now branches ➔ [3,5] leaf 2
le$ (3)—root ➔ [3,5] leaf 3
e$ (4)—root ➔ [4,5] leaf 4
$ (5)—root ➔ [5,5] leaf 5

Final Extracted Output: trie nodes, inner, leaves, height · tree nodes, inner (root, p), leaves, height . Check: ⟹ ✓; tree edge lengths ✓.

Applied Exercise — PT-03 counts for banana

Problem: give nodes, inner nodes, leaves and height of the suffix trie and suffix tree of banana, and its distinct substrings.

Final Extracted Output: trie , tree ; edge lengths ⟹ distinct substrings ✓; longest repeated substring deepest inner node ana.

⚠️ Common Mistakes

  • 💡 Dropping the lone $ suffix ➔ one leaf short, every count off by one — the lecture’s apple slide shows leaves where there are .
  • 💡 Routing a suffix into another suffix’s path ➔ creates a cycle — the lecture’s first apple attempt. Every node has one parent; sharing happens only along a common prefix from the root (ple$ reuses the root’s p child, nothing deeper).
  • 💡 “A suffix tree is space” with text labels ➔ only [start,end] indices make it .
  • 💡 “A suffix tree builds in ” ➔ naive insertion is ; linear construction needs Ukkonen, which this unit does not teach.
  • 💡 Mixing the two label conventions ➔ (4,4) is start , length (prep, 1-based); [4,4] is one character at 0-based index (lecture). Same digits, different substrings.
  • 💡 Counting all non-root nodes as distinct substrings ➔ that includes the $-terminated ones; subtract them.

🧠 Active Recall