Suffix Tree
Linear Time Algorithm
String Processing
Data Structures
Computer Science

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.

Practice algorithms

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

  1. Size: A suffix tree for a string of length `n` has at most `2n` nodes.
  2. Construction Time: Suffix trees can be built in O(n)O(n) time, where `n` is the length of the string.
  3. 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:

  1. Initialization: Start with an empty tree. Process the string character by character.
  2. 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.
  3. 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.
  4. 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).

  1. Start with the first character `b$`.
  2. Add `a$`, create branches for both suffixes.
  3. Insert `n$`, handle overlaps by creating new nodes as required.
  4. 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.