Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this is IndexNode.java this is IndexTree.java this is ReadingFromFileExample.java this is txt example 1 Your Assignment We will create an IndexTree, a special type of

image text in transcribed

this is IndexNode.java

image text in transcribed

this is IndexTree.java

image text in transcribed

image text in transcribedthis is ReadingFromFileExample.java

image text in transcribed

this is txt example

image text in transcribed

1 Your Assignment We will create an IndexTree, a special type of Binary Search Tree. The IndexTree does a bit more than your standard tree, as we will use it to build an index of a file. This will be much like the index you find at the end of a textbook, where each topic is listed in alphabetical order with the pages it is found on. Since files don't have traditional pages to work off of, we will instead use line numbers. Furthermore, rather than building an index of only a few select topics, we will build an index over all the words in the file. To do this, we use a special type of node. The IndexTree is made up of special IndexNodes. Rather than using generics, each IndexNode stores a word, the count of occurrences of that word, and a list of all lines that word appeared on (this means that each IndexNode will hold their own list). Nodes in the tree will be sorted by the String. 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. 2 How to Read a File Good news, everyone! You already know how to do most of it. I've attached a file showing how to use Scanner to read every line of text from the file, split that line into individual words, and print out each word. Be sure to scrub each word of those pesky punctuations, like commas package index; import java.util.List; public class IndexNode { // The word for this entry String word; // The number of occurrences for this word int occurences; // A list of line numbers for this word. List list; Index Node left; IndexNode right; // Constructors // Constructor should take in a word and a line number // it should initialize the list and set occurrences to 1 // Complete This // return the word, the number of occurrences, and the lines it appears on. // string must be one line public String toString(){ return ""; } } package index; // Your class. Notice how it has no generics. // This is because we use generics when we have no idea what kind of data we are getting 1/ Here we know we are getting two pieces of data: a string and a line number public class IndexTree { // This is your root // again, your root does not use generics because you know your nodes 1/ hold strings, an int, and a list of integers private IndexNode root; // Make your constructor // It doesn't need to do anything // complete the methods below // this is your wrapper method // it takes in two pieces of data rather than one 1/ call your recursive add method public void add(String word, int lineNumber) { } // your recursive method for add // Think about how this is slightly different the the regular add method 1/ 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 // use book as guide public void delete(String word) { } // your recursive case Il remove the word and all the entries for the word // This should be no different than the regular technique. private IndexNode delete (Index Node root, string word) { return null; } // prints all the words in the index in inorder order // To successfully print it out // this should print out each word followed by the number of occurrences and the list of all occurrences // each word and its data gets its own line 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 } } package index; import java.io.*; import java.util.Scanner; public class ReadingFromFileExample { public static void main(String [] args) { String fileName = "pg100.txt"; try { Scanner scanner = new Scanner(new File(fileName)); while( scanner.hasNextLine()){ String line = scanner.nextLine(); System.out.println(line); //String[] words = line.split("\\s+"); //for(String word : words) { // word = word.replaceAll(":", ""); word = word.replaceAll(", ""); // System.out.println(word); //} } scanner.close(); II } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } 4 Unthrifty loveliness why dost thou spend, Upon thy self thy beauty's legacy? Nature's bequest gives nothing but doth lend, And being frank she lends to those are free: Then beauteous niggard why dost thou abuse, The bounteous largess given thee to give? Profitless usurer why dost thou use So great a sum of sums yet canst not live? For having traffic with thy self alone, Thou of thy self thy sweet self dost deceive, Then how when nature calls thee to be gone, What acceptable audit canst thou leave? Thy unused beauty must be tombed with thee, Which used lives th' executor to be. 5 Those hours that with gentle work did frame The lovely gaze where every eye doth dwell Will play the tyrants to the very same, And that unfair which fairly doth excel: For never-resting time leads summer on To hideous winter and confounds him there, Sap checked with frost and lusty leaves quite gone, Beauty o'er-snowed and bareness every where: Then were not summer's distillation left A liquid prisoner pent in walls of glass, Beauty's effect with beauty were bereft, Nor it nor no remembrance what it was. But flowers distilled though they with winter meet, Leese but their show, their substance still lives sweet. 1 Your Assignment We will create an IndexTree, a special type of Binary Search Tree. The IndexTree does a bit more than your standard tree, as we will use it to build an index of a file. This will be much like the index you find at the end of a textbook, where each topic is listed in alphabetical order with the pages it is found on. Since files don't have traditional pages to work off of, we will instead use line numbers. Furthermore, rather than building an index of only a few select topics, we will build an index over all the words in the file. To do this, we use a special type of node. The IndexTree is made up of special IndexNodes. Rather than using generics, each IndexNode stores a word, the count of occurrences of that word, and a list of all lines that word appeared on (this means that each IndexNode will hold their own list). Nodes in the tree will be sorted by the String. 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. 2 How to Read a File Good news, everyone! You already know how to do most of it. I've attached a file showing how to use Scanner to read every line of text from the file, split that line into individual words, and print out each word. Be sure to scrub each word of those pesky punctuations, like commas package index; import java.util.List; public class IndexNode { // The word for this entry String word; // The number of occurrences for this word int occurences; // A list of line numbers for this word. List list; Index Node left; IndexNode right; // Constructors // Constructor should take in a word and a line number // it should initialize the list and set occurrences to 1 // Complete This // return the word, the number of occurrences, and the lines it appears on. // string must be one line public String toString(){ return ""; } } package index; // Your class. Notice how it has no generics. // This is because we use generics when we have no idea what kind of data we are getting 1/ Here we know we are getting two pieces of data: a string and a line number public class IndexTree { // This is your root // again, your root does not use generics because you know your nodes 1/ hold strings, an int, and a list of integers private IndexNode root; // Make your constructor // It doesn't need to do anything // complete the methods below // this is your wrapper method // it takes in two pieces of data rather than one 1/ call your recursive add method public void add(String word, int lineNumber) { } // your recursive method for add // Think about how this is slightly different the the regular add method 1/ 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 // use book as guide public void delete(String word) { } // your recursive case Il remove the word and all the entries for the word // This should be no different than the regular technique. private IndexNode delete (Index Node root, string word) { return null; } // prints all the words in the index in inorder order // To successfully print it out // this should print out each word followed by the number of occurrences and the list of all occurrences // each word and its data gets its own line 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 } } package index; import java.io.*; import java.util.Scanner; public class ReadingFromFileExample { public static void main(String [] args) { String fileName = "pg100.txt"; try { Scanner scanner = new Scanner(new File(fileName)); while( scanner.hasNextLine()){ String line = scanner.nextLine(); System.out.println(line); //String[] words = line.split("\\s+"); //for(String word : words) { // word = word.replaceAll(":", ""); word = word.replaceAll(", ""); // System.out.println(word); //} } scanner.close(); II } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } 4 Unthrifty loveliness why dost thou spend, Upon thy self thy beauty's legacy? Nature's bequest gives nothing but doth lend, And being frank she lends to those are free: Then beauteous niggard why dost thou abuse, The bounteous largess given thee to give? Profitless usurer why dost thou use So great a sum of sums yet canst not live? For having traffic with thy self alone, Thou of thy self thy sweet self dost deceive, Then how when nature calls thee to be gone, What acceptable audit canst thou leave? Thy unused beauty must be tombed with thee, Which used lives th' executor to be. 5 Those hours that with gentle work did frame The lovely gaze where every eye doth dwell Will play the tyrants to the very same, And that unfair which fairly doth excel: For never-resting time leads summer on To hideous winter and confounds him there, Sap checked with frost and lusty leaves quite gone, Beauty o'er-snowed and bareness every where: Then were not summer's distillation left A liquid prisoner pent in walls of glass, Beauty's effect with beauty were bereft, Nor it nor no remembrance what it was. But flowers distilled though they with winter meet, Leese but their show, their substance still lives sweet

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

Database Concepts International Edition

Authors: David M. Kroenke

6th Edition International Edition

0133098222, 978-0133098228

More Books

Students also viewed these Databases questions