0
import java.util.Optional;

public class InOrderSuccessorBinaryTree<K,V> extends SimpleBinaryTree<K,V> {
    public Optional<K> inOrderSuccessorKey(K key) {
           BinaryTreeNode<K,V> curr = this.root;
            ...
           if (key < curr.getKey())
            ...
}

I don't understand this error: Tree.javaInOrderSuccessorBinaryTree.java:13: error: bad operand types for binary operator '<', and I'm not experienced with java I want to know how to compare generic values, specifically K How do I update this class to compare Ks?

import java.util.Optional;

public class BinaryTreeNode<K,V> {
    private BinaryTreeNode<K,V> left;
    private BinaryTreeNode<K,V> right;
    private K key;
    private V value;
    ...
    public K getKey() {
        return key;
    }

    public void setKey(K key) {
        this.key = key;
    }
    ...
}
  • The reason is `K` and `V` are unbounded non-primitive compatible type parameters. You can't use non-primitive (int, long etc) or wrapper types (Integer, Long etc) with relational operators like <. For ex: if you make your types bounded like ``, it will compile (but very confusing). – Laksitha Ranasingha Dec 05 '21 at 20:50
  • Look at these https://stackoverflow.com/questions/24556796/why-cant-we-use-relational-operators-on-string, https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html – Laksitha Ranasingha Dec 05 '21 at 20:51
  • @LaksithaRanasingha I know the reason, I just want to know how to compare them – Falling Forward Dec 05 '21 at 20:55
  • 1
    Well, you asked `I don't understand this error: Tree.javaInOrderSuccessorBinaryTree.java:13: error: bad operand types for binary operator '<'`? – Laksitha Ranasingha Dec 05 '21 at 21:16

0 Answers0