0

Here are the Classes I'm using...

public class MyBinarySearchTreePlus<T extends KeyedItem<KT>, KT extends Comparable<? super KT>>
        extends MyBinarySearchTree<T, KT> implements BSTPInterface<T, KT> {
    public MyBinarySearchTreePlus() {
        super();
    }
}

.

public class MyBinarySearchTree<T extends KeyedItem<KT>, KT extends Comparable<? super KT>>
            extends BinaryTreeBasis<T> {

    public MyBinarySearchTree() {}
}  

.

public abstract class BinaryTreeBasis<T> {
    protected TreeNode<T> root;

    public BinaryTreeBasis() {
      root = null;
    }
}

.

public abstract class KeyedItem<KT extends
                                 Comparable<? super KT>> {
    private KT searchKey;

    public KeyedItem(KT key) {
        searchKey = key;
    }
}

Nothing I try will create a BinarySearchTree, I try:

MyBinarySearchTreePlus<Integer, Integer> tree = new MyBinarySearchTreePlus<Integer, Integer>();

I always get the error (coming from the first parameter):

Bound mismatch: The type Integer is not a valid substitute for the bounded 
parameter <T extends KeyedItem<KT>> of the type MyBinarySearchTreePlus<T,KT>

What kind of Object is is T extends KeyedItem looking for? It doesn't compile with Comparable Objects, non-Comparable Objects, primitive Objects. So what exactly should I be using here? I don't understand why there's even 2 in the first place, obviously 1 is the type you are storing in the tree but whats's the other 1? I also have a TreeNode class of type T if that makes any difference.

Mike Naples
  • 57
  • 1
  • 8
  • Please how us how you try to instantiate the `BinarySearchTree`. – Turing85 Apr 30 '18 at 17:42
  • Updated with new info. – Mike Naples Apr 30 '18 at 17:52
  • 1
    The problem is your first generic parameter. It has to be a type that inherits from `KeyedItem` and `Integer` does not inherit from `KeyedItem`. – Turing85 Apr 30 '18 at 17:55
  • `T extends KeyedItem` is _only going to accept a `KeyedItem`_. It won't accept anything that doesn't extend that. It won't accept any built-in classes, for example. Only custom classes you make extending `KeyedItem` would work. – Louis Wasserman Apr 30 '18 at 17:55
  • Ok, maybe I need to read more about BST's because what's the purpose of having a keyed item? I thought they only stored items in a structured fashion, BST's dont store searching keys for each value do they? – Mike Naples Apr 30 '18 at 18:02
  • BSTs certainly store whatever keys they need to search in themselves. – Louis Wasserman Apr 30 '18 at 18:07
  • Why is a key used if the data is structured? I thought the idea of BST is that larger items are on right and vice versa, I thought it was that order that allows us to search in logarithmic time. So how is the search key used? – Mike Naples Apr 30 '18 at 18:19
  • The key is the part of the node used for comparison/sorting. For instance, you can have a tree of tasks like (priority, name) where the priority is the key, and the BST will sort them by priority and ignore the name. – Sean Van Gorder May 01 '18 at 21:09

0 Answers0