Error: 'incompatible types: Comparable can not be converted to T'

0

I need to do TDA of Binary Tree, I have my Node class and my ArbolB class, I need to be parameterized and at the same time use the compareTo method for the method of inserting Node, but I get the error:

incompatible types: Comparable cannot be converted to T
  where T is a type-variable:
    T extends Comparable<T> declared in class ArbolB
public class Nodo<T extends Comparable<T>>{
    T dato;
    Nodo <T> izq,der;

    public Nodo(T dato){
        this.dato = dato;
        this.izq = this.der = null;
    }

    public T getDato() {
        return dato;
    }

    public void setDato(T dato) {
        this.dato = dato;
    }

    public Nodo<T> getIzq() {
        return izq;
    }

    public void setIzq(Nodo<T> izq) {
        this.izq = izq;
    }

    public Nodo<T> getDer() {
        return der;
    }

    public void setDer(Nodo<T> der) {
        this.der = der;
    }

    @Override
    public String toString(){
    return "Su dato es " + dato;
    }

}




    public class ArbolB<T extends Comparable<T>> {

     Nodo raiz;

    public ArbolB() {
        this.raiz = null;
    }

    //metodo insertar
    public Nodo insertar(Nodo A, T nuevo) {
        if (A == null) {
            return new Nodo(nuevo);
        }
        if (nuevo.compareTo(A.getDato()) == 0) {
            return A;
        } else if (nuevo.compareTo(A.getDato()) > 0) {
            A.setDer(insertar(A.getDer(), nuevo));
        } else {
            A.setIzq(insertar(A.getIzq(), nuevo));
        }

        return A;
    }

}
    
asked by Michelle 14.12.2017 в 03:56
source

1 answer

1

In the method insertar of the class ArbolB you are trying to compare two objects that do not have to have the same nature. Since you use parameterization in class Nodo , try defining this method as:

public Nodo<T> insertar(Nodo<T> A, T nuevo) {...}
    
answered by 09.05.2018 в 09:49