views:

43

answers:

2

I'm getting a "java.lang.string cannot be cast to node" exception. I thought of converting the localRoot to a string using a provided toString method then comparing them, nut this leaves no concept of 'greater than' or 'less than' which I need to navigate the BST...

 int computeResult = ((Node<E>)o).compareTo(localRoot);

where o is of type Object localRoot is of type Node compareTo method takes a Node

A: 

Funny, I spent some time this weekend working on a Java binary tree implementation. See what you think of it.

duffymo
A: 

String does have a compareTo method that does have a concept of greater than and less than

So you could write that line of code as:

int computeResult = o.toString().compareTo(localRoot.toString());

From The Javadocs for java.lang.string:

public int compareTo(String anotherString)

Specified by: compareTo in interface Comparable

Parameters: anotherString - the String to be compared.

Returns: the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

instanceofTom