Define an IndexTree such that each node has data fields to store a word, the count of occurrences of that word, and a list of
Define an IndexTree such that each node has data fields to store a word, the count of occurrences of that word, and a list of all lines that word appeared on. Use an ArrayList to store line numbers. Use an IndexTree object to store an index of all the words that are in the provided text file, then display the index by performing an inorder traversal of the tree
Please implement the tree instructor provide code
package index; import java.util.List; public class IndexNode { // The word for this entry String word; // The number of occurences for this word int occurences; // A list of line numbers for this word. Listlist; IndexNode left; IndexNode right; //Constructors // Complete This // return the word and the lines it appears on. // string must be one line public String toString(){ return ""; } }
package index; public class IndexTree{ // This is your root private IndexNode root; // Make your constructor // complete the methods below // call your recursive add method public void add(String word, int lineNumber){ } //your recursive method for add // Think about how this works // When you add the word to the index, if it already exists, you want to // add it to the IndexNode that already exists // otherwise make a new indexNode private IndexNode add(IndexNode root, String word, int lineNumber){ return null; } // returns true if the word is in the index public boolean contains(String word){ return false; } // call your recursive method public void delete(String word){ } // your recursive case // remove the word and all the entries for the word private IndexNode delete(IndexNode root, String word){ return null; } // prints all the words in the index in inorder order public void printIndex(){ } public static void main(String[] args){ IndexTree index = new IndexTree(); // add all the words to the tree // print out the index // test removing a word from the index } }
import java.io.*;
public class ReadingFromFileExample { // adapted from // https://www.caveofprogramming.com/java/java-file-reading-and-writing-files-in-java.html public static void main(String [] args) {
String fileName = "pg2240.txt"; String line = null; BufferedReader bufferedReader;
try {
bufferedReader= new BufferedReader(new FileReader(fileName));
while(bufferedReader.ready()) { line = bufferedReader.readLine();
//System.out.println(line); String[] words = line.split("\\s+"); for(String word : words){ word = word.replaceAll("[.;,?]", ""); word = word.replaceAll(",", ""); System.out.println(word); } }
bufferedReader.close(); } catch(FileNotFoundException e) { System.err.println("File not found"); } // catch any other exception catch(Exception e) { e.printStackTrace(); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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