2

I'm trying to implement a Binary Tree in java with Generics, i searched and i find this question: Implementing Binary Tree in Java with Generic Comparable<T> data?, but i couldn't resolve my doubts. So i have two classes,

BST_Tree<T> 

and

Node<T extends Comparable<T>> 

I want that my implementation can:

  1. Take every type of Object and put it inside the field key in Node

  2. Compare every node with the key field

This is the code:

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

    private T key;
    private Node left;
    private Node right;
    private Node p;

    public void setKey(T key) {
        this.key = key;
    }

    public T getKey() {
        return key;
    }

    public Node getLeft() {
        return left;
    }

    public Node getRight() {
        return right;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public void setRight(Node right) {
        this.right = right;
    }

    public void setP(Node p) {
        this.p = p;
    }

    public boolean getBolCompMin(T key) {
        return this.key.compareTo(key) < 0;
    }
}

My Node class is suppose to extend Comparable in order to compare the key.

This is my tree:

public class BST_Tree < T > {

    private ArrayList < Node > nodes;
    private Node root;

    public BST_Tree(Node root) {
        this.root = root;
    }

    public void insertNode(T key) {
        Node z = new Node();
        z.setKey(key);
        Node x = this.root;
        Node y = new Node();

        while (x != null) {

            y = x;
            if (z.getBolCompMin(x.getKey())) {
                x = x.getLeft();
            } else {
                x = x.getRight();
            }
        }

        z.setP(y);

        if (z.getBolCompMin(y.getKey())) {
            y.setLeft(z);
        } else {

            y.setRight(z);
        }
    }
    public void InOderWalk(Node x) {
        if (x != null) {
            InOderWalk(x.getLeft());
            System.out.println(x.getKey());
            InOderWalk(x.getRight());
        }
    }

    public Node getRoot() {
        return root;
    }
}

My tree tries to set the key in node z but it fails. This is the error:

incompatible types: T cannot be converted to java.lang.Comparable

Thank you in advance!

3
  • BST_Tree = BinarySearchTree_Tree Commented Oct 17, 2015 at 14:54
  • 1
    for best results use <T extends Comparable<? super T>> Commented Oct 18, 2015 at 11:03
  • can i ask you why? Thank you by the way Commented Oct 18, 2015 at 13:40

1 Answer 1

1

Your

public class BST_Tree<T>

should be

public class BST_Tree<T extends Comparable<T>>

And every Node variable inside your BST_Tree and Node classes should be Node<T>.

This would ensure that you can only instantiate your BST_Tree class with element types that implement Comparable.

Sign up to request clarification or add additional context in comments.

6 Comments

what about the method getBolCompMin(T key) that is in Node and, of course, can works only if Node extends comparable. By the way that method return true if a key is minor of the currenct Node's key (this.key)
@Francesco I still don't understand what you asked in your comment. getBolCompMin looks fine to me.
ok, it can work only if Node extends Comparable since it uses a method in Comparable (.compareTo)
@Francesco You mean it can work only if T implements Comparable<T>, which it does.
so you mean that booth my class have to implements Comparable<T>?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.