Java Program - I Need Some Help With This Program I Am Getting A Few Errors I Pu
ID: 3694729 • Letter: J
Question
Java Program - I Need Some Help With This Program I Am Getting A Few Errors I Put Them In BOLD
public class BinarySearchTree
{
private BSTNode root;
private int count;
public BinarySearchTree()
{
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() +" ");
}
}
public void addElement(int index,int element)
{
BSTNode temp = new BSTNode(element);
int count;
if(index==0)
root = temp;
else
{
BSTNode 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 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 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--;
}
}
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);
}
}
Explanation / Answer
/****************
dear user the problem that has you presented is the same in both cases, it is refer to the comparison between two values.
the first of them have a problem due the type of variable declared in your code; this is type integer, and is confused which ones values do you want to compare.
the second present the same problem on inverse sense. because the integer type can't be compared with compareTo must be compared with Integer.compare(int a , int b)
in the same way strings can't be compared with operators == and < or > must be compared with compareTo. this was fixed on the code below
in addition it code does not look in a good way, because the definition of the node BTS is
public class BSTNode
{
BSTNode left, right;
int data; // <--- and data like integer is not the correct way to solve the problem
why? well you are stored words like strings, so the correct way will be Word data;
in this way you could solve the question, but it implies modify all the declaration of the class BTS. however it could be more easy in many ways.
Regards!
*****************/
public class BinarySearchTree
{
private BSTNode root;
private int count;
public BinarySearchTree()
{
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() +" ");
}
}
public void addElement(int index,int element)
{
BSTNode temp = new BSTNode(element);
int count=0;
if(index==0)
root = temp;
else
{
BSTNode current = root;
boolean added = false;
while (!added)
{
//Replace the call of an instance method compareTo with the call of static compare method, like this:
//if(temp.data.compareTo(current.data) < 0)
if(Integer.compare(element,current.data)<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.equals(((Word) comp).Phrase)) //equals is used to compare strings
//if (comp.Phrase == ((Word) comp).Phrase)
return 0;
else if (comp.Phrase.compareTo(((Word) comp).Phrase) < 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--;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.