operator '<' undefined for type K
Hey,
Trying to implement a binary search tree.
I can't get the search feature working...
I keep getting this error:
The operator < is undefinedfor the argument type(s) K, KBinarySearchTree/src/myPackageBSTree.javaline 35
similarly for'>'
http://www.darkdayssoftware.com/BSTree.java
http://www.darkdayssoftware.com/TreeNode.java
Any Help much appreciated.
The > (and <, ==, >=, <=) operator(s) can only be used to compare primitive data types.
K probably (or should) implement the Comparable interface, so you should do it like this:K.compareTo(K) > 0 // first K is more than the second K (the parameter)
Without wishing to state the obvious, the error message is simply telling you that the two operators cannot be used to make comparisons between objects of that particular class. Remember that Java - unlike C++ - does not support operator overloading so it is not always possible to use all of the operators to perform comparisons between different objects. It is quite likely that you would see a similar message if you tried to compare two Strings using these operators.