Javatpoint Logo
Javatpoint Logo

Suffix tree introduction:

In algorithms for string processing and pattern matching, a suffix tree is a type of data structure. It allows for quick pattern searching and other string-related activities by compactly representing all the suffixes of a given string. It was first introduced by Ukkonen in 1995 and is now a key idea in bioinformatics and computer science.

Trie is simply an expanded version of the suffix tree. It is a trie that has all of a string's suffixes compressed into it. Suffix trees can be used to address several string-related issues. Pattern matching, spotting distinctive substrings within a string, and figuring out the longest palindrome are a few of these issues.

A suffix is a substring that consists of all the characters in the string from a particular location to the very end. For instance, the suffixes for the string "banana" are "banana," "nana," "nana," "ana," "na," and "a." These suffixes are all stored in a tree-like data structure called a suffix tree. An ordered tree data structure called a trie is effective at storing a dynamic set of strings. Each edge of a suffix tree corresponds to a single character, and the pathways from the root to the leaves make up the suffixes of the starting string.

A suffix tree's compact representation is one of its key characteristics. A suffix tree merges such similar branches to conserve space because numerous suffixes in a string have the same prefixes. This is essential for the data structure's effectiveness, especially when working with lengthy strings.

Although building a suffix tree from the start can be difficult, some methods can do so quickly. Ukkonen's algorithm, which has a linear-time complexity, is one such algorithm.

Numerous string-related algorithms use suffix trees for a variety of purposes, including pattern searching, locating the largest repeated substring, building a generic suffix tree for multiple strings, and effectively resolving various string-based issues.

Suffix trees are widely used in bioinformatics for tasks including genome assembly, gene searching, and identifying sequence similarities. Additionally, they are used in text editors for quick search and replace operations and in other programs that deal with extensive string processing.

However, building a suffix tree might take a lot of resources because the tree needs to be stored, which limits its application to very lengthy strings or big datasets. Other compressed variations have been created to address space efficiency issues while keeping quick search capabilities, such as suffix arrays and FM-indexes.

Substring searches benefit greatly from suffix trees. Standard substring search algorithms have an O(n*m) time complexity when given a text string of length n and a pattern string of length m. However, substring search can be executed in O(m) time with a pre-built suffix tree, making it substantially faster for big text strings and numerous pattern searches.

The search procedure starts at the root of the suffix tree and proceeds along the edges that match the characters in the pattern at each stage. The search is successful and the pattern's occurrences can be followed from the leaf to the root if it is exhausted before reaching a leaf.

Suffix trees can be developed into more sophisticated data structures like suffix arrays, enhanced suffix arrays (ESA), or FM-indexes when dealing with numerous pattern searches or searching for patterns that are similar but differ somewhat from each other (approximate pattern matching). Applications like full-text search engines and DNA sequence analysis are made possible by the efficient support for numerous substring search query types provided by these structures.

Construction of Suffix Tree:

An important algorithmic concept in computer science and string processing is the creation of a suffix tree. A suffix tree is an efficient data structure for performing different string-related operations since it is a compressed trie data structure that represents all the suffixes of a particular string. It can be used for a variety of things, including pattern matching, substring searching, and other string manipulation operations.

String Preprocessing:

An input string S of length n needs to be preprocessed by adding a distinctive character (commonly designated as $) at the end. This character is used to indicate the end of each suffix in the tree and should not appear anywhere else in the original string.

Tree Initialization:

Initialize the tree with a blank suffix tree data structure. This is typically shown as a tree, with each node having the ability to have numerous children, and each edge being labeled with a portion of the original string.

Iterative Suffix Addition:

Perform the following operation for each suffix S[i:] in the preprocessed string (from the first character to the last $):

  1. Using the characters of the current suffix, work your way up the suffix tree from the root until you reach a point where the path leading to the suffix terminates or the next character is missing.
  2. Add new nodes and edges for the remaining characters in the suffix if the path corresponding to it comes to an end.
  3. Split the edge where the mismatch occurs, then generate new nodes and edges for the remaining characters if the following suffix character is missin'
  4. Add the remaining suffix characters until all of them have been processed.

The suffix tree has now been completely constructed.

Time Complexity: One can efficiently construct a suffix tree in linear time O(n) by employing a variety of algorithms, such as Ukkonen's algorithm or McCreight's algorithm.


Let's build a suffix tree for the string "banana$" in the input. We'll go over each step involved in building the tree.

Step 1: Preprocess the input string.

Input String: "banana$"

Step 2: Initialize the tree.

Empty Suffix Tree: Root


Step 3: Iterative Suffix Addition:

a. Process the suffix "banana$".

The tree after adding "banana$" as a suffix:

Similarly, a follow for all substrings of "banana$"

Applications of Suffix tree:

Substring Search: Suffix trees make it possible to quickly search for substrings. By navigating the suffix tree, you may quickly determine whether a pattern string exists in the original string given the pattern string. About the length of the pattern, this operation can be completed in linear time.

Longest Common Substring: Suffix trees can be used to identify the longest common substring that several strings have in common. You can quickly determine the longest common substring by building a generic suffix tree for each input text.

Pattern Matching with Wildcards: Suffix trees are capable of handling pattern matching when using wildcards (jokers). When you have patterns with ambiguous or missing characters, this is helpful.

Longest Repeated Substring: Suffix trees can be used to identify the longest repeating substring in a given string. This has a variety of uses, including data compression and bioinformatics.

Palindromes: Suffix trees are effective at locating the longest palindromic substring within a given string, which is helpful for tasks like DNA analysis and RNA folding, among others.

Multiple Pattern Matching: You may quickly look for many patterns in the source string using a single suffix tree. Applications like text indexing and searching benefit from this.

Generalized Suffix Tree: You can quickly locate common substrings and carry out other related actions by building a generalized suffix tree for a group of strings.

Data Compression: Suffix trees are used in data compression methods like the Burrows-Wheeler Transform (BWT) and Burrows-Wheeler Inverse Transform, which are related.

Bioinformatics: Suffix trees are widely used in bioinformatics to examine DNA sequences, biological strings, and genomic data.

Text Editing and Manipulation: Suffix trees can help with several text editing tasks, including substring substitution, insertion, and deletion.

Advantages of Suffix tree:

  • efficient memory use because all prefixes are compressed.
  • Fast pattern matching and substring searches with linear time complexity.
  • the ability to quickly identify the longest shared substring amongst different strings.
  • handling numerous pattern-matching tasks well.
  • support for wildcard (joker) substring searches.
  • the capacity to quickly identify the longest repeating substring in a string.
  • Finding the longest palindromic substring in a string quickly.
  • building in linear time with effective techniques.
  • many uses in text manipulation, data compression, and bioinformatics.
  • a powerful tool for different computer science jobs involving strings.

Disadvantages of Suffix tree:

  • The complexity of Construction: Creating a suffix tree can be challenging and may call for complex algorithms.
  • Memory Usage: Suffix trees can use up a lot of memory, especially when dealing with lengthy input strings.
  • Construction Time: Compared to more straightforward data structures, suffix trees may require more time to build initially.
  • Non-Dynamic: Suffix trees must be rebuilt for any changes to the source string since they are difficult to modify after they have been constructed.
  • Operation Complexity: When compared to other data structures, suffix trees can have more difficult-to-implement operations.
  • Overhead for Small Strings: For short strings, building a suffix tree may be more time-consuming than it is worth.
  • Suffix trees are best suited for activities involving strings; they may not be as flexible for work involving other forms of data.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA