I am trying to implement a Trie
class in Java which just supports insertion and searching operation up until now. I think it works perfectly on the several examples I have tried but I am not able to come up with a tricky test case to check if it functions perfectly or not. Can you take a look at my implementation and see if it's the right way to code up a trie data structure?
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/* This class represents the structure of node that will be stored in a trie */
class TrieNode implements Comparable<TrieNode>, Comparator<TrieNode>{
// the alphabet to be stored in a node
Character letter;
// this variable stores if the node is a terminating point of a valid word in a dictionary
boolean isLeaf;
// this variable stores the count of the words that end at this node
int count;
// this set stores the set of child nodes attached to this node
ArrayList<TrieNode> childNodes;
// this is the parameterless constructor for the class
public TrieNode(){
}
// this is the constructor for the TrieNode class
public TrieNode(Character letter, boolean isLeaf, int count, ArrayList<TrieNode> childNodes){
this.letter = letter;
this.isLeaf = isLeaf;
this.count = count;
this.childNodes = childNodes;
}
// this method is used for sorting all of the Trie nodes according to their alphabet value
public int compareTo(TrieNode o) {
return this.letter.compareTo(o.letter);
}
@Override
public int compare(TrieNode o1, TrieNode o2) {
return o1.compareTo(o2);
}
}
/* This class represents the Trie data structure and is used for inserting and searching in a Trie */
class Trie{
// this reference variable stores the root node of a Trie data structure
TrieNode root;
// this is the constructor for the Trie class
public Trie(){
this.root = new TrieNode(' ', false, 0, new ArrayList<TrieNode>());
}
// this function is used to insert a character into a Trie
public void insertTrie(String word){
ArrayList<TrieNode> temp = this.root.childNodes;
Collections.sort(temp, new TrieNode());
for(int i = 0; i < word.length(); ++i){
boolean found = false;
for(int j = 0; j < temp.size(); ++j){
if(temp.get(j).letter == word.charAt(i)){
temp = temp.get(j).childNodes;
Collections.sort(temp, new TrieNode());
found = true;
break;
}
}
if(found)
continue;
TrieNode newNode;
if(i+1 == word.length()){
newNode = new TrieNode(word.charAt(i), true, 0, new ArrayList<TrieNode>());
}else{
newNode = new TrieNode(word.charAt(i), false, 0, new ArrayList<TrieNode>());
}
temp.add(newNode);
Collections.sort(temp, new TrieNode());
temp = newNode.childNodes;
Collections.sort(temp, new TrieNode());
}
}
// this function returns true if the word being searched for exists in the trie
public boolean searchTrie(String word){
ArrayList<TrieNode> temp = this.root.childNodes;
for(int i = 0; i < word.length() - 1; ++i){
boolean found = false;
for(int j = 0; j < temp.size(); ++j){
if(temp.get(j).letter != word.charAt(i))
continue;
else{
found = true;
temp = temp.get(j).childNodes;
break;
}
}
if(!found)
return false;
}
for(int i = 0; i < temp.size(); ++i){
if(word.charAt(word.length() - 1) == temp.get(i).letter && temp.get(i).isLeaf)
return true;
}
return false;
}
}
/* for the sake of simplicity we consider only lowercase english alphabets */
public class DataStructure1_TrieOperations {
public static void main(String[] args){
String text = "bell bid stock spock sell shell";
String[] dictionary = text.split("\\s+");
Trie tr1 = new Trie();
for(int i = 0; i < dictionary.length; ++i){
tr1.insertTrie(dictionary[i]);
}
System.out.println(tr1.searchTrie("shell"));
}
}
tart
and thentar
. While looking fortar
, your code would return a false value. Ideally it should be returning true. \$\endgroup\$