# Suffix Tree

> The Suffix Tree is a compressed trie of all suffixes of a text, optimized for fast substring searches.

## What is a suffix tree used for?

The specialized tree that stores all suffixes of a string, enabling efficient substring and pattern matching. Known for rapid text searching with linear space and time complexity.

- Topic: Tree
- Type: data structure

## Common mistakes

- Building it by inserting every suffix into a plain trie, which takes O(n²) time and space.
- Forgetting a unique end marker such as '$', so some suffixes end in the middle of an edge.

## Interview questions

### What is the time complexity of building a suffix tree?

Ukkonen's algorithm builds it in O(n) for a fixed alphabet. A substring search then takes O(m), where m is the pattern length.

### Suffix tree vs suffix array: which should I use?

A suffix array stores only the sorted suffix positions, so it uses much less memory. It answers the same queries with binary search, at a small extra time cost.

### Suffix tree vs trie: what is the difference?

A trie stores a set of separate words. A suffix tree is a compressed trie of all suffixes of one text, used to search for substrings inside it.

## Related cards

- [Trie](https://wonderium.dev/algorithms/trie-prefix-tree.md)
- [String](https://wonderium.dev/algorithms/string.md)
- [Tree](https://wonderium.dev/algorithms/tree.md)
- [Binary Search](https://wonderium.dev/algorithms/binary-search.md)
- [O(n)](https://wonderium.dev/algorithms/o-n.md)

---

- Level: [Algorithms](https://wonderium.dev/algorithms.md)
- HTML version: https://wonderium.dev/algorithms/suffix-tree
- Last updated: 2026-11-25
