0% found this document useful (0 votes)
2 views

Types of Tries.pptx

The document discusses three types of tries: Standard Trie, Compressed Trie, and Suffix Trie, each with distinct structures and functionalities. Standard Tries are basic tree structures for storing words, Compressed Tries optimize space by merging nodes, and Suffix Tries are used for pattern matching by storing all suffixes of a string. The document also outlines how to build and search within a Suffix Tree, highlighting its applications in various string-related problems.

Uploaded by

OML series
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Types of Tries.pptx

The document discusses three types of tries: Standard Trie, Compressed Trie, and Suffix Trie, each with distinct structures and functionalities. Standard Tries are basic tree structures for storing words, Compressed Tries optimize space by merging nodes, and Suffix Tries are used for pattern matching by storing all suffixes of a string. The document also outlines how to build and search within a Suffix Tree, highlighting its applications in various string-related problems.

Uploaded by

OML series
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Types of Tries

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;
}

2. It is an ordered tree like data structure.


3. Each node(except the root node) in a standard trie is labeled with a character.
4. The children of a node are in alphabetical order.
5. Each node or branch represents a possible character of keys or words.
6. Each node or branch may have multiple branches.
7. The last node of every key or word is used to mark the end of word or node.
Illustration of the Standard Trie
String S = {ball, box, bomb, basket, stock, stop}
In standard trie, every traversal from the root node represents
one of the words in the string S.
Compressed Trie
1. A Compressed Trie has the below structure:
class Node
{
// Array to store the nodes of tree
Node[] children = new Node[26];
// To store the edgeLabel
StringBuilder[] edgeLabel = new StringBuilder[26];
// To check for end of string
boolean isEnd;
}
2. A Compressed Trie is an advanced version of the standard trie.
3. Each nodes(except the leaf nodes) have atleast 2 children.
4. It is used to achieve space optimization.
Compressed Trie
5. To derive a Compressed Trie from a Standard Trie,
compression of chains of redundant nodes is
performed.
6. It consists of grouping, re-grouping and un-grouping
of keys of characters.
7. While performing the insertion operation, it may be
required to un-group the already grouped characters.
8. While performing the deletion operation, it may be
required to re-group the already grouped characters.
9. A compressed trie T storing s strings(keys) has s
external nodes and O(s) total number of nodes.
Illustration of the Compressed Trie
A compressed trie is space optimized: if a node is the only child
of its parent, merge the node with the parent.
S[0] = b a l l
S[1] = b o x
S[2] = b o m b
S[3] = b a s k e t
S[4] = s t o c k
S[5] = s t o p
Suffix Trie
1. A Compressed Trie has the below structure:
struct SuffixTreeNode
{
// Array to store the nodes
struct SuffixTreeNode *children[256];
//pointer to other node via suffix link
struct SuffixTreeNode *suffixLink;
// (start, end) interval specifies the edge,
// by which the node is connected to its parent node
int start;
int *end;
// For leaf nodes, it stores the index of Suffix for the path from root to
leaf
int suffixIndex;
}
Suffix Trie
2. A Suffix Trie is an advanced version of the compressed trie.
3. The most common application of suffix trie is Pattern
Matching.
4. While performing the insertion operation, both the word and
its suffixes are stored.
5. A suffix trie is also used in word matching and prefix
matching.
6. To generate a suffix trie, all the suffixes of given string are
considered as individual words.
7. Using the suffixes, compressed trie is built.
Illustration of the Suffix Trie
How to build a Suffix Tree for a given
text?
• Suffix Tree is compressed trie of all suffixes, so
following are very abstract steps to build a
suffix tree from given text.
1) Generate all suffixes of given text.
2) Consider all suffixes as individual words
and build a compressed trie.
• Let us consider an example text “banana\0”
where ‘\0’ is string termination character.
Following are all suffixes of “banana\0”
How to build a Suffix Tree for a given
text?
• Following are all suffixes of “banana\0”

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy