Trie Datastructure
Prakash K.
Senior Technology Services Engineer @ Walmart [ CSM? | CSD? | PSM? | CSPO? | PSPO? | 12x Microsoft? Azure? certified | 4x GCP? certified | Core Java | Go | Microservices | JPA | DSA | SOLID design principles ]
GitHub Link : https://github.com/prakashbtech87/PrakashJavaRepository/tree/main/src/main/java/com/prakash/datastructure/trie
A Trie (pronounced like “try”) is a special type of tree-like data structure used mainly for storing and managing strings, particularly for tasks like autocomplete or spell checking. Here’s how it works in simple terms:
The name “Trie” (pronounced like “try”) is derived from the word “retrieval.” It refers to a specific type of tree data structure used primarily for storing and retrieving keys in a way that allows for efficient access.
Basic Concept
How It Works
root
└─ c
└─ a
└─ t
Key Features
Example:
package com.prakash.datastructure.trie;
/**
* @author prakashkaruppusamy
*/
class TrieNode {
// Each node can have multiple children (one for each character)
TrieNode[] children = new TrieNode[26]; // For 'a' to 'z'
boolean isEndOfWord = false; // Indicates if a word ends at this node
// Constructor
public TrieNode() {
for (int i = 0; i < 26; i++) {
children[i] = null; // Initialize children to null
}
}
}
class Trie {
private TrieNode root; // Root node of the Trie
// Constructor
public Trie() {
root = new TrieNode(); // Initialize the root node
}
// Insert a word into the Trie
public void insert(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
int index = c - 'a'; // Get the index for the character (0 for 'a', 1 for 'b', ...)
if (node.children[index] == null) {
node.children[index] = new TrieNode(); // Create a new node if it doesn't exist
}
node = node.children[index]; // Move to the child node
}
node.isEndOfWord = true; // Mark the end of the word
}
// Search for a word in the Trie
public boolean search(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
int index = c - 'a'; // Get the index for the character
if (node.children[index] == null) {
return false; // If the node doesn't exist, the word isn't in the Trie
}
node = node.children[index]; // Move to the child node
}
return node.isEndOfWord; // Return true if it's the end of a word
}
// Check if there is any word in the Trie that starts with the given prefix
public boolean startsWith(String prefix) {
TrieNode node = root;
for (char c : prefix.toCharArray()) {
int index = c - 'a'; // Get the index for the character
if (node.children[index] == null) {
return false; // If the node doesn't exist, no words with this prefix
}
node = node.children[index]; // Move to the child node
}
return true; // Found the prefix
}
}
领英推荐
Explanation of the Code
TrieNode Class:
Trie Class:
Insert Method:
Search Method:
package com.prakash.datastructure.trie;
/**
* @author prakashkaruppusamy
*/
public class Main {
public static void main(String[] args) {
Trie trie = new Trie();
// Insert words into the Trie
trie.insert("apple");
trie.insert("app");
// Search for words
System.out.println(trie.search("apple")); // true
System.out.println(trie.search("app")); // true
System.out.println(trie.search("appl")); // false
System.out.println(trie.startsWith("ap")); // true
System.out.println(trie.startsWith("b")); // false
}
}
Output:
This simple implementation of a Trie allows you to insert words, search for complete words, and check for prefixes efficiently. It’s particularly useful for applications like autocomplete, spell-checking, and IP routing. The main advantage of using a Trie is that it can provide faster lookup times compared to other data structures like hash tables when dealing with prefix-related queries.