Types of Tries.pptx
Types of Tries.pptx
Types of Tries
• A trie is a tree-like information retrieval data
structure whose nodes store the letters of an
alphabet. It is also known as a digital tree or a
radix tree or prefix tree.
• Tries are classified into three categories:
1. Standard Trie
2. Compressed Trie
3. Suffix Trie
Standard Trie
1. A Standard Trie has the below structure:
class Node
{
// Array to store the nodes of a tree
Node[] children = new Node[26];
// To check for end of string
boolean isWordEnd;
}
banana\0
anana\0
nana\0
ana\0
na\0
a\0
\0
How to build a Suffix Tree for a given
text?
• If we consider all of the above suffixes as individual words and build a trie, we get
following.
How to build a Suffix Tree for a given
text?
• If we join chains of single nodes, we get the following compressed trie, which is the
Suffix Tree for given text “banana\0”
How to search a pattern in the built
suffix tree?
Following are abstract steps to search a pattern in the built Suffix
Tree.
1) Starting from the first character of the pattern and root of Suffix
Tree, do following for every character.
a. For the current character of pattern, if there is an edge from the
current node of suffix tree, follow the edge.
b. If there is no edge, print “pattern doesn’t exist in text” and return.
2) 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”. Let us consider the example pattern as “nan” to
see the searching process. Following diagram shows the path
followed for searching “nan” or “nana”.
How to search a pattern in the built
suffix tree?
Applications of Suffix Tree
• Suffix tree can be used for a wide range of
problems.
• Following are some famous problems where
Suffix Trees provide optimal time complexity
solution.
1) Pattern Searching
2) Finding the longest repeated substring
3) Finding the longest common substring
4) Finding the longest palindrome in a string