Javatpoint Logo
Javatpoint Logo

Generalized Suffix Tree


In the realm of computer science and algorithms, the Generalized Suffix Tree (GST) stands out as a powerful and versatile data structure. This sophisticated tree-based data structure has proven to be invaluable in a variety of applications, ranging from bioinformatics and text processing to data compression and pattern matching.

What is Suffix Trees?

To comprehend the Generalized Suffix Tree, it's essential to first understand its precursor - the Suffix Tree.

A Suffix Tree is a tree-like data structure that represents all the suffixes of a given string. These trees efficiently store information about the substrings contained within the original string, enabling fast retrieval and pattern matching.

However, the concept of a Generalized Suffix Tree takes this a step further. While a regular Suffix Tree represents the suffixes of a single string, a Generalized Suffix Tree handles multiple strings simultaneously. This makes it an ideal choice for scenarios where you need to compare and analyze multiple strings concurrently.

The construction of a suffix tree for a single string of length n typically takes O(n) time and space complexity. The tree has the property that each edge label is a substring of the input string, and each path from the root to a leaf represents a unique suffix.

What is a Generalized Suffix Tree?

A Generalized Suffix Tree is also a tree-like data structure that stores all the suffixes of a set of strings. Unlike a standard suffix tree, which is built for a single string, a Generalized Suffix Tree can handle multiple strings at once. This makes it a powerful tool for tasks like searching for common substrings among multiple texts.

The time complexity for constructing a Generalized Suffix Tree form strings of total length N is O(N), where N is the sum of the lengths of all strings

Generalized Suffix Tree

Key Features of Generalized Suffix Trees:

  • Multiple String Support: Handles multiple input strings efficiently.
  • Substring Search: Allows searching for substrings in the set of strings.
  • Longest Common Substring: Can find the longest common substring among multiple strings.
  • Pattern Matching: Efficiently supports searching for patterns within the set of strings.

Construction of a Generalized Suffix Tree:

1. Concatenation of Strings:

Combine all input strings into a single string by adding unique delimiters between them. This ensures that each input string's suffixes are distinguishable in the tree.

2. Build Suffix Tree:

Construct a suffix tree for the concatenated string using the Ukkonen's algorithm or another efficient suffix tree construction algorithm.

3. Label Nodes with String IDs:

Assign each node in the tree a label indicating the string to which the corresponding substring belongs. This step is crucial for distinguishing substrings from different input strings.



  • The program defines two classes: GSTNode and
  • The GSTNode class represents a node in the generalized suffix tree and contains a map of child nodes corresponding to different characters, as well as a vector of indices representing the positions where the suffix associated with the current node occurs.
  • The GeneralizedSuffixTree class manages the overall tree structure.
  • The GeneralizedSuffixTree class has a constructor that takes a vector of strings as input and builds the suffix tree.
  • The tree is constructed by iteratively extending the tree with each string, adding a unique identifier (index) to each leaf node to differentiate between different strings.
  • The extendSuffixTree method is responsible for extending the suffix tree by adding nodes corresponding to each character in the input string.
  • If a node for a character doesn't exist, it is created. The method also updates the indices associated with each node.
  • The buildSuffixTree method is responsible for initiating the construction of the suffix tree for a collection of strings. It appends a unique identifier to each string to distinguish between them and calls the extendSuffixTree
  • The printTree method is responsible for printing the entire suffix tree. It starts from the root and recursively prints the edges and associated indices of each node in a depth-first manner.
  • In the main function, a vector of strings is created, and a GeneralizedSuffixTree object is instantiated using these strings.
  • Finally, the printTree method is called to display the constructed generalized suffix tree.

Program Output:

Generalized Suffix Tree


1. Longest Common Substring (LCS):

Generalized Suffix Trees are invaluable in solving the Longest Common Substring problem for multiple strings. By identifying the deepest internal node with leaf nodes from all input strings, one can determine the LCS efficiently.

2. Applications in String Matching:

One of the primary applications of Generalized Suffix Trees is in efficient string matching. With a GST, one can quickly determine if a given substring exists in any of the input strings. This is invaluable in tasks such as DNA sequence analysis, where identifying specific patterns or motifs across multiple sequences is crucial.

3. Bioinformatics Applications:

In bioinformatics, Generalized Suffix Tree (GST) play a pivotal role in various applications. DNA and protein sequence analysis often involves searching for common motifs, patterns, or similarities across multiple biological sequences. Generalized Suffix Trees enable researchers to efficiently perform these tasks, facilitating the discovery of important information within large datasets.

4. Pattern Matching and Data Compression:

Generalized Suffix Tree (GST) find applications beyond bioinformatics. They are used in data compression algorithms, particularly in applications where multiple strings need to be efficiently represented. By leveraging the compact representation of suffix trees, Generalized Suffix Tree contribute to the development of more efficient compression techniques.


The Generalized Suffix Tree (GST) stands as a powerful data structure with broad applications in various fields, ranging from bioinformatics to text indexing and pattern matching. Its ability to efficiently store and retrieve all suffixes of multiple strings simultaneously makes it a versatile tool for solving complex problems. In this conclusion, we will delve into the significance of the Generalized Suffix Tree, its strengths, and potential areas for improvement.

One of the key strengths of the Generalized Suffix Tree (GST) lies in its ability to handle multiple strings seamlessly. By consolidating the suffixes of different strings in a single tree structure, it facilitates quick searches and pattern matching across the entire dataset. This capability is particularly valuable in bioinformatics, where the analysis of genetic sequences from multiple organisms or individuals requires simultaneous examination of their suffixes.

Moreover, the Generalized Suffix Tree (GST) has proven to be an efficient solution for various string-related problems, such as finding the longest common substring among multiple strings or identifying repeated patterns within a dataset. Its time and space complexities are often favorable, making it a practical choice for large-scale applications.

Next TopicInterval Tree

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA