[英]How can i fix my implementation a generic Decrease-Key method in a Min Heap in java?
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:我想减少我正在使用 ArrayList 实现的 Min Heap 数据结构中索引为 i 的元素的值(然后将元素移动到正确的位置),这是它的实现,我将跳过插入, heapSize,Min Heap 实现的方法和其他随机的东西:
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:现在,这是我用来执行 Decrease-Key 操作的方法:
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.我不明白为什么调用比较函数时没有问题,但是当我想在界面中使用我的减法函数来获取第 i 个位置的元素与键之间的减法结果时,java 告诉我参数是 T 类型,它们需要是 Integer 类型才能执行操作,但是当使用 T 参数调用比较函数时,这没有问题。 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.为简单起见,我将 subtract 方法放在 Comparator 类中,我不想为此创建一个类,这看起来也合乎逻辑,但对此不确定。
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.为简单起见,我将 subtract 方法放在 Comparator 类中,我不想为此创建一个类,这看起来也合乎逻辑,但对此不确定。
The Comparator
interface does not declare a subtract
method. Comparator
接口不声明subtract
方法。 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.