Answered step by step
Verified Expert Solution
Link Copied!

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 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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How would you handle the difficulty level of the texts?

Answered: 1 week ago