Skip to content
Advanced

Suffix Trees

The ultimate compressed trie of all suffixes.

Time Complexity
O(N)
Space Complexity
O(N)

Interactive Visualization

BAN$
ANA$
NA$
$
Rooted Compressed Suffix Trie

Technical Guide

A Suffix Tree is a compressed Trie of all suffixes of a string. It allows for incredibly fast operations: finding the longest repeated substring, finding the longest common substring of two strings, and counting pattern occurrences. While complex to implement (Ukkonen's Algorithm), it provides O(N) construction and query time.

Logical Phases

  • 1
    Building a Trie of all suffixes.
  • 2
    Compressing paths with single children into a single edge.
  • 3
    Using 'suffix links' to accelerate construction.
  • 4
    Terminating all suffixes with a unique '$' character.

Implementation

tree.insert(all_suffixes);

Expert Pitfalls

High memory overhead—often 10x to 20x the size of the original string.