1

I want to create a simple Binary Search Tree which uses generics to specify the data type. However, when I want to create a new tree of Integers, I get the following error:

type argument java.lang.Integer is not within bounds of type-variable T

I tried other data types which are clearly extending Comparable, so why is this not working?

Here is my code:

Interface:

public interface Comparable<T>
{
    int compareTo( T t );
}

BinarySearchTree:

public class BinarySearchTree<T extends Comparable<T>>
{
private T content;
private BinarySearchTree<T> leftChild, rightChild;

public BinarySearchTree()
{
    content = null;
    leftChild = null;
    rightChild = null;
}

public T getContent()     
{
    if(!isEmpty())
    {
        return content;
    }
    else
    {
        throw new RuntimeException();
    }
}

public boolean isEmpty()
{
    return content == null;
}

public boolean isLeaf()
{
    return !isEmpty() && leftChild.isEmpty() && rightChild.isEmpty();
}

public void add(T t)
{
    if(isEmpty())
    {
        content = t;
        leftChild = new BinarySearchTree<T>();
        rightChild = new BinarySearchTree<T>();
    }
    else
    {
        if(content.compareTo(t) > 0)
        {
            leftChild.add(t);
        }
        if(content.compareTo(t) < 0)
        {
            rightChild.add(t);
        }
    }
} 

public int size()
{
    if(isEmpty())
    {
        return 0;
    }
    else
    {
        return 1 + leftChild.size() + rightChild.size();
    }
}

public boolean contains(T t)
{
    if(isEmpty())
    {
        return false;
    }
    else
    {
        if(content.compareTo(t) > 0)
            leftChild.contains(t);
        else if(content.compareTo(t) < 0)
            rightChild.contains(t);
        return true;
    }
}

public void show()
{
    if(!isEmpty())
    {
        leftChild.show();
        System.out.println(content);
        rightChild.show();
    }
}

}

Main:

public class main
{

public static void main(String[] args)
{
    test();
}

public static void test()
{
    BinarySearchTree<Integer> tree = new BinarySearchTree<>();
    tree.add("5");
    tree.add("10");
    tree.add("3");
    tree.add("1");
    tree.show();
}
}

The error comes with this line: BinarySearchTree<Integer> tree = new BinarySearchTree<>();

3
  • 2
    Why are you adding Strings to a BinarySearchTree<Integer>? Commented May 25, 2017 at 9:37
  • That's a mistake I didn't fix yet, I used a String Tree before. Commented May 25, 2017 at 9:41
  • Just made a minimal example myself, and it compiles without any problem. If you really did define your own Comparable, then this is indeed exacrly the problem - Integer is-a java.lang.Comparable<Integer>, but you seem to expect it to be of your own Comparable type, and that is not true. It would've been if Java supported duck typing, but it doesn't. Commented May 25, 2017 at 9:44

2 Answers 2

1

This is happening because you've defined your own interface Comparable<T>, of which Integer is not a subtype.

Delete your Comparable, and use the one in java.lang instead.

Also, as Eran pointed out, you shouldn't be adding String values to a BinarySearchTree<Integer>.

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

2 Comments

Thank you! So, my own Interface was overriding the default one, is that what created the error?
Yes. It's generally a bad idea to create your own classes or interfaces with the same names as classes or interfaces in java.lang.
0

You should not create your own Comparable interface. It's a part JDK, you can use it.

Comments

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.