为啥我在比较我的优先级队列堆中的两个索引的行上得到空值?
Posted
技术标签:
【中文标题】为啥我在比较我的优先级队列堆中的两个索引的行上得到空值?【英文标题】:Why am I getting null on a line that compares two indices in my priority queue heap?为什么我在比较我的优先级队列堆中的两个索引的行上得到空值? 【发布时间】:2018-11-27 09:33:24 【问题描述】:我正在创建一个类型为 T 的优先级队列堆。当我向堆中添加多个整数时,我在 第 55 行 上得到一个空指针异常,这是 reheapUp 方法使用比较器来决定哪个整数优先。 我已经坚持了几个小时。起初我以为我必须实现一个通用的比较方法,但这没有意义,因为没有足够具体的东西来比较。我使用的比较方法来自一个旧项目,在该项目中我制作了一个比较字符串的二叉搜索树图。
/*
* PQHeap.java
* 11/12/18
*/
import java.util.Comparator;
import java.util.*;
public class PQHeap<T>
private Object[] heap; //hash table
private int heapSize;
private int capacity;
private Comparator<T> comparator;
public PQHeap(Comparator<T> comparator)
heapSize = 0;
capacity = 100;
heap = new Object[capacity];
public int size()
return this.heapSize;
public void add(T obj)
ensureCapacity();
//add to lower right most leaf
heap[heapSize++] = obj;
reheapUp();
public void ensureCapacity()
if(heapSize < heap.length/2)
return;
Object newHeap[] = new Object[2*heap.length];
for(int i=0; i<heap.length; i++)
newHeap[i] = heap[i];
heap = newHeap;
@SuppressWarnings("unchecked")
private void reheapUp()
int outOfPlaceInd = heapSize - 1;
while(outOfPlaceInd > 0)
int parentInd = (outOfPlaceInd - 1)/2;
**if (comparator.compare((T)heap[outOfPlaceInd], (T)heap[parentInd]) < 0)**
swap(outOfPlaceInd, parentInd);
outOfPlaceInd = (outOfPlaceInd-1)/2;
else
return;
private void swap(int i, int j)
Object copy = heap[i];
heap[i] = heap[j];
heap[j] = copy;
@SuppressWarnings("unchecked")
public T remove()
if(heapSize == 0)
throw new IllegalStateException("Trying to remove from an empty PQ!");
Object p = heap[0];
heap[0] = heap[--heapSize];
reheapDown();
return (T)p;
@SuppressWarnings("unchecked")
private void reheapDown()
int outOfPlaceInd = 0;
int leftInd = 2*outOfPlaceInd+1; //left child
int rightInd = 2*outOfPlaceInd+2; //right child
while(leftInd <= heapSize-1)
int smallerChildInd = leftInd;
if ((rightInd < heapSize) && (comparator.compare((T)heap[rightInd], (T)heap[leftInd]) < 0))
smallerChildInd = rightInd;
// is the parent smaller or equal to the smaller child
int compare = comparator.compare((T)heap[outOfPlaceInd], (T)heap[smallerChildInd]);
// if the parent is larger than the child...swap with smaller child
if (compare > 0)
swap(outOfPlaceInd, smallerChildInd);
// update indices
outOfPlaceInd = smallerChildInd;
leftInd = 2*outOfPlaceInd + 1;
rightInd = 2*outOfPlaceInd + 2;
else
return;
public static void main( String[] args )
PQHeap<Integer> pq = new PQHeap<Integer>(new TestIntComparator());
pq.add( 10 );
pq.add( 20 );
System.out.println(pq.size());
pq.add( 20 );
pq.add( 30 );
class TestIntComparator implements Comparator<Integer>
public TestIntComparator() ;
public int compare(Integer o1, Integer o2)
return o1-o2;
// class NaturalComparator<T extends Comparable<T>> implements Comparator<T>
// public int compar(T a, T b)
// return a.compareTo(b);
//
//
【问题讨论】:
因为您没有将传入构造函数的比较器分配给相应的类属性以供以后使用? 好的,那会是什么样子? 【参考方案1】:在 PQHeap 构造函数中,您不会将输入比较器对象分配给类字段。像这样添加行:
this.comparator = comparator;
在你的构造函数中
【讨论】:
我很惊讶,您在 2 秒内解决了我的问题。谢谢! @Subwoofer 如果您觉得它有帮助并解决了您的问题,请点击已解决的按钮分享您的爱 ;)以上是关于为啥我在比较我的优先级队列堆中的两个索引的行上得到空值?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我在 IOS 上得到 Mapboxgl api 的空白页面?
手写 asm.js - 你如何跟踪堆中的 javascript 对象?