Suffix Tree
A Suffix Tree for a given text is a compressed trie for all suffixes of the given text.
How to build a Suffix Tree for a given text?
As discussed above, Suffix Tree is compressed trie of all suffixes, so following are very abstract steps to build a suffix tree from given text.
- Generate all suffixes of given text.
- Consider all suffixes as individual words and build a compressed trie.
How to search a pattern in the built suffix tree?
We have discussed above how to build a Suffix Tree which is needed as a pre-processing step in pattern searching. Following are abstract steps to search a pattern in the built Suffix Tree.
- Starting from the first character of the pattern and root of Suffix Tree, do following for every character.
- For the current character of pattern, if there is an edge from the current node of suffix tree, follow the edge.
- If there is no edge, print “pattern doesn’t exist in text” and return.
- If all characters of pattern have been processed, i.e., there is a path from root for characters of the given pattern, then print “Pattern found”.
Applications
- Pattern Searching
- Finding the longest repeated substring
- Finding the longest common substring
- Finding the longest palindrome in a string