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

Define an IndexTree class such that each node has data fields to store a word, t

ID: 3701762 • Letter: D

Question

Define an IndexTree class such that each node has data fields to store a word, the count of occurances of that word in a document file, and the line number for each occurance. Use an ArrayList to store the line numbers. Use an IndexTree object to store an index of words appearing in the text file, and then display the index by performing an inorder traversal of the tree.

//CLASS BinarySearchTree

BinarySearch Tree Index Tree - BinarySearchTree index + E find(E target) + boolean contains E target) + boolean add(E target) + void printOrderedData() + Index Tree(String filename) + void addRecord(String w, int line) + void printIndex() Word Comparable - String w - int count - ArrayList lines + int compareTo(Word o) + Word(String w, int line) + void addOccurrence(int line) + String toString()

Explanation / Answer

Answer :

package com.tutai;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class IndexTree {
   private Node root=null;
public static void main(String[] args) {
    IndexTree t=new IndexTree();
    t.run();
}

static class Node {
    Node left;
    Node right;
    String word;
    int noOfOccurence;
    ArrayList<String> lineNumbers;

    public Node(String word,int noOfOccurence,ArrayList<String> lineNumbers) {
      this.word = word;
      this.noOfOccurence=noOfOccurence;
      this.lineNumbers=lineNumbers;
    }
}

public void run() {
   System.out.println("Please enter file to read:");
    Scanner s=new Scanner(System.in);
    String fileName=s.next();
    try{
        Scanner fs = new Scanner(new File(fileName));
        Map<String, Integer> wordCounts = new TreeMap<String, Integer>();
      
        int lineNumber=1;
//        ArrayList[][] lineNumbers = new ArrayList[100][10];
        ArrayList<ArrayList<String>> lineNumbers = new ArrayList<ArrayList<String>>();
        while (fs.hasNext()) {
            String next = fs.next().toLowerCase();
          
            if (!wordCounts.containsKey(next)) {
                wordCounts.put(next, 1);
              
            } else {
                wordCounts.put(next, wordCounts.get(next) + 1);
            }
            for(int i=0;i<lineNumbers.size();i++){
               if(lineNumbers.get(i).equals(next)){            
               for(int j=0;j<10;j++){
                       if(lineNumbers.get(i).get(j).equals("")){
                           lineNumbers.get(i).set(j, ""+lineNumber);
                           break;
                       }
                   }
               }else{
                   lineNumbers.get(i).set(0,next);
               }
            }
            if(next.contains(".")){
               lineNumber+=1;
            }
        }
        ArrayList<String> al=new ArrayList<String>();
        for (String word : wordCounts.keySet()) {
            int count = wordCounts.get(word);
            for(int i=0;i<lineNumbers.size();i++){
               if(lineNumbers.get(i).equals(word)){            
                   al=lineNumbers.get(i);
               }
            }
            if(root==null){
               root = new Node(word,count,al);
            }else{
               insert(root,word,count,al);
            }
        }  
        printInOrder(root);
    }catch(Exception e){}
}
  

  

public void insert(Node node, String word,int count,ArrayList<String>al) {
    if (count < node.noOfOccurence) {
      if (node.left != null) {
        insert(node.left, word,count,al);
      } else {      
        node.left = new Node(word,count,al);
      }
    } else if (count > node.noOfOccurence) {
      if (node.right != null) {
        insert(node.right, word,count,al);
      } else {      
        node.right = new Node(word,count,al);
      }
    }
}

public void printInOrder(Node node) {
    if (node != null) {
      printInOrder(node.left);
      System.out.println("Word " + node.word);
      System.out.println("No of occurence:"+node.noOfOccurence);
    
      printInOrder(node.right);
    }
}


}

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