Java 中的 MaxHeap 实现无法正常工作
MaxHeap implementation in Java doesn't work properly
我通过扩展 ArrayList 实现了堆。然而,它似乎作为 minheap 工作得很好(与这段代码几乎没有区别),但它不能作为 maxheap 正常工作。我认为我有一部分或一部分使用不当。我想知道哪里错了或被误解了。
如果有更好的方法,请指教,不胜感激。
class Heap<T extends Comparable<T>> extends ArrayList<T> {
public void insert(T elem) {
this.add(elem);
int idx = this.size() - 1;
if(idx > 0 && this.compare(idx, (idx - 1) / 2)){
Collections.swap(this, idx, (idx - 1) / 2);
idx = (idx - 1) / 2;
}
}
public void removeTop() {
if(this.size() == 1) {
this.remove(0);
return;
}
this.set(0, this.remove(this.size() - 1));
int here = 0;
while(true) {
int left = here * 2 + 1;
int right = here * 2 + 2;
if(left >= this.size()) break;
int next = here;
if(!this.compare(next, left)) {
next = left;
}
if(right < this.size() && !this.compare(next, right)){
next = right;
}
if(next == here) break;
Collections.swap(this, next, here);
here = next;
}
}
private void swap(int idx1, int idx2) {
T temp = this.get(idx1);
this.set(idx1, this.get(idx2));
this.set(idx2, temp);
}
private boolean compare(int idx1, int idx2) {
return this.get(idx1).compareTo(this.get(idx2)) >= 0;
}
}
(+) compare
方法用于根据类型比较两个元素。我想在堆初始化时得到一种 Compare function
。喜欢...
Heap<Integer> heap = new Heap<Integer>(new SomekindofCompareFunction());
Java可以吗?
使用 Comparator
:
class Heap<T> extends ArrayList<T> {
private final Comparator<T> comparator;
public Heap(Comparator<T> comparator) {
this.comparator = comparator;
}
...
private boolean compare(int idx1, int idx2) {
return comparator.compare(get(idx1), get(idx2)) >= 0;
}
}
您可以这样创建 Heap
:
Heap<Integer> heap = new Heap<Integer>((a,b) -> a.compareTo(b));
我通过扩展 ArrayList 实现了堆。然而,它似乎作为 minheap 工作得很好(与这段代码几乎没有区别),但它不能作为 maxheap 正常工作。我认为我有一部分或一部分使用不当。我想知道哪里错了或被误解了。
如果有更好的方法,请指教,不胜感激。
class Heap<T extends Comparable<T>> extends ArrayList<T> {
public void insert(T elem) {
this.add(elem);
int idx = this.size() - 1;
if(idx > 0 && this.compare(idx, (idx - 1) / 2)){
Collections.swap(this, idx, (idx - 1) / 2);
idx = (idx - 1) / 2;
}
}
public void removeTop() {
if(this.size() == 1) {
this.remove(0);
return;
}
this.set(0, this.remove(this.size() - 1));
int here = 0;
while(true) {
int left = here * 2 + 1;
int right = here * 2 + 2;
if(left >= this.size()) break;
int next = here;
if(!this.compare(next, left)) {
next = left;
}
if(right < this.size() && !this.compare(next, right)){
next = right;
}
if(next == here) break;
Collections.swap(this, next, here);
here = next;
}
}
private void swap(int idx1, int idx2) {
T temp = this.get(idx1);
this.set(idx1, this.get(idx2));
this.set(idx2, temp);
}
private boolean compare(int idx1, int idx2) {
return this.get(idx1).compareTo(this.get(idx2)) >= 0;
}
}
(+) compare
方法用于根据类型比较两个元素。我想在堆初始化时得到一种 Compare function
。喜欢...
Heap<Integer> heap = new Heap<Integer>(new SomekindofCompareFunction());
Java可以吗?
使用 Comparator
:
class Heap<T> extends ArrayList<T> {
private final Comparator<T> comparator;
public Heap(Comparator<T> comparator) {
this.comparator = comparator;
}
...
private boolean compare(int idx1, int idx2) {
return comparator.compare(get(idx1), get(idx2)) >= 0;
}
}
您可以这样创建 Heap
:
Heap<Integer> heap = new Heap<Integer>((a,b) -> a.compareTo(b));