Question: I have an assaignment from Data Structures and Algorithms. The program supposed to take a txt file as an input and a word to search

I have an assaignment from Data Structures and Algorithms. The program supposed to take a txt file as an input and a word to search then, search that word in the file. However my program doesn't take any input file instead, it gives a readymade output and exception error. I will be putting my assaignment document with required outputs , my code below and I will put the input file context too. There are 3 classes in total: TrieNode, TrieAssaignment and Trie. This is the test class: class Trie {
private TrieNode root;
public Trie(){
root = new TrieNode();
}
public void insert(String word){
TrieNode current = root;
for (char ch : word.toCharArray()){
current = current.children.computeIfAbsent(ch, c -> new TrieNode());
}
current.isEndOfWord = true;
current.frequency++;
}
public boolean search(String word){
TrieNode current = root;
for (char ch : word.toCharArray()){
current = current.children.get(ch);
if (current == null){
return false;
}
}
return current.isEndOfWord;
}
private void autoCompleteHelper(TrieNode node, String prefix, List results){
if (node.isEndOfWord){
results.add(prefix);
}
for (Map.Entry entry : node.children.entrySet()){
autoCompleteHelper(entry.getValue(), prefix + entry.getKey(), results);
}
}
public void autoComplete(String prefix){
TrieNode current = root;
for (char ch : prefix.toCharArray()){
current = current.children.get(ch);
if (current == null){
System.out.println("No words");
return;
}}
List results = new ArrayList>();
autoCompleteHelper(current, prefix, results);
Collections.sort(results);
System.out.println(String.join(",", results));
}
private void reverseAutoCompleteHelper(TrieNode node, String suffix, List results, String currentWord){
if (node.isEndOfWord && currentWord.endsWith(suffix)){
results.add(currentWord);
}
for (Map.Entry entry : node.children.entrySet()){
reverseAutoCompleteHelper(entry.getValue(), suffix, results, currentWord + entry.getKey());
}
}
public void reverseAutoComplete(String suffix){
List results = new ArrayList>();
reverseAutoCompleteHelper(root, suffix, results, "");
Collections.sort(results);
System.out.println(results.isEmpty()?"No words" : String.join(",", results));
}
private void fullAutoCompleteHelper(TrieNode node, String prefix, String suffix, List results, String currentWord){
if (node.isEndOfWord && currentWord.startsWith(prefix) && currentWord.endsWith(suffix)){
results.add(currentWord);
}
for (Map.Entry entry : node.children.entrySet()){
fullAutoCompleteHelper(entry.getValue(), prefix, suffix, results, currentWord + entry.getKey());
}
}
public void fullAutoComplete(String prefix, String suffix){
List results = new ArrayList>();
fullAutoCompleteHelper(root, prefix, suffix, results, "");
Collections.sort(results);
System.out.println(results.isEmpty()?"No words" : String.join(",", results));
}
public void findTopK(int k){
PriorityQueue> maxHeap = new PriorityQueue>((a, b)->{
if (!a.getValue().equals(b.getValue())){
return b.getValue()- a.getValue();
}
return a.getKey().compareTo(b.getKey());
});
Map wordFreq = new HashMap>();
collectWords(root,"", wordFreq);
maxHeap.addAll(wordFreq.entrySet());
List results = new ArrayList>();
for (int i =0; i k && !maxHeap.isEmpty(); i++){
results.add(maxHeap.poll().getKey());
}
System.out.println(String.join(",", results));
}
private void collectWords(TrieNode node, String prefix, Map wordFreq){
if (node.isEndOfWord){
wordFreq.put(prefix, node.frequency);
}
for (Map.Entry entry : node.children.entrySet()){
collectWords(entry.getValue(), prefix + entry.getKey(), wordFreq);
}}} Input1.txt file: I ate dinner
We had a three course meal
Brad came to dinner with us
He loves fish tacos
In the end, we all felt like we ate too much
We all agreed; it wall
 I have an assaignment from Data Structures and Algorithms. The program

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!