i keep getting this error for it. please help.
BST.java:23: operator <= cannot be applied to int,java.lang.Object
public void add(int element)
{
boolean done = false;
BSTNode cursor = root;
BSTNode newE = new BSTNode(element, null, null);
if (cursor == null)
{
// the BSTNode is empty
root = newE;
}
else
{
while (!done)
{
if (element <= cursor.getData()) //error right here
{
if (cursor.getLeft() == null)
{
cursor.setLeft(newE);
done = true;
}
else
{
cursor = cursor.getLeft();
}
}
else
{
// go to the right branch
if (cursor.getRight() == null)
{
cursor.setRight(newE);
done = true;
}
else
{
cursor = cursor.getRight();
}
}
}
}
}
and here is my BSTNode code
import java.io.*;
import java.util.Scanner;
public class BSTNode<E>
{
private E data;
private BSTNode<E> left, right;
public BSTNode(E initialData, BSTNode<E> initialLeft, BSTNode<E> initialRight)
{
data = initialData;
left = initialLeft;
right = initialRight;
}
// Getting and Setting Data Links
public E getData()
{
return data;
}
public BSTNode<E> getLeft()
{
return left;
}
public BSTNode<E> getRight()
{
return right;
}
public void setData(E newData)
{
data = newData;
}
public void setLeft(BSTNode<E> newLeft)
{
left = newLeft;
}
public void setRight(BSTNode<E> newRight)
{
right = newRight;
}
public BSTNode<E> removeLeftmost()
{
// the node that activates removeLeftmost has no left child, thus is itself the leftmost node of the subtree
if (left == null)
{
return right;
}
else
{
// a recursive call removes the leftmost node from my left subtree
left = left.removeLeftmost();
return this;
}
}
public BSTNode<E> removeRightmost()
{
// the node that activates removeLeftmost has no left child, thus is itself the leftmost node of the subtree
if (right == null)
{
return left;
}
else
{
// a recursive call removes the leftmost node from my left subtree
right = right.removeLeftmost();
return this;
}
}
public E getLeftmostData()
{
if(left == null)
return null;
else
return left.getLeftmostData();
}
public E getRightmostData()
{
if(right == null)
return null;
else
return right.getRightmostData();
}
public void preOrderPrint()
{
System.out.println(data);
if (left != null)
left.preOrderPrint();
if (right != null)
right.preOrderPrint();
}
public void postOrderPrint()
{
if(left != null)
left.postOrderPrint();
if(right != null)
right.postOrderPrint();
System.out.println(data);
}
public void inOrderPrint()
{
if(left != null)
left.inOrderPrint();
System.out.println(data);
if(right != null)
right.inOrderPrint();
}
}
thanks.

New Topic/Question
Reply




MultiQuote



|