ArticleslgStudy

computer science

Suffix tree

Suffix tree is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Suffix tree rather than just read about it. In short: In computer science, a suffix tree (also called PAT tree or, in an earlier form, position tree) is a compressed trie containing all the suffixes of the given text as their keys and positions in the text as their values. Suffix trees allow particularly fast implementations of many important string operations.

Suffix tree — main illustration
Suffix tree — illustration

Key takeaways

  • Suffix tree belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Suffix tree to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Suffix tree from memory before moving on to harder problems.

Reference excerpt

In computer science, a suffix tree (also called PAT tree or, in an earlier form, position tree) is a compressed trie containing all the suffixes of the given text as their keys and positions in the text as their values. Suffix trees allow particularly fast implementations of many important string operations. The construction of such a tree for the string S takes time and space linear in the length of S. Once constructed, several operations can be performed quickly, such as locating a substring in S, locating a substring if a certain number of mistakes are allowed, and locating matches for a regular expression pattern. Suffix trees also provided one of the first linear-time solutions for the longest common substring problem. These speedups come at a cost: storing a string's suffix tree typically requires significantly more space than storing the string itself.

History The concept was first introduced by Weiner (1973). Rather than the suffix S[i..n], Weiner stored in his trie the prefix identifier for each position, that is, the shortest string starting at i and occurring only once in S. His Algorithm D takes an uncompressed trie for S[k+1..n] and extends it into a trie for S[k..n]. This way, starting from the trivial trie for S[n..n], a trie for S[1..n] can be built by n−1 successive calls to Algorithm D; however, the overall run time is O(n2). Weiner's Algorithm B maintains several auxiliary data structures, to achieve an overall run time linear in the size of the constructed trie. The latter can still be O(n2) nodes, e.g. for S=anbnanbn$. Weiner's Algorithm C finally uses compressed tries to achieve linear overall storage size and run time. Donald Knuth subsequently characterized the latter as "Algorithm of the Year 1973" according to his student Vaughan Pratt. The text book Aho, Hopcroft & Ullman (1974, Sect.9.5) reproduced Weiner's results in a simplified and more elegant form, introducing the term position tree. McCreight (1976) was the first to build a (compressed) trie of all suffixes of S. Although the suffix starting at i is usually longer than the prefix identifier, their path representations in a compressed trie do not differ in size. On the other hand, McCreight could dispense with most of Weiner's auxiliary data structures; only suffix links remained. Ukkonen (1995) further simplified the construction. He provided the first online-construction of suffix trees, now known as Ukkonen's algorithm, with running time that matched the then fastest algorithms. These algorithms are all linear-time for a constant-size alphabet, and have worst-case running time of O(nlog⁡n) in general. Farach (1997) gave the first suffix tree construction algorithm that is optimal for all alphabets. In particular, this is the first linear-time algorithm for strings drawn from an alphabet of integers in a polynomial range. Farach's algorithm has become the basis for new algorithms for constructing both suffix trees and suffix arrays, for example, in external memory, compressed, succinct, etc.

Definition The suffix tree for the string S of length n is defined as a tree such that:

The tree has exactly n leaves numbered from 1 to n. Except for the root, every internal node has at least two children. Each edge is labelled with a non-empty substring of S. No two edges starting out of a node can have string-labels beginning with the same character. The string obtained by concatenating all the string-labels found on the path from the root to leaf i spells out suffix S[i..n], for i from 1 to n. If a suffix of S is also the prefix of another suffix, such a tree does not exist for the string. For example, in the string abcbc, the suffix bc is also a prefix of the suffix bcbc. In such a case, the path spelling out bc will not end in a leaf, violating the fifth rule. To fix this problem, S is padded with a terminal symbol not seen in the string (usually denoted $). This ensures that no suffix is a prefix of another, and that there will be n leaf nodes, one for each of the n suffixes of S. Since all internal non-root nodes are branching, there can be at most n−1 such nodes, and n+(n−1)+1=2n nodes in total (n leaves, n−1 internal non-root nodes, 1 root). Suffix links are a key feature for older linear-time construction algorithms, although most newer algorithms, which are based on Farach's algorithm, dispense with suffix links. In a complete suffix tree, all internal non-root nodes have a suffix link to another internal node. If the path from the root to a node spells the string χα, where χ is a single character and α is a string (possibly empty), it has a suffix link to the internal node representing α. See for example the suffix link from the node for ANA to the node for NA in the figure above. Suffix links are also used in some algorithms running on the tree. A generalized suffix tree is a suffix tree made for a set of strings instead of a single string. It represents all suffixes from this set of strings. Each string must be terminated by a different termination symbol.

Functionality A suffix tree for a string S of length n can be built in Θ(n) time, if the letters come from an alphabet of integers in a polynomial range (in particular, this is true for constant-sized alphabets). For larger alphabets, the running time is dominated by first sorting the letters to bring them into a range of size O(n); in general, this takes O(nlog⁡n) time. The costs below are given under the assumption that the alphabet is constant. Assume that a suffix tree has been built for the string S of length n, or that a generalised suffix tree has been built for the set of strings D={S1,S2,…,SK} of total length n=n1+n2+⋯+nK. You can:

… excerpt ends here. Continue reading the full article.

Illustrations

Suffix tree: Suffix tree for the text BANANA. Each substring is terminated with special character $. The six paths from the root to the leaves (shown as boxes) correspond to the six suffixes A$, NA$, ANA$, NANA$, ANANA$ and BANANA$. The numbers in the leaves give the start position of the corresponding suffix. Suffix links, drawn dashed, are used during construction.
Suffix tree for the text BANANA. Each substring is terminated with special character $. The six paths from the root to the leaves (shown as boxes) correspond to the six suffixes A$, NA$, ANA$, NANA$, ANANA$ and BANANA$. The numbers in the leaves give the start position of the corresponding suffix. Suffix links, drawn dashed, are used during construction.

Worked examples

Example 1 — a first encounter with Suffix tree

Start with the simplest possible case. Write down what Suffix tree claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Suffix tree before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Suffix tree ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Suffix tree

In research
Suffix tree appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Suffix tree in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Suffix tree is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer science suffixes, String data structures, Substring indices, so understanding it makes those chapters shorter.
In everyday life
Look for Suffix tree outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Suffix tree” →

How to study Suffix tree in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Suffix tree means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Suffix tree out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Suffix tree in simple terms?

In computer science, a suffix tree (also called PAT tree or, in an earlier form, position tree) is a compressed trie containing all the suffixes of the given text as their keys and positions in the text as their values. Suffix trees allow particularly fast implementations of many important string o…

Why does Suffix tree matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Suffix tree?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Suffix tree.

Tags

  • Computer science suffixes
  • String data structures
  • Substring indices
  • Trees (data structures)

Keep exploring