public E findMin()
{
ThreadedTreeNode cursor = root;
while(cursor.leftChild != null)
{
cursor = cursor.leftChild;
}
return cursor.data;
}
[java] Why does this not compile?
I have this section of code:
and I get this error.
ThreadedTree.java:24: incompatible types
found : java.lang.Comparable
required: E
return cursor.data;
If I return, for example, root.data (both root and cursor are the same type of nodes) it will compile. There is something obvious I'm sure I'm missing, but I'm not sure what it is. Any ideas?
Impossible to tell, with only that much code. Post the definition of ThreadedTreeNode, as well as all other classes involved in the type determination.
The ThreadedTreeNode class is pretty bare bones.
Those are the fields. The only other function is the constructor.
class ThreadedTreeNode<E extends Comparable<E>>{ E data; // The data in the node. ThreadedTreeNode<E> leftChild; // Left child. ThreadedTreeNode<E> rightChild; // Right child}
Those are the fields. The only other function is the constructor.
The cursor variable in your method needs to be declared as type ThreadedTreeNode<E>, rather than just ThreadedTreeNode:
The root member should be declared the same.
public E findMin(){ ThreadedTreeNode<E> cursor = root; while(cursor.leftChild != null) { cursor = cursor.leftChild; } return cursor.data;}
The root member should be declared the same.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement