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

Trie Data Structure

Uploaded by

Diptimayee Rana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Trie Data Structure

Uploaded by

Diptimayee Rana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Trie Data Structure

The Trie data structure is a tree-like data structure used for storing a dynamic set of strings. It is
commonly used for efficient retrieval and storage of keys in a large dataset. The structure supports
operations such as insertion, search, and deletion of keys, making it a valuable tool in fields like
computer science and information retrieval. In this article we are going to explore insertion and
search operation in Trie Data Structure.

Representation of of Trie Node:-


A Trie data structure consists of nodes connected by edges. Each node represents a character or a part
of a string. The root node, the starting point of the Trie, represents an empty string. Each edge
emanating from a node signifies a specific character. The path from the root to a node represents the
prefix of a string stored in the Trie. A simple structure to represent nodes of the English alphabet
can be as follows.
struct TrieNode
{
// pointer array for child nodes of each node
TrieNode* child[26];
// Used for indicating ending of string
bool wordEnd;
TrieNode()
{
wordEnd = false;
for (int i = 0; i < 26; i++)
{
child[i] = NULL;
}
}};
Insertion in Trie Data Structure:
Let’s walk through the process of inserting the words into a Trie data structure. We have already
cover the basics of Trie and its node structure.

Here’s a visual representation of inserting the words “and” and “ant” into a Trie data structure:
Inserting “and” in Trie data structure:
Start at the root node: The root node has no character associated with it and its wordEnd value is 0,
indicating no complete word ends at this point.
 First character “a”: Calculate the index using ‘a’ – ‘a’ = 0. Check if the child[0] is null. Since it
is, create a new TrieNode with the character “a“, wordEnd set to 0, and an empty array of
pointers. Move to this new node.
 Second character “n”: Calculate the index using ‘n’ – ‘a’ = 13. Check if child[13] is null. It is, so
create a new TrieNode with the character “n“, wordEnd set to 0, and an empty array of pointers.
Move to this new node.
 Third character “d”: Calculate the index using ‘d’ – ‘a’ = 3. Check if child[3] is null. It is, so
create a new TrieNode with the character “d“, wordEnd set to 1 (indicating the word “and” ends
here).
Inserting “ant” in Trie data structure:
Start at the root node: Root node doesn’t contain any data but it keep track of every first character of
every string that has been inserted.
 First character “a”: Calculate the index using ‘a’ – ‘a’ = 0. Check if the child[0] is null. We
already have the “a” node created from the previous insertion. so move to the existing “a” node.
 First character “n”: Calculate the index using ‘n’ – ‘a’ = 13. Check if child[13] is null. It’s not, so
move to the existing “n” node.
 Second character “t”: Calculate the index using ‘t’ – ‘a’ = 19. Check if child[19] is null. It is, so
create a new TrieNode with the character “t“, wordEnd set to 1 (indicating the word “ant” ends
here).
Searching in Trie Data Structure:
Searching for a key in Trie data structure is similar to its insert operation. However, It only compares
the characters and moves down. The search can terminate due to the end of a string or lack of key in
the trie.
Steps by step approach for searching in Trie Data structure:
 Start at the root node. This is the starting point for all searches within the Trie.
 Traverse the Trie based on the characters of the word you are searching for. For each character,
follow the corresponding branch in the Trie. If the branch doesn’t exist, the word is not present in
the Trie.
 If you reach the end of the word and the wordEnd flag is set to 1, the word has been found.
 If you reach the end of the word and the wordEnd flag is 0, the word is not present in the Trie,
even though it shares a prefix with an existing word.
Below is the implementation of the above approach:-
// Method to insert a key into the Trie
void insertKey(TrieNode* root, const string& key)
{ // Initialize the curr pointer with the root node
TrieNode* curr = root;
// Iterate across the length of the string
for (char c : key)
{
// Check if the node exists for the
// current character in the Trie
if (curr->child[c - 'a'] == nullptr)
{
// If node for current character does
// not exist then make a new node
TrieNode* newNode = new TrieNode(); // Keep the reference for the newly
// created node
curr->child[c - 'a'] = newNode;
}
// Move the curr pointer to the
// newly created node
curr = curr->child[c - 'a'];
}
// Mark the end of the word
curr->wordEnd = true;
}
Output
Query String: do
The query string is present in the Trie
Query String: geek
The query string is present in the Trie
Query String: bat
The query string is not present in the Trie
Let’s assume that we have successfully inserted the words “and“, “ant“, and “dad” into our Trie, and
we have to search for specific words within the Trie data structure. Let’s try searching for the word
“dad“:

 We start at the root node.


 We follow the branch corresponding to the character ‘d’.
 We follow the branch corresponding to the character a’.
 We follow the branch corresponding to the character ‘d’.
 We reach the end of the word and wordEnd flag is 1. This means that “dad” is present in the Trie.
Below is the implementation of searching strings in Trie Data Structure:-
// Method to search a key in the Trie
bool searchKey(TrieNode* root, const string& key)
{
// Initialize the curr pointer with the root node
TrieNode* curr = root;
// Iterate across the length of the string
for (char c : key)
{
// Check if the node exists for the
// current character in the Trie
if (curr->child[c - 'a'] == nullptr)
return false;
// Move the curr pointer to the
// already existing node for the
// current character
curr = curr->child[c - 'a'];
}
// Return true if the word exists
// and is marked as ending
return curr->wordEnd;
}
Time Complexity: O(number of words * maxLengthOfWord)
Space Complexity : O(number of words * maxLengthOfWord)

You might also like