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:
- Standard Trie
- Compressed Trie
- Suffix Trie
Standard Trie A standard trie have the following properties:
- 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;
}
- It is an ordered tree like data structure.
- Each node(except the root node) in a standard trie is labeled with a character.
- The children of a node are in alphabetical order.
- Each node or branch represents a possible character of keys or words.
- Each node or branch may have multiple branches.
- The last node of every key or word is used to mark the end of word or node.
Below is the illustration of the Standard Trie:
Compressed Trie A Compressed trie have the following properties:
- 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;
}
- A Compressed Trie is an advanced version of the standard trie.
- Each nodes(except the leaf nodes) have atleast 2 children.
- It is used to achieve space optimization.
- To derive a Compressed Trie from a Standard Trie, compression of chains of redundant nodes is performed.
- It consists of grouping, re-grouping and un-grouping of keys of characters.
- While performing the insertion operation, it may be required to un-group the already grouped characters.
- While performing the deletion operation, it may be required to re-group the already grouped characters.
- A compressed trie T storing s strings(keys) has s external nodes and O(s) total number of nodes.
Below is the illustration of the Compressed Trie:
Suffix Trie A Suffix trie have the following properties:
- 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;
}
- A Suffix Trie is an advanced version of the compressed trie.
- The most common application of suffix trie is Pattern Matching.
- While performing the insertion operation, both the word and its suffixes are stored.
- A suffix trie is also used in word matching and prefix matching.
- To generate a suffix trie, all the suffixes of given string are considered as individual words.
- Using the suffixes, compressed trie is built.
Below is the illustration of the Suffix Trie:
Last Updated :
15 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...