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

import java.util.*; public class SearchTree{ //Declaring variables public String

ID: 3697186 • Letter: I

Question

import java.util.*;

public class SearchTree{

//Declaring variables

public String word;

  public Set<Integer> lineNumbers;

//creating objects of class

    public SearchTreeleft, right;

public SearchTree (String w, int lineNo)

{

        word = w;

     // initializing constructor using TreeSet

lineNumbers = new TreeSet<Integer>();

       lineNumbers.add(lineNo);

        left = null;

        right = null;

    }

public void recordWord(String word2, int lineNo)

{

        if (word.compareToIgnoreCase(word2) < 0)

{

            if (right != null)

{

                right.recordWord(word2, lineNo);

            }

else

         {

                right = new SearchTree (word2, lineNo);

            }

        }

      else if (word.compareToIgnoreCase(word2) > 0)

   {

            if (left != null)

          {

                left.recordWord(word2, lineNo);

            }

else

       {

                left = new SearchTree (word2, lineNo);

            }

        }

   else if (word.compareToIgnoreCase(word2) == 0)

{

lineNumbers.add(lineNo);

   }

    }

public void display()

{

         if (left != null)

{

            left.display();

        }

        System.out.println(word + lineNumbers);

if (right != null)

{

right.display();

        }

    }

public int numberOfEntries()

{

        int count = 1;

        if (left != null)

       {

            count += left.numberOfEntries();

        }

        if (right != null)

       {

            count += right.numberOfEntries();

        }

   }

}

Write a program that uses a binary search tree to find out how many times each word in a writing sample occurs. Your program should do the following: » Read a text file Build a binarv search tree that uses data from the text file as it is read in: If the next word isn't in the tree, add it to the tree if the next word is in the tree, increment its count . Print a word frequency histogram 3 Example Input: This sentence repeats words because a sentence that repeats words makes a good example sentence. Output: because 1 example 1 good 1 makes 1 repeats 2 sentence 3 that 1 this 1 words 2

Explanation / Answer

package com.dq.thematrix.main.java.MirroringScripts;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class SearchTree{

   public static void main(String args[])
   {


       BufferedReader br = null;

       try {

           String sCurrentLine;
           int i=0;
           br = new BufferedReader(new FileReader("testing.txt"));

           while ((sCurrentLine = br.readLine()) != null) {
               System.out.println(sCurrentLine); //Read the line, and add it to your searchtree

               String tokens[]=sCurrentLine.split(" ");
               for(int j=0;j<tokens.length;j++)
               {
                   SearchTree sc=new SearchTree(tokens[j],i);
                   sc.recordWord(sCurrentLine, i);
                   i++;
               }
           }
          
           //You can now display the number of entries

       } catch (IOException e) {
           e.printStackTrace();
       } finally {
           try {
               if (br != null)br.close();
           } catch (IOException ex) {
               ex.printStackTrace();
           }
       }
   }
   //Declaring variables
   public String word;
   public Set<Integer> lineNumbers;
   //creating objects of class
   public SearchTree left, right;
   public SearchTree (String w, int lineNo)
   {
       word = w;
       // initializing constructor using TreeSet
       lineNumbers = new TreeSet<Integer>();
       lineNumbers.add(lineNo);
       left = null;
       right = null;
   }
   public void recordWord(String word2, int lineNo)
   {
       if (word.compareToIgnoreCase(word2) < 0)
       {
           if (right != null)
           {
               right.recordWord(word2, lineNo);
           }
           else
           {
               right = new SearchTree (word2, lineNo);
           }
       }
       else if (word.compareToIgnoreCase(word2) > 0)
       {
           if (left != null)
           {
               left.recordWord(word2, lineNo);
           }
           else
           {
               left = new SearchTree (word2, lineNo);
           }
       }
       else if (word.compareToIgnoreCase(word2) == 0)
       {
           lineNumbers.add(lineNo);
       }
   }
   public void display()
   {
       if (left != null)
       {
           left.display();
       }
       System.out.println(word + lineNumbers);
       if (right != null)
       {
           right.display();
       }
   }
   public int numberOfEntries()
   {
       int count = 1;
       if (left != null)
       {
           count += left.numberOfEntries();
       }
       if (right != null)
       {
           count += right.numberOfEntries();
       }
       return count;
   }
}