Heres the thing, I have a class call HoldQueue it needs to hold a list of another class LeftistHeaps which will be a heap of another class TaskObject. LeftistHeap has to be defined as generic, but when implemented in HoldQueue is when it will be given TaskObject as the "data type" its storing. The problem lies in that I nee to make sure when I use a merge method defined in LeftistHeap that the root is highest priority out of the two heaps to be merged, thus the compare. So far Ive done this:
public class LeftistHeap<E extends Comparable<? super E>> {
private HeapNode<E> root;
private int count;
public LeftistHeap() {
root = null;
}
public void merge(LeftistHeap<E> newHeap) {
if(this == newHeap) // Avoid aliasing problems
return;
root = merge(root, newHeap.root);
newHeap.root = null;
}
private HeapNode<E> merge(HeapNode<E> heap1, HeapNode<E> heap2) {
if(heap2 == null)
return heap1;
if(heap1 == null)
return heap2;
if(heap1.data.compareTo(heap2.data) < 0)<-------------single compare line
return mergeSmall(heap1, heap2);
else
return mergeSmall(heap2, heap1);
}
private HeapNode<E> mergeSmall(HeapNode<E> heap1, HeapNode<E> heap2) {
if(heap1.left == null) // Single node
heap1.left = heap2; // Other fields in h1 already accurate
else {
heap1.right = merge(heap1.right, heap2);
if(heap1.left.nullPath < heap1.right.nullPath)
swapChildren(heap1);
heap1.nullPath = heap1.right.nullPath + 1;
}
return heap1;
}
}//end LeftistHeap
The problem lies in the compare. When I implement LeftistHeap I get an error:
LeftistHeap<TaskObject> heap = new LeftistHeap<TaskObject>();
this gives the error:
Bound mismatch: The type TaskObject is not a valid substitute for the bounded parameter <E extends Comparable<? super E>> of the type LeftistHeap<E>
Now if I delete <E extends Comparable<? super E> and replace it with just <E> in my LeftistHeap code the single compareTo becomes the issue and becomes a error.
Is there away to work around this?