내 문제는 기능보다 의미가 있습니다. 코드가 deQueue 및 enQueue 함수를 올바르게 구현 한 것처럼 보입니다.우선 순위 큐에서 올바른 힙 구현
reheapDown 및 reheapUp 기능을 잘못 사용하고있다, 그리고 나는 문제가 내 힙 기능
아이디어는 환자 개체가 제거 될 때 그렇게 간단한 우선 순위 큐 시스템을 재배치하는 것입니다package priqueue;
public class Hosheap{
private Patient[] elements;
private int numElements;
public Hosheap(int maxSize)
{
elements= new Patient[maxSize];
numElements=maxSize;
}
public void ReheapDown(int root,int bottom)
{
int maxChild;
int rightChild;
int leftChild;
leftChild=root*2+1;
rightChild=root*2+2;
if (leftChild<=bottom)
{
if(leftChild==bottom)
maxChild=leftChild;
else
{
if(elements[leftChild].getPriority() <= elements[rightChild].getPriority())
maxChild=rightChild;
else
maxChild=leftChild;
}
if(elements[root].getPriority()<elements[maxChild].getPriority())
{
Swap(root,maxChild);
ReheapDown(maxChild,bottom);
}
}
}
public void ReheapUp(int root,int bottom)
{
int parent;
if(bottom>root)
{
parent=(bottom-1)/2;
if(elements[parent].getPriority()<elements[bottom].getPriority())
{
Swap(parent,bottom);
ReheapUp(root,parent);
}
}
}
public void Swap(int Pos1, int Pos2)
{
Patient temp;
temp = elements[Pos1];
elements[Pos1]=elements[Pos2];
elements[Pos2]=temp;
}
public Patient getElement(int e)
{
return elements[e];
}
public void setElement(Patient p, int n)
{
elements[n]=p;
}
}
, ReheapUp에있다 생각 또는 아래로 올바르게 코드를 수행하지 않는 큐를 재정렬합니다. 우선 순위 큐 코드를 포함해야합니까, 아니면 이미 너무 길습니까?
NetBeans IDE 6.0.1을 사용하고 있습니다.
현재이 간단하지만 효율적인 구현 http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=standardTemplateLibrary2#priority을 확인할 수 있습니다. – Dimitris