I am working on some performance tests in various data structures. My list has a HashMap and Trie data structure. I ended up with HashMap but not sure how to use Trie to solve the problem below -
I have a text file that contains 2 million English words with their frequencies in this format -
hello 100
world 5000
good 2000
bad 9000
...
Now I read this file line by line and save it to HashMap. The first split line goes like a key in HashMap, and the next split line goes like a value in HashMap, and so I can measure the insert with the code below.
Map<String, String> wordTest = new HashMap<String, String>();
try {
fis = new FileInputStream(FILE_LOCATION);
reader = new BufferedReader(new InputStreamReader(fis));
String line = reader.readLine();
while (line != null) {
String[] splitString = line.split("\\s+");
wordTest.put(splitString[0].toLowerCase().trim(), splitString[1].trim());
line = reader.readLine();
}
}
, Trie, Trie, HashMap? String? , Trie .
: -
TrieImpl
public class TrieImpl {
private TrieNode r;
public TrieImpl() {
r = new TrieNode();
}
public boolean has(String word) {
return r.has(word);
}
public void insert(String word){
r.insert(word);
}
public String toString() {
return r.toString();
}
public static void main(String[] args) {
TrieImpl t = new TrieImpl();
System.out.println("Testing some strings");
t.insert("HELLO");
t.insert("WORLD");
}
}
TrieNode class -
public class TrieNode {
private TrieNode[] c;
private boolean flag = false;
public TrieNode() {
c = new TrieNode[26];
}
protected void insert(String word) {
int val = word.charAt(0) - 64;
if (c[val] == null) {
c[val] = new TrieNode();
}
if (word.length() > 1) {
c[val].insert(word.substring(1));
} else {
c[val].flag = true;
}
}
public boolean has(String word) {
int val = word.charAt(0) - 64;
if (c[val] != null && word.length() > 1) {
c[val].has(word.substring(1));
} else if (c[val].flag == true && word.length() == 1) {
return true;
}
return false;
}
public String toString() {
return "";
}
}
, , , String?