I have implemented a trie in Java and I would request you to review it and suggest improvements or new features.
The code is split into 3 files.
TrieNode (individual nodes in the trie)
import java.util.HashMap;
class TrieNode {
private char value;
private HashMap<Character, TrieNode> children ;
public TrieNode() {
// TODO Auto-generated constructor stub
children = new HashMap<>();
}
public TrieNode(char value) {
this.value = value;
children = new HashMap<>();
}
public char getValue() {
return value;
}
public void setValue(char value) {
this.value = value;
}
public HashMap<Character, TrieNode> getChildren() {
return children;
}
public void setChildren(HashMap<Character, TrieNode> children) {
this.children = children;
}
}
Trie (the trie itself)
import java.util.Set;
public class Trie {
private TrieNode root;
public Trie() {
// TODO Auto-generated constructor stub
root = new TrieNode();
root.setValue('$'); //arbitary node value for root
}
/**
* This method will build the trie with the given set of words
* @param wordSet
*/
public void buildTrie(Set<String> wordSet){
for (String word : wordSet){
int wordLength = word.length();
TrieNode crawl = root;
for (int i=0;i<wordLength ;++i){
crawl = traverseTrie(crawl, word.charAt(i));
}
}
}
/**
* This method will traverse the trie and find out the exact position to
* insert a particular character
* @param node
* @param input
* @return
*/
public TrieNode traverseTrie(TrieNode node,char input){
TrieNode newNode = null;
//if no such node exists then create one
if (node.getChildren().get(input)==null){
node.getChildren().put(input, new TrieNode(input));
newNode = node.getChildren().get(input);
}else{
//if exist then just traverse
newNode = node.getChildren().get(input);
}
return newNode;
}
/**
* This method searches the trie for the given word and returns a boolean value
* indicating it's presence or absence
* @param word
* @return {@link boolean} value indicating it's presence or absence
*/
public boolean searchWord(String word) {
// TODO Auto-generated method stub
boolean result = true;
int wordLength = word.length();
TrieNode crawler = root;
for (int i= 0 ; i< wordLength ; ++i){
char character = word.charAt(i);
if (crawler.getChildren().get(character) == null){
result = false ;
break;
}else{
crawler = crawler.getChildren().get(character);
}
}
return result;
}
//getters and setters
public TrieNode getRoot() {
return root;
}
public void setRoot(TrieNode root) {
this.root = root;
}
}
TrieDriver (the driver program to run the trie)
import java.util.HashSet;
import java.util.Set;
public class TrieDriver {
public static void main(String[] args) {
// TODO Auto-generated method stub
Trie trie = new Trie();
Set<String> dictionary = new HashSet<>();
dictionary.add("CAT");
dictionary.add("CATWOMAN");
dictionary.add("CATERER");
dictionary.add("DOT");
dictionary.add("DOG");
dictionary.add("DOC");
//build the trie
trie.buildTrie(dictionary);
String wordToSearch = "CAX";
//search the trie
System.out.println("The word "+wordToSearch + " is present or not???"+
(boolean)trie.searchWord(wordToSearch));
}
}