Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 classes in total: TrieNode, TrieAssaignment and Trie. This is the test class: class Trie
private TrieNode root;
public Trie
root new TrieNode;
public void insertString word
TrieNode current root;
for char ch : word.toCharArray
current current.children.computeIfAbsentch c new TrieNode;
current.isEndOfWord true;
current.frequency;
public boolean searchString word
TrieNode current root;
for char ch : word.toCharArray
current current.children.getch;
if current null
return false;
return current.isEndOfWord;
private void autoCompleteHelperTrieNode node, String prefix, List results
if nodeisEndOfWord
results.addprefix;
for MapEntry entry : node.children.entrySet
autoCompleteHelperentrygetValue prefix entry.getKey results;
public void autoCompleteString prefix
TrieNode current root;
for char ch : prefix.toCharArray
current current.children.getch;
if current null
System.out.printlnNo words";
return;
List results new ArrayList;
autoCompleteHelpercurrent prefix, results;
Collections.sortresults;
System.out.printlnStringjoin results;
private void reverseAutoCompleteHelperTrieNode node, String suffix, List results, String currentWord
if nodeisEndOfWord && currentWord.endsWithsuffix
results.addcurrentWord;
for MapEntry entry : node.children.entrySet
reverseAutoCompleteHelperentrygetValue suffix, results, currentWord entry.getKey;
public void reverseAutoCompleteString suffix
List results new ArrayList;
reverseAutoCompleteHelperroot suffix, results, ;
Collections.sortresults;
System.out.printlnresultsisEmptyNo words" : String.join results;
private void fullAutoCompleteHelperTrieNode node, String prefix, String suffix, List results, String currentWord
if nodeisEndOfWord && currentWord.startsWithprefix && currentWord.endsWithsuffix
results.addcurrentWord;
for MapEntry entry : node.children.entrySet
fullAutoCompleteHelperentrygetValue prefix, suffix, results, currentWord entry.getKey;
public void fullAutoCompleteString prefix, String suffix
List results new ArrayList;
fullAutoCompleteHelperroot prefix, suffix, results, ;
Collections.sortresults;
System.out.printlnresultsisEmptyNo words" : String.join results;
public void findTopKint k
PriorityQueue maxHeap new PriorityQueuea b
if agetValueequalsbgetValue
return bgetValue agetValue;
return agetKeycompareTobgetKey;
;
Map wordFreq new HashMap;
collectWordsroot wordFreq;
maxHeap.addAllwordFreqentrySet;
List results new ArrayList;
for int i ; i k && maxHeap.isEmpty; i
results.addmaxHeappollgetKey;
System.out.printlnStringjoin results;
private void collectWordsTrieNode node, String prefix, Map wordFreq
if nodeisEndOfWord
wordFreq.putprefix node.frequency;
for MapEntry entry : node.children.entrySet
collectWordsentrygetValue prefix entry.getKey wordFreq;
Inputtxt 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started