How can i fix my implementation a generic Decrease-Key method in a Min Heap in java?

48 Views Asked by At

I want to decrease the value of an element with index i in my Min Heap data structure that i am implementing using an ArrayList (and then move the element in the correct position), here is the implementation of it, i will skip the insert, heapSize, method for the Min Heap implementation and other random stuff:


public class MinHeap<T> {
   
    private List<T> MinHeap = null;
    private Comparator<? super T> comparator;
    private int size; 
   

    public MinHeap(Comparator<? super T> comparator) throws MinHeapException {
        if(comparator==null) throw new MinHeapException("MinHeap constructor: comparator parameter cannot be null");
        MinHeap = new ArrayList<>();
        size = 0; 
        this.comparator = comparator;
    }
}

To compare the generic elements, in this case Integers, i am using a comparator Class as follows:

public class IntegerComparator implements Comparator<Integer>{
  @Override
  public int compare(Integer x, Integer y) {
    
    int result = Integer.valueOf(x).compareTo(Integer.valueOf(y));
    System.out.println(result);
    return result;

   }

   public int subtract(Integer x, Integer y){
       
       int result = Integer.valueOf(x) - Integer.valueOf(y);
       return result;
   }
}

Now, this is the method that i use to perform the Decrease-Key operation:

    public void decreaseKey(int i, T key) {
        //here no error, no problem
        if ((this.comparator).compare(MinHeap.get(i), key) < 0 ) {
            throw new IllegalArgumentException("Key is larger than the original key");
        }


        //here is the error. 
        int result = (this.comparator).subtract(MinHeap.get(i), key) ;

        MinHeap.set(i, result);

        int parent = parent(i);

        while (i > 0 && (this.comparator).compare(MinHeap.get(parent), MinHeap.get(i)) > 0) {

            swap(i, parent);
            i = parent;
            parent = parent(parent);
        }
    }

I am not understanding why when the compare function gets called there is no problem, but when i want to use my subtract function in the interface to get the result of the subtraction between the element at the i position and the key, java tells me that the parameters are of type T and they need to be of type Integer to perform the operation, but when the compare function gets called with T parameters , there is no problem in that. How am i supposed to perform a generic subtraction for my method? Below i will put the main of my program:

public class Main {
    public static void main(String[] args) throws MinHeapException{
        
        MinHeap<Integer> heap = new MinHeap<>(new IntegerComparator());
        heap.insert(3);
        heap.insert(14);
        heap.insert(32);
        heap.insert(12);
        heap.insert(11);
        heap.insert(1);
        heap.insert(4);
        //array will be : 1, 11, 3, 14, 12, 32, 4
        
        heap.printHeap();
        
        heap.decreaseKey(4, 2);

        heap.printHeap();
    
    }

}

I have put the subtract method in the Comparator class for simplicity, i didnt want to create a class only for that, also it seemed logical, but not sure about this.

I have put the subtract method in the Comparator class for simplicity, i didnt want to create a class only for that, also it seemed logical, but not sure about this.

1

There are 1 best solutions below

0
On

The Comparator interface does not declare a subtract method. Even if a specific implementation of it has it, you cannot call it without casting. You can define your own interface instead.

public interface HeapComparator<T> extends Comparator<T> {
    int subtract(T x, T y);
}
public class IntegerComparator implements HeapComparator<Integer> {
    @Override
    public int compare(Integer x, Integer y) {
        int result = Integer.valueOf(x).compareTo(Integer.valueOf(y));
        System.out.println(result);
        return result;
    }

    @Override
    public int subtract(Integer x, Integer y) {
        int result = Integer.valueOf(x) - Integer.valueOf(y);
        return result;
    }
}
// ...
public class MinHeap<T> {
    private List<T> MinHeap = null;
    private HeapComparator<? super T> comparator;