Java 堆优先级队列实现
我正在编写一个涉及堆实现的代码,在我的while循环行中,我的bubbleUp方法中似乎出现了一个取消引用的错误。这可能是一个相当基本的问题,但解决这个问题的最佳方法是什么?我在实现removeHigh方法时也遇到了一些问题,该方法用于从队列中删除最高的元素Java 堆优先级队列实现,java,heap,priority-queue,Java,Heap,Priority Queue,我正在编写一个涉及堆实现的代码,在我的while循环行中,我的bubbleUp方法中似乎出现了一个取消引用的错误。这可能是一个相当基本的问题,但解决这个问题的最佳方法是什么?我在实现removeHigh方法时也遇到了一些问题,该方法用于从队列中删除最高的元素 public class HeapPriorityQueue implements PriorityQueue { protected final static int DEFAULT_SIZE = 10000; pro
public class HeapPriorityQueue implements PriorityQueue
{
protected final static int DEFAULT_SIZE = 10000;
protected Comparable[] storage;
protected int currentSize;
public HeapPriorityQueue ()
{
this.storage=new Comparable[DEFAULT_SIZE];
this.currentSize=0;
}
public HeapPriorityQueue(int size)
{
this.currentSize=size;
this.storage=new Comparable[currentSize];
}
public int size ()
{
return currentSize;
}
public boolean isEmpty ( )
{
if(currentSize==0)
return true;
else
return false;
}
public boolean isFull ( )
{
if(currentSize==DEFAULT_SIZE)
return true;
else
return false;
}
public Comparable removeHigh () throws HeapEmptyException
{
if(isEmpty()) {
throw new HeapEmptyException();
}else{
Comparable[] k = new Comparable[0];
k.equals(storage[1]);
storage[1] = storage[currentSize];
storage[currentSize] = null;
currentSize--;
bubbleDown();
return storage[currentSize];
}
}
public void insert ( Comparable k ) throws HeapFullException
{
if(isFull()) {
throw new HeapFullException();
}
currentSize++;
int index = currentSize;
storage[index] = k;
bubbleUp();
}
protected void bubbleUp ( )
{
int index = this.currentSize;
while(parent(index).compareTo(storage[index]) > 0) {
swapElement(index, parent(index));
index = parent(index);
}
}
protected void bubbleDown()
{
int index = 1;
while (hasLeft(index)) {
int smaller = leftChild(index);
if(hasRight(index) &&
storage[leftChild(index)].compareTo(storage[rightChild(index)])>0) {
smaller = rightChild(index);
}
if(storage[index].compareTo(storage[smaller]) > 0) {
swapElement(index, smaller);
}else{
break;
}
}
}
protected void swapElement ( int p1, int p2 )
{
Comparable k = storage[p1];
storage[p1] = storage[p2];
storage[p2] = k;
}
protected int parent ( int pos )
{
if(pos>1)
return pos;
else
return 0;
}
protected int leftChild ( int pos )
{
return pos*2;
}
protected int rightChild ( int pos )
{
return pos*2+1;
}
protected boolean hasLeft ( int pos )
{
if(leftChild(pos) <= currentSize)
return true;
else
return false;
}
protected boolean hasRight ( int pos )
{
if(rightChild(pos) <= currentSize)
return true;
else
return false;
}
}
public类HeapPriorityQueue实现PriorityQueue
{
受保护的最终静态int默认_大小=10000;
受保护的可比存储;
受保护的int电流大小;
公共HEAPPERRITYQUEUE()
{
this.storage=新的可比较[默认大小];
这个.currentSize=0;
}
公共HeapPriorityQueue(整数大小)
{
这个.currentSize=size;
此。存储=新的可比[当前大小];
}
公共整数大小()
{
返回电流大小;
}
公共布尔值为空()
{
如果(currentSize==0)
返回true;
其他的
返回false;
}
公共布尔值已满()
{
如果(当前大小==默认大小)
返回true;
其他的
返回false;
}
public compariable removehight()抛出heapmptyexception
{
if(isEmpty()){
抛出新的HeapImptyException();
}否则{
可比[]k=新可比[0];
k、 等于(存储[1]);
存储器[1]=存储器[currentSize];
存储[currentSize]=空;
当前大小--;
泡泡镇();
返回存储器[当前大小];
}
}
公共无效插入(可比k)引发堆填充异常
{
如果(isFull()){
抛出新的HeapFullException();
}
currentSize++;
int index=当前大小;
存储[索引]=k;
泡泡蛋白();
}
受保护的空气泡()
{
int index=this.currentSize;
while(父(索引).compareTo(存储[索引])>0){
swapElement(指数、母指数);
索引=父(索引);
}
}
受保护的void bubbleDown()
{
int指数=1;
while(hasleet(索引)){
int较小=leftChild(索引);
如果(指数),
存储[leftChild(索引)]。比较(存储[rightChild(索引)])>0){
较小=右子女(索引);
}
if(存储[索引]).compareTo(存储[更小])>0{
Swaplement(指数,较小);
}否则{
打破
}
}
}
受保护的孔隙斜楔(内部p1、内部p2)
{
可比k=存储量[p1];
存储器[p1]=存储器[p2];
存储[p2]=k;
}
受保护的int父级(int pos)
{
如果(位置>1)
返回pos;
其他的
返回0;
}
受保护的int-leftChild(int-pos)
{
返回位置*2;
}
受保护的int rightChild(int pos)
{
返回位置*2+1;
}
受保护的布尔hasleet(int-pos)
{
假设(leftChild(pos)交换元素后,parent(index)
的结果将更改。请重试
protected void bubbleUp() {
int index = this.currentSize;
int pi;
while((pi = parent(index)).compareTo(storage[index]) > 0) {
swapElement(index, pi);
index = pi;
}
}