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

Binary Search Tree JAVA Write a program that uses a binary search tree to find o

ID: 3697056 • Letter: B

Question

Binary Search Tree JAVA

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

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();

        }

   }

}