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

JAVA Program - public class BSTNode { BSTNode left, right; int data; public BSTN

ID: 3694063 • Letter: J

Question

JAVA Program -

public class BSTNode
{
   BSTNode left, right;
   int data;


   public BSTNode()
   {
   left = null;
   right = null;
   data = 0;
   }

   public BSTNode(int n)
   {
   left = null;
   right = null;
   data = n;
   }

   public void setLeft(BSTNode n)
   {
   left = n;
   }

   public void setRight(BSTNode n)
   {
   right = n;
   }

   public BSTNode getLeft()
   {
   return left;
   }

   public BSTNode getRight()
   {
   return right;
   }

   public void setData(int d)
{
   data = d;
   }

   public int getData()
   {
   return data;
   }
}

public class BST
{
   private BSTNode root;

   public BST()
   {
   root = null;
   }

   public boolean isEmpty()
   {
   return root == null;
   }

   public void insert(int data)
   {
   root = insert(root, data);
   }

   private BSTNode insert(BSTNode node, int data)
   {
   if (node == null)
   node = new BSTNode(data);
   else
   {
   if (data <= node.getData())
   node.left = insert(node.left, data);
   else
   node.right = insert(node.right, data);
}
   return node;
   }

   public void delete(int k)
   {
   if (isEmpty())
   System.out.println("Tree Empty");
   else if (search(k) == false)
   System.out.println("Sorry "+ k +" is not present");
   else
   {
   root = delete(root, k);
   System.out.println(k+ " deleted from the tree");
   }
   }
   private BSTNode delete(BSTNode root, int k)
   {
   BSTNode p, p2, n;
   if (root.getData() == k)
   {
   BSTNode lt, rt;
   lt = root.getLeft();
   rt = root.getRight();
   if (lt == null && rt == null)
   return null;
   else if (lt == null)
   {
   p = rt;
   return p;
   }
   else if (rt == null)
   {
   p = lt;
   return p;
   }
   else
   {
   p2 = rt;
   p = rt;
   while (p.getLeft() != null)
   p = p.getLeft();
   p.setLeft(lt);
   return p2;
   }
   }
   if (k < root.getData())
   {
   n = delete(root.getLeft(), k);
   root.setLeft(n);
   }
   else
   {
   n = delete(root.getRight(), k);
   root.setRight(n);
   }
   return root;
   }

   public int countNodes()
   {
   return countNodes(root);
   }
  
   private int countNodes(BSTNode r)
   {
   if (r == null)
   return 0;
   else
   {
   int l = 1;
   l += countNodes(r.getLeft());
   l += countNodes(r.getRight());
   return l;
   }
   }

   public boolean search(int val)
   {
   return search(root, val);
   }

   private boolean search(BSTNode r, int val)
   {
   boolean found = false;
   while ((r != null) && !found)
   {
   int rval = r.getData();
   if (val < rval)
r = r.getLeft();
   else if (val > rval)
   r = r.getRight();
   else
   {
   found = true;
   break;
   }
   found = search(r, val);
   }
   return found;
   }

   public void inorder()
{
   inorder(root);
   }
   private void inorder(BSTNode r)
   {
   if (r != null)
   {
inorder(r.getLeft());
   System.out.print(r.getData() +" ");
   inorder(r.getRight());
   }
   }
  
   public void preorder()
   {
   preorder(root);
   }
   private void preorder(BSTNode r)
   {
   if (r != null)
   {
   System.out.print(r.getData() +" ");
   preorder(r.getLeft());
   preorder(r.getRight());
   }
   }
  
  
   public void postorder()
   {
   postorder(root);
   }
   private void postorder(BSTNode r)
   {
   if (r != null)
   {
   postorder(r.getLeft());
   postorder(r.getRight());
   System.out.print(r.getData() +" ");
   }
   }
}

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 binary 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 This sentence repeats words because a sentence that repeats words makes a good example sentence. A 2 because 1 example 1 good 1 makes 1 repeats 2 sentence 3 that 1 this 1 words 2

Explanation / Answer

program :

import java.util.Scanner;
import java.io.*;
import java.util.StringTokenizer;
public class TestDriver2
{
public static void main(String[] args) throws IOException
{
BinarySearchTree bST = new BinarySearchTree();
Word wrd1 = new Word();
Word wrd2 = new Word();
int tracker;
String inFileName = "inwords.txt";
BufferedReader inFile = new BufferedReader (new FileReader (inFileName));
String liner = inFile.readLine();
liner = liner.toLowerCase();
System.out.println("The passage that was read from infile is : " + liner);
StringTokenizer strTokenizer = new StringTokenizer (liner, "., ");
while (strTokenizer.hasMoreTokens())
{
tracker = 0;
wrd1 = new Word(strTokenizer.nextToken(),tracker++);   
}
System.out.println("BST is: " + bST);   
}
}
public void addElement (Comparable element)
{
BinaryTreeNode temp = new BinaryTreeNode (element);
if (isEmpty())
root = temp;
else
{
BinaryTreeNode current = root;
boolean added = false;
while (!added)
{
if (element.compareTo(current.element) < 0)
if (current.left == null)
{
current.left = temp;
added = true;
}
else
current = current.left;
else
if (current.right == null)
{
current.right = temp;
added = true;
}
else
current = current.right;
}
}
count++;
}
public class Word
{
private String Phrase;
private int counter;
public Word()
{
Phrase = null;
counter = 0;
}
public Word(String phr, int cnt)
{
Phrase = phr;
counter = cnt;
}
public Word(String phr)
{
Phrase = phr;
counter = 0;
}
public boolean equals(Word looking)
{
boolean status;
  
if(Phrase.equals(looking.Phrase)&& (counter != looking.counter))
status = true;
else
status = false;
return status;
}
public int compareTo(Word comp)
{
if (comp.Phrase == ((Word) comp).Phrase)
return 0;
else if ((comp.Phrase) > ((Word) comp).Phrase)
return 1;
else
return -1;
}
public String toString()
{
String describer = Phrase + ", " + counter;
return describer;
}
public void setPhrase(String p)
{
Phrase = p;
}
public void setCount(int c)
{
counter = c;
}
public String getWord()
{
return Phrase;
}
public int getCount()
{
return counter;
}
public void incword()
{
counter++;
}
public void decword()
{
counter--;
}   
}