How is it possible to build a suffix tree in linear time?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A suffix tree is a compressed trie containing all the suffixes of a given text. It is a powerful data structure that allows many string processing problems to be solved in linear time once the tree is constructed. This article provides a detailed explanation of how a suffix tree can be built for a given string in linear time, emphasizing key algorithms and techniques used in the process.
Basic Concepts
Before diving into the construction algorithm, let's review a few fundamental concepts associated with suffix trees:
- Suffix: A suffix of a string is a substring that starts at any position and extends to the end of that string.
- Trie: A tree-like data structure that stores a dynamic set of strings, where each key is represented by a path from the root to a leaf.
- Compressed Trie: A trie with nodes that store edges labeled with substrings rather than single characters, compressing long paths of single characters.
Key Properties of Suffix Trees
- Size: A suffix tree for a string of length `n` has at most `2n` nodes.
- Construction Time: Suffix trees can be built in time, where `n` is the length of the string.
- Applications: They enable efficient solutions for problems like substring search, longest repeated substring, and more.
Linear Time Construction
Building a suffix tree in linear time is made possible mainly through Ukkonen's Algorithm, which incrementally builds the suffix tree for a string in a left-to-right manner. Let's introduce some vital components of Ukkonen's Approach:
Ukkonen's Algorithm Overview
The crux of Ukkonen's algorithm lies in reducing redundant operations by tracking active points and leveraging suffix links. Here's a step-by-step description:
- Initialization: Start with an empty tree. Process the string character by character.
- Active Point Tracking: Maintain an active point consisting of an active node, active edge, and active length to determine where the new character influences the existing tree.
- Extension Rule Handling: For each new character, apply one of the following rules:
- Rule 1: If the active point indicates that the character fits the existing edge, extend the edge.
- Rule 2: If the edge must split, introduce a new internal node and edge.
- Rule 3: If the character is new under an active node, create a new leaf node.
- Suffix Links: Use suffix links—pointers from internal nodes to other nodes—to efficiently find the next point from where the insertion process can resume for the next phase.
Example
Consider building a suffix tree for the string `banana` (with an added terminal symbol `$` to ensure uniqueness of suffixes).
- Start with the first character `b$`.
- Add `a$`, create branches for both suffixes.
- Insert `n$`, handle overlaps by creating new nodes as required.
- Continue this for `a
$\, `n$\, and finally `a$`, respecting the suffix links for efficiency.
Optimization Summary with Ukkonen's Algorithm
- Applications: Apart from substring search, suffix trees facilitate tasks like finding longest palindromes, constructing Suffix Arrays, and performing string comparisons, among others.
- Variations: Various other optimizations and algorithms exist, like McCreight’s or Weiner’s methods, but Ukkonen’s remains popular for its clarity and efficiency.
- Challenges: While the algorithm promises linear time, the practical implementation can be complex, often requiring careful attention to edge cases.
Related reading
- How is Monte Carlo Tree Search implemented in practice
- How is Nesterov's Accelerated Gradient Descent implemented in Tensorflow?
- How is nth_element Implemented?
- How is On log n different then Olog n?
- How is pagerank calculated in a distributed way?
- How is Python's List Implemented?
- How is quick sort better at cache locality than mergesort?
- How is str.joiniterable method implemented in Python/ Linear time string concatenation

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.