Trie Data Structure in Java
A Trie Data Structure is nothing but it is a tree-like data structure which is used to efficiently store and retrieve the dynamic set of Strings or Keys. It is certainly used for tasks that will involve searching for strings with common prefix like auto-complete or spell-checking applications.
In this article, we will learn about Trie Data Structure Implementation in Java.
Organization of a Trie Data Structure
In a Trie, each node represents the single character of the string. The structure of the Trie is organized such that each path from a root node to the leaf node i.e. an end of the word node, It is represent the string. The Trie data structure has an insertion operation and a searching operation. Each node may have multiple children each corresponding to a different character. A flag is usually associated with each node to mark whether the string represented by that node is completed.
Note: The root node represents an empty string.
Representation of Trie Data structure:

Explanation of the Image:
- root node is the empty node in the above tree.
- Each node represents the character, and the end-of-word nodes are the last of the tree.
- The path from the root to each end-of-word node represents the word in a Trie.
- The strings in the above Trie data structure are BOX, BALL, BAT, CAT and CHAI.
- Also, There are some word which don't make any sense but are there BALT.
Program to Implement Trie Data Structure
Below is the implementation of Trie Data Structure:
// Java Program to Implement Trie
// Data Structure
// Helper Class TrieNode
class TrieNode {
TrieNode[] children;
boolean isEndOfWord;
public TrieNode() {
// Assuming lowercase English letters
children = new TrieNode[26];
isEndOfWord = false;
}
}
// Class Trie
public class Trie {
private TrieNode root;
// Constructor
public Trie() {
root = new TrieNode();
}
// Insert a word into the Trie
public void insert(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (current.children[ch - 'a'] == null) {
current.children[ch - 'a'] = new TrieNode();
}
current = current.children[ch - 'a'];
}
current.isEndOfWord = true;
}
// Search for a word in the Trie
public boolean search(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (current.children[ch - 'a'] == null) {
// Word not found
return false;
}
current = current.children[ch - 'a'];
}
return current != null && current.isEndOfWord;
}
// Check if a given prefix exists in the Trie
public boolean startsWith(String prefix) {
TrieNode current = root;
for (int i = 0; i < prefix.length(); i++) {
char ch = prefix.charAt(i);
if (current.children[ch - 'a'] == null) {
// Prefix not found
return false;
}
current = current.children[ch - 'a'];
}
return true;
}
// Main Method
public static void main(String[] args) {
Trie trie = new Trie();
trie.insert("hello");
trie.insert("world");
// Demonstrate search and startsWith
System.out.println(trie.search("hello"));
System.out.println(trie.search("world"));
System.out.println(trie.search("hi"));
System.out.println(trie.startsWith("hell"));
}
}
Output
true true false true
Complexity of the Above Methods:
Operation | Explanation | Complexity |
---|---|---|
Insertion (insert) Operation | Inserting a word into Trie involves the traversing Trie from the root to leaf node corresponding to each character in word. |
|
Search (search) Operation | Searching for the word in the Trie is involve the traversing Trie from the root to leaf node corresponding to each character in word. |
|
Prefix Search (startWith) Operation | Checking if the given prefix exists in Trie is involve the traversing the Trie from the root to node corresponding to each character in the prefix. |
|
Applications of Trie Data Structure:
There are some key applications of Trie data structure as mentioned below:
- Auto-Complete and Spell Checking
- Dictionary Implementations
- IP Address Lookup
- Prefix Matching
- Text Compression
- Contact Management and Search
- DNA Sequencing and Genome Analysis
- Word Frequency Counting
Conclusion
In conclusion, the Trie data structure is the powerful tool for the efficiently handling string-related operations, particularly those are involve the prefix-based searches. It is ability to the store and retrieve the strings in a hierarchical manner makes it invaluable in the wide range of the applications from the auto-complete and spell checking in the text editors to the IP routing in the computer networks and from the contact management in the mobile devices to the DNA sequencing in the bioinformatics. Trie data structure is compact the representation of common prefixes leads to the efficient storage and retrieval of the data and making it an optimal choice for scenarios involving large dictionaries or datasets.