In this lab you create an index for the complete works of William Shakespeare (pg100.txt), although you can use it on other files. It will test your ability to put together multiple data structures and object.
Create an IndexTree, a special type of Binary Search Tree.The Index Tree 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 text book, 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.
These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references.
Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.
public class IndexTree {
static int printCount = -1;
// This is your root
// again, your root does not use generics because you know your nodes
// hold strings, an int, and a list of integers
private IndexNode root;
// Make your constructor
// It doesn't need to do anything
public IndexTree() {
}
// complete the methods below
// this is your wrapper method
// it takes in two pieces of data rather than one
// call your recursive add method
public void add(String word, int lineNumber){
if (root == null)
root = new IndexNode(word, lineNumber);
else
add(root, word, lineNumber);
}
// your recursive method for add
// Think about how this is slightly different the the regular add method
// 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 node, String word, int lineNumber){
if (word.compareTo(node.word) < 0) {
if (node.left != null) {
return add(node.left, word, lineNumber);
} else {
node.left = new IndexNode(word, lineNumber);
return node.left;
}
} else if (word.compareTo(node.word) > 0) {
if (node.right != null) {
return add(node.right, word, lineNumber);
} else {
node.right = new IndexNode(word, lineNumber);
return node.right
This is only a preview of the solution.
Please use the purchase button to see the entire solution.