Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

All I need is the test class for this, with the main method that can read text f

ID: 3713985 • Letter: A

Question

All I need is the test class for this, with the main method that can read text files and make an index.

This assignment is end of chapter 6, Programming Project 7, from page 357 of the textbook. I am simply giving you more specific details Define an IndexTree class such that each node has data fields to store a word, the count of occurrences of that word in a document file, and the line number for each occurrence. Use an ArrayList to store the line numbers. Use an IndexTree object to store an index of words appearing in a text file, and then display the index by performing an inorder traversal of the tree BinarySearchTree E extends Comparable> IndexTree + E find(E target) +boolean contains(E target) + boolean add(E target) + void printOrderedData0 BinarySearchTree index +IndexTree(String filename) + void addRecord(String w, int line) + void printIndex0) Word Kinterface>> Comparable - String w -int count ArrayList Integer> lines int compareTo(Word o) + Word(String w, int line) + void addOccurrence(int line) + String toString) Consider the UML above. I left a few things out of the UML that you won't need to directly worry about (e.g the BinarySearchTree class extends the BinaryTree class, which I've provided you with) Do the following Implement the find method of the BinarySearchTree class. The rest of that class is already implemented (from an in-class example). Carefully read the javadoc comment that I provided in the source code to make sure you implement the correct thing Implement the IndexTree class, along with its inner class Word, as specified in the UML above. A reminder on some UML notation: means private, and+ means public. Here are details of what the various methods should do: Inner class, Word, represents one unique word, the count of the number of occurrences of that word, and a list of all of the line numbers from the file where it appeared. o Constructor initializes the Word for the first occurrence found, so should initialize the field w to whatever String the word is, count to 1, and the ArrayList to a single element consisting of whatever line number was specified addOccurrence should add the specified line number to the ArrayList only if it's not already in the list, and regardless should increase count by 1. We want to know total number of times the word was found, but the list of line numbers should only have unique line numbers (think of a book's index, where you wouldn't see the same page number listed multiple times for the same word) '

Explanation / Answer

Code

BinaryTree.java


package com.src;
public class BinaryTree<E> {
protected Node<E> root;
public BinaryTree() {
  
}
public BinaryTree(E data, BinaryTree<E> leftTree, BinaryTree<E> rightTree) {
root = new Node<E>(data);
if (leftTree != null) {
root.left = leftTree.root;
}
if (rightTree != null) {
root.right = rightTree.root;
}
}
protected BinaryTree(Node<E> root) {
this.root = root;
}
public BinaryTree<E> getLeftSubtree() {
if (root != null && root.left != null)
return new BinaryTree<E>(root.left);
else
return null;
}
public BinaryTree<E> getRightSubtree() {
if (root != null && root.right != null)
return new BinaryTree<E>(root.right);
else
return null;
}
public E getData() {
if (root != null)
return root.data;
else
return null;
}
public boolean isEmpty() {
return root == null;
}
public boolean isLeaf() {
return root != null && root.left == null && root.right == null;
}
protected void preOrderTraversal(ProcessData<E> visitOperation) {
preOrderTraversal(root, visitOperation);
}
private void preOrderTraversal(Node<E> node, ProcessData<E> visitOperation) {
if (node == null)
return;
visitOperation.process(node.data);
preOrderTraversal(node.left, visitOperation);
preOrderTraversal(node.right, visitOperation);
}
protected void postOrderTraversal(ProcessData<E> visitOperation) {
postOrderTraversal(root, visitOperation);
}
private void postOrderTraversal(Node<E> node, ProcessData<E> visitOperation) {
if (node == null)
return;
postOrderTraversal(node.left, visitOperation);
postOrderTraversal(node.right, visitOperation);
visitOperation.process(node.data);
}
protected void inOrderTraversal(ProcessData<E> visitOperation) {
inOrderTraversal(root, visitOperation);
}
private void inOrderTraversal(Node<E> node, ProcessData<E> visitOperation) {
if (node == null)
return;
if (node.left != null && visitOperation instanceof PrePostProcess)
((PrePostProcess<E>) visitOperation).pre();
inOrderTraversal(node.left, visitOperation);
visitOperation.process(node.data);
inOrderTraversal(node.right, visitOperation);
if (node.right != null && visitOperation instanceof PrePostProcess)
((PrePostProcess<E>) visitOperation).post();
}
protected interface ProcessData<E> {
void process(E data);
}
protected interface PrePostProcess<E> extends ProcessData<E> {
void pre();
void post();
}
protected static class Node<E> {
protected E data;
protected Node<E> left;
protected Node<E> right;
public Node(E data) {
this.data = data;
}
public String toString() {
return data.toString();
}
}
}


BinarySearchTree.java
package com.src;
public class BinarySearchTree<E extends Comparable<E>> extends BinaryTree<E> {
public E find(E target) {
return find(root,target);
}
private E find(Node<E> subtreeRoot, E target) {
if (subtreeRoot == null)
return null;
if (target.compareTo(subtreeRoot.data) == 0)
return subtreeRoot.data;
else if (target.compareTo(subtreeRoot.data) < 0)
return find(subtreeRoot.left, target);
else
return find(subtreeRoot.right, target);
}
public boolean contains(E target) {
return contains(root, target);
}
public boolean add(E target) {
if (root == null) {
root = new Node<E>(target);
return true;
}
return add(root, target);
}
public void printOrderedData() {
inOrderTraversal(new ProcessData<E>() {
public void process(E data) {
System.out.print(data + " ");
}
});
}
private boolean contains(Node<E> subtreeRoot, E target) {
if (subtreeRoot == null)
return false;
if (target.compareTo(subtreeRoot.data) == 0)
return true;
else if (target.compareTo(subtreeRoot.data) < 0)
return contains(subtreeRoot.left, target);
else
return contains(subtreeRoot.right, target);
}
private boolean add(Node<E> subtreeRoot, E target) {
if (target.compareTo(subtreeRoot.data) == 0)
return false;
else if (target.compareTo(subtreeRoot.data) < 0) {
if (subtreeRoot.left == null) {
subtreeRoot.left = new Node<E>(target);
return true;
}
return add(subtreeRoot.left, target);
} else {
if (subtreeRoot.right == null) {
subtreeRoot.right = new Node<E>(target);
return true;
}
return add(subtreeRoot.right, target);
}
}
}
//Word.java
package com.src;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class Word implements Comparable<Word> {
private String w;
private int count;
private ArrayList<Integer> lines;
public Word(String w, int line) {
super();
this.w = w;
this.count = 1;
lines.add(line);
}
public void addOccurance(int line) {
if(!lines.contains(line))
lines.add(line);
count++;
}
@Override
public String toString() {
String s = w + "(" + count + "): ";
int i=0;
for(i=0;i<lines.size()-1;i++) {
s += lines.get(i) + ", ";
}
s += lines.get(i);
return s;
}
@Override
public int compareTo(Word o) {
// TODO Auto-generated method stub
return this.w.compareTo(o.w);
}
}
//IndexTree.java
package com.src;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class IndexTree {
private BinarySearchTree<Word> index;
public IndexTree(String filename) throws FileNotFoundException {
super();
int count = 0;
Scanner sc = new Scanner(filename);
while (sc.hasNextLine()) {
String line = sc.nextLine();
count++;
StringTokenizer st = new StringTokenizer(line," ");
  
while (st.hasMoreTokens()) {  
Word w = new Word(st.nextToken().toLowerCase(), count);
index.add(w);
}  
}
sc.close();
}
public void addRecord(Word w, int line) {
if(index.contains(w)) {
Word wd = index.find(w);
wd.addOccurance(line);
}
else {
index.add(w);
}
}
public void printIndex() {
index.printOrderedData();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote