I'm attempting to write my own binary search tree in java. I have written all my methods and I am now trying to write a program to test the methods.
However, when I try to implement my "insert" method, it will not compile and I have no idea why.
public class lab05driver {
public static void main(String[] args) {
BST q = new BST();
int a = 5;
String b = "jed";
double c = 1.8;
char d = 'r';
boolean e = false;
int f = 35;
String g = "yay";
double h = 2.1;
char i = 'i';
boolean j = true;
Integer k = 5;
q.insert(k);
}}
and my BST class looks like this:
public class BST implements myBST {
private myTreeNode root;
public BST() {
}
public void insert(Comparable x) {
if(root == null) {
root = new myTreeNode();
root.data = x;
} else if ( !lookup(x) ) {
root.insert(x);
}
}
...more code...
}
and, myBST looks like:
public interface myBST {
public void insert(Comparable x);
public void delete(Comparable x);
public boolean lookup(Comparable x);
public void printPreOrder();
public void printInOrder();
public void printPostOrder();
}
finally, myTreeNode looks like:
public class myTreeNode {
public myTreeNode() {
}
public Comparable data ;
public myTreeNode leftchild;
public myTreeNode rightchild;
public myTreeNode parent;
public void insert(Comparable d) {
//if less than
//does left exist? if it doesnt, make it, give it d
//if it exists call insertrecursive on rightchild
if(d.compareTo(data) <= 0) {
if(leftchild != null) {
leftchild.insert(d);
} else {
leftchild = new myTreeNode();
leftchild.data = d;
leftchild.parent = this;
}
} else {
if(rightchild != null) {
rightchild.insert(d);
} else {
rightchild = new myTreeNode();
rightchild.data = d;
rightchild.parent = this;
}
}
}
...more code...
}
it is throwing an error at "q.insert(k)" in the lab05driver. any help/suggestions would be greatly appreciated...
~~~~~
EDIT: sorry i jsut copied that wrong... there is a main method and Integer k is an integer...
the error im getting the command line is:
warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
q.insert(k); is a statement. Statements need to be in methods, it isn't currently in a method.
So do something like:
public class lab05driver
{
public static void main( String[] args )
{
BST q = new BST();
int a = 5;
String b = "jed";
double c = 1.8;
char d = 'r';
boolean e = false;
int f = 35;
String g = "yay";
double h = 2.1;
char i = 'i';
boolean j = true;
Integer k = 1; // changed because "Integer k = "test";" doesn't compile
q.insert(k);
}
}
Note the signature I used for the method. This is the signature that Java sees as the entry method (where the program will start).
The most obvious problem that I can see is:
Integer k = "test";
k needs to be an integer of some sort - you've assigned it a String. This is not a valid assignment. Valid values would be -1, 0, 1 etc - any integer value.
Once you assign a value value (or change k to be the String class) your code should be ok
Related
I am trying to check whether my levelorder of my Binary Search Tree is equal to the other one. To do this, I tried to make a compareTo method. I only give equal values to the method, but it keeps on saying the condition is false. When I place breakpoints, I see that the values are still equal. I am probably not understanding it correctly. Does anyone know how to solve this?
Here is what I did, as you can see below, the compareTo returns a 1 instead of a 0:
import edu.princeton.cs.algs4.BST;
import java.util.*;
public class MyBST implements Comparable<MyBST>{
private Object e;
public MyBST(Object e){
this.e = e;
}
private Object getE(){
return e;
}
public static void main(String[] args) {
int size = 4;
Random r = new Random();
Set<Integer> tes = new LinkedHashSet<>(size);
Stack<Integer> stack = new Stack<>();
while (tes.size() < size) {
tes.add(r.nextInt(10));
}
System.out.println("possible combinations");
Set<Stack<Integer>> combos = combos(tes, stack, tes.size());
Object[] arr = combos.toArray();
List<String> d = new ArrayList<>();
for (Object s : arr) {
String b = s.toString();
b = b.replaceAll("\\[", "").replaceAll("\\]", "");
d.add(b);
}
int index = 0;
do {
BST<String, Integer> bst1 = new BST<String, Integer>();
BST<String, Integer> bst2 = new BST<String, Integer>();
String key1 = d.get(index);
String key2 = d.get(index);
key1 = key1.replaceAll(" ", "");
String[] m = key1.split(",");
key2 = key2.replaceAll(" ", "");
String[] n = key2.split(",");
System.out.println("1e order");
for (int j = 0; j < m.length; j++) {
System.out.println(m[j]);
bst1.put(m[j], 0);
}
System.out.println("2e order");
for (int j = 0; j < n.length; j++) {
System.out.println(n[j]);
bst2.put(n[j], 0);
}
System.out.println("levelorder 1e BST");
MyBST e = new MyBST(bst1.levelOrder());
MyBST y = new MyBST(bst2.levelOrder());
System.out.println(bst1.levelOrder());
System.out.println("levelorder 2e BST");
System.out.println(bst2.levelOrder());
System.out.println(e.compareTo(y) + "\n");
index++;
} while (index < arr.length - 1);
}
public static Set<Stack<Integer>> combos(Set<Integer> items, Stack<Integer> stack, int size) {
Set<Stack<Integer>> set = new HashSet<>();
if (stack.size() == size) {
set.add((Stack) stack.clone());
}
Integer[] itemz = items.toArray(new Integer[0]);
for (Integer i : itemz) {
stack.push(i);
items.remove(i);
set.addAll(combos(items, stack, size));
items.add(stack.pop());
}
return set;
}
#Override
public int compareTo(MyBST o) {
if (this.e == o.e) {
return 0;
}
else
return 1;
}
}
Here you can find the BST.java class: BST.java
And the output is something like:
The breakpoint at the compareTo method says:
When you're using the == operator you're actually checking to see if the references point to the same object in memory. From your debugging screenshot you can see that they are not. this.e points to object Queue#817 while o.e points to Queue#819.
If all you want to do is test for equality, then just override equals and hashCode. You can do it like this (rest of class omitted):
public class MyBST {
private Object e;
public MyBST(Object e) {
this.e = e;
}
public Object getE(){
return e;
}
#Override
public int hashCode() {
return Objects.hashCode(e);
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof MyBST))
return false;
MyBST me = (MyBST) obj;
if (e == null) {
if (me.e != null)
return false;
} else if (!e.equals(me.e))
return false;
return true;
}
}
Implementing Comparable is more involved since you need to check for less, equal, or greater than other instances of MyBST. Unfortunately, the only field in MyBST is an Object which does not tell you anything about its actual fields. So without specific fields with which to test you need to ensure that the Object you pass also implements Comparable. Then you can declare your class like this. Rest of class omitted.
It simply says that
MyBST is comparable.
And the object that is passed in the constructor is comparable.
class MyBST<T extends Comparable<? super T>> implements Comparable<MyBST<T>>{
private T e;
public MyBST(T e){
this.e = e;
}
public T getE(){
return e;
}
#Override
public int compareTo(MyBST<T> o) {
return e.compareTo(o.e);
}
}
The other alternative is to simply pass the actual object type and store it as such, not as Object. Then just implement Comparable in MyBST and use the appropriate fields of the passed object. Lets say the object was an Apple object, you could do this.
class Apple {
String type;
int weight;
}
class MyBST implements Comparable<MyBST> {
private Apple apple;
public MyBST(Apple apple) {
this.apple = apple;
}
#Override
public int compareTo(MyBST e) {
// this could be different depending on how you wanted
// to compare one apple to another. This comparison favors
// type over weight.
// check type - String class implements comparable
int ret = apple.type.compareTo(e.apple.type);
if (ret != 0) {
return ret;
}
// same type so check weight
if (apple.weight < e.apple.weight) {
return -1;
}
if (apple.weight > e.apple.weight) {
return 1;
}
return 0; // equals apples based on criteria
}
}
Finally, you have this.
private Object getE(){
return e;
}
A private getter is not usually very useful. Make it public.
Structure of my class:
public class Priorityy implement Comparable {
public int compareTo(Object pe) {
Priorityy p = (Priorityy) pe;
if (this.key < p.key) {
return 1;
} else if (this.key > p.key) {
return -1;
} else {
return 0;
}
}
}
Th problem is that p.key is always null, why exactly is that? I have my array initialized with elements in it but it always throws NullPointerException whenever I try Arrays.sort(arr).
How can I fix this?
Edit: Here is the complete code and print did print the elements of array arr:
import java.util.Arrays;
class Priorityy implements Comparable {
int size;
int front = 0;
int rear = 0;
static Priorityy[] arr = new Priorityy[3];
int key;
String value;
public Priorityy(int key, String value) {
this.key = key;
this.value = value;
insert();
}
public void insert() {
arr[front] = this;
System.out.println(arr[front].value);
while (front + 1 != 3) {
front = front + 1;
}
}
public Priorityy remove() {
Priorityy x = arr[front];
front = front - 1;
return x;
}
public int compareTo(Object pe) {
Priorityy p = (Priorityy) pe;
if (this.key < p.key) {
System.out.println(p.key);
return 1;
} else if (this.key > p.key) {
System.out.println("3");
return -1;
} else {
System.out.println("4");
return 0;
}
}
public static void main(String... s) {
new Priorityy(10, "Watch");
new Priorityy(40, "Laptop");
new Priorityy(60, "Wallet");
Arrays.sort(arr);
for (Priorityy element : arr) {
System.out.println(element.key);
System.out.println(element.value);
}
}
}
As per your code
Priorityy p = (Priorityy)pe;
^^ ---------- this is null
You have null object in the array. Handle null object gracefully.
For example
if(pe instanceof Priorityy){ // return false for null object
// your code goes here
}
Better use Generic Comparable and use Integer.compare(int,int) to compare two int values.
class Priorityy implements Comparable<Priorityy> {
public int compareTo(Priorityy pe) {
if (pe != null) {
return Integer.compare(this.key, pe.key);
} else {
// return what ever if pe is null
}
}
}
You're putting things into your array in a really strange manner.
But given that, the problem is that you're not using a static field to store the next position to insert an element into, so the next time you create an instance of Priorityy, the field first contains the value zero again. So you're inserting all three objects into element zero of the array.
Change one line of your code and it will work:
int front = 0;
To:
static int front = 0;
I don't see where you are using size and rear but you probably want these to be static too.
One other suggestion: Java has a nice short syntax for increasing or decreasing the value of a variable by one using the ++ or -- operator, so you can shorten things by saying:
front++;
instead of
front = front + 1;
(and front-- instead of front = front - 1)
TreeComparable is a Comparable interface.
The Error:
java.lang.String cannot be cast to TreeComparable
This is the line giving me the error
if (((TreeComparable) r.getInfo()).compareTo((TreeComparable) p.getInfo()) < 0 )
And here is the method for that line:
public void insertBST(Object o) {
ObjectTreeNode p, q;
ObjectTreeNode r = new ObjectTreeNode(o);
if (root == null)
root = r;
else {
p = root;
q = root;
while (q != null) {
p = q;
if (((TreeComparable)(r.getInfo())).compareTo((TreeComparable)(p.getInfo())) < 0 )
q = p.getLeft();
else
q = p.getRight();
}
if (((TreeComparable)(r.getInfo())).compareTo((TreeComparable)(p.getInfo())) < 0)
setLeftChild(p, r);
else
setRightChild(p, r);
}
}
Note: BST stands for binary search tree.
The getInfo method of the ObjectTreeNode class:
private Object info;
public Object getInfo() {
return info;
}
and finally, I don't know if these will help, but my TreeComparable compareTo declaration:
int compareTo(Object o);
and the compareTo method in the (Word) class:
String word;
public int compareTo(Object o) {
Word w = (Word) o;
return this.word.compareTo(w.getWord());
}
The Help is greatly appreciated.
This is because String does not implement the interface TreeComparable. There is a an interface Comparable that String implements. String can be upcasted to this interface.
This question already has an answer here:
unchecked or unsafe operation error
(1 answer)
Closed 8 years ago.
I am trying to modify the compareTo Method so that I can use Comparable Interface here but it's giving me the stated warning. What am I doing wrong ? It shows that there are no syntax errors but as soon as I click on Compile, a small window pops up with a message : " Warnings from last compilation F:\Desktop\PoochyPavy.java uses unchecked or unsafe operations. Recompile with -Xlint:unchecked for details."
The original Code is :
public class PoochyPavy implements Comparable
{
public PoochyPavy(int k)
{
value = k;
}
public int compareTo(Object nerd) //This method doesn’t follow the normal rules
{
PoochyPavy pp = (PoochyPavy) nerd;
int r;
if (value = = 6 * pp.value)
{
r = 136;
}
else
{
r = -137;
}
return r;
}
…other methods…
public int value;
}
My attempt to modify the code :
public int PoochyPavy(int k)
{
int Value = k;
return k;
}
public int compareTo(Comparable nerd) //This method needs modification
{
PoochyPavy pp = (PoochyPavy) nerd;
int r = 0;
if (((Comparable)value).compareTo(6*pp.value)==0)
{
return r;
}
else if (((Comparable)value).compareTo(6*pp.value)<0)
{
return r-1;
}
else if (((Comparable)value).compareTo(6*pp.value)>0)
{
return r+1;
}
return r;
}
//…other methods…
public int value;
Can you try this. Your constructor also seem not proper conflicts with function definition.
public class PoochyPavy implements Comparable<PoochyPavy> {
public PoochyPavy(int k) {
value = k;
}
public int compareTo(PoochyPavy nerd) // This method doesn’t follow the
// normal
// rules
{
PoochyPavy pp = nerd;
int r = 0;
if (((Integer) value).compareTo(6 * pp.value) == 0) {
return r;
} else if (((Integer) value).compareTo(6 * pp.value) < 0) {
return r - 1;
} else if (((Integer) value).compareTo(6 * pp.value) > 0) {
return r + 1;
}
return r;
}
public int value;
}
Basically, I am implementing an AVL tree by reading a set of integers from a text file and then populate the tree by using the add() method. Also, the program is supposed to print in order the set of integers.
As I run the program, a StackOverflowError pops up. I think that this error is being triggered due to something malfunctioning in the add() method.
I would really appreaciate if someone helps me as I am new to this type of programming.
This is part of the Main Class:
public static void main(String[] args) throws FileNotFoundException
{
AVL s1 = new AVL();
Scanner file = new Scanner(new File("C:\\Users\\Dell\\Desktop\\integers.txt"));
while(file.hasNext())
{
// String str = file.next();
//int b = Integer.parseInt(str);
int b = file.nextInt();
s1.add(b);
}
v1.PrintInOrder(v1.root);
These are the add() and PrintInOrder() methods:
public boolean add(int key)
{
root = add(root, key);
return true;
}
private Node add(Node b1, int key)
{
if(b1 == null)
{
return new Node(key);
}
if(key < b1.element){
b1.left = add(b1.left, key);
}
else
{
b1.right = add(b1.right, key);
}
int Left_Height = getHeight(b1.left);
int Right_Height = getHeight(b1.right);
// a height imbalance requires that two subtrees differ by two
if(Math.abs(LeftHeight - RightHeight )== 2)
return Balance(n1);
else
{
n1.ResetHeight();
return b1;
}
}
public void PrintInOrder(Node b1){
if(b1 != null){
PrintInOrder(b1.left);
System.out.println(b1.element);
PrintInOrder(b1.right);
}
}
This is the Node class:
public class Node {
Node left;
Node right;
int element;
int height;
public Node(int keys){
this(keys, null, null);
}
public Node(int d, Node right1, Node left1){
element = d;
height = 0;
left = left1;
right = right1;
}
// This method recalculates the height if the right or left subtrees have been altered
public void ResetHeight(){
int LeftHeight = AVL.getHeight(left);
int RightHeight = AVL.getHeight(right);
height = 1 + Math.max(LeftHeight,RightHeight);
}
Since stack overflows commonly occur in recursion. Use your IDE and set a break at locations where you have done recusion, then debug. Step through it.