Data Structures · Visualization

Suffix Tree: Search

Suffix Tree

Searching a suffix tree for a pattern walks down from the root matching the pattern's characters against edge labels; if the walk completes without mismatch, the pattern occurs in the text, and the subtree below marks every occurrence. Time: O(m) for pattern length m, independent of text length n. Space: O(1) auxiliary beyond the tree.

Big-O: Build O(n); substring search O(m) for pattern length m.

Suffix Tree: Search

Suffix Tree

Searching a suffix tree for a pattern walks down from the root matching the pattern's characters against edge labels; if the walk completes without mismatch, the pattern occurs in the text, and the subtree below marks every occurrence.

How Search Works

  1. Start at the root and the beginning of the pattern.
  2. At each step, find the child edge whose label starts with the next unmatched character.
  3. Match characters along that edge; if a mismatch occurs, the pattern is absent.
  4. If the whole pattern is consumed, the pattern exists — all leaves in the current subtree give its occurrence positions.

Search Worked Example

Input: Search for "ana" in the suffix tree of "banana

quot;

  1. From root, follow edge starting with 'a'
  2. Match 'a', then 'n', then 'a' along the compressed edges
  3. Pattern fully consumed at an internal node whose subtree has 2 leaves

Output: "ana" occurs at positions 1 and 3 in "banana"

Try your own input
Suffix Tree: SearchO(m)
Walks the pattern’s characters against edge labels; the leaf positions under the matched node are every occurrence.
string: "banana$"
0
1→ pos 0
"banana$"
8
4
2→ pos 1
"na$"
5→ pos 3
"$"
"na"
9→ pos 5
"$"
"a"
6
3→ pos 2
"na$"
7→ pos 4
"$"
"na"
10→ pos 6
"$"
Active node Active edge Edge label Node
Suffix Tree: Search
Press ▶ Run to watch it step by step.
Python · Suffix Trees in Code
def search(tree, pattern):
    cur = tree
    for ch in pattern:
        if ch not in cur:
            return []              # mismatch — absent
        cur = cur[ch]
    leaves = []

    def collect(node):
        if "$end" in node:
            leaves.append(node["$end"])
        for k, v in node.items():
            if k != "$end":
                collect(v)

    collect(cur)                   # subtree = occurrences
    return sorted(leaves)          # e.g. "ana" → [1, 3]

# search(tree, "ana")  → [1, 3]  in O(m)
# example: search "ana"
Input: Search for "ana" in the suffix tree of "banana
quot;

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Search Time & Space Complexity

TimeO(m) for pattern length m, independent of text length n
SpaceO(1) auxiliary beyond the tree

When to Use Search

  • Fast repeated substring searches after one-time build cost
  • Bioinformatics motif search
  • Auto-suggest / longest repeated substring tools

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.