【发布时间】:2019-02-26 20:48:27
【问题描述】:
我创建了一个优先级队列,它可以按顺序输入项目并按顺序删除它们。即使两个数字具有相同的优先级,它也会删除第一个输入的数字。
如果存在三个具有相同优先级的数字,则不会删除第一个。我将如何去做,还是应该这样做?
出队函数:
public void deQueue(Animal item)
{
item = items.elements[0];
items.elements[0] = items.elements[numItems - 1];
numItems--;
items.ReheapDown(0, numItems - 1);
}
ReheapDown 函数:
public void ReheapDown(int root, int bottom)
{
int maxchild, rightchild, leftchild;
leftchild = root * 2 + 1;
rightchild = root * 2 + 2;
if (leftchild <= bottom)
{
if (leftchild == bottom)
maxchild = leftchild;
else
{
if (elements[leftchild].priority <= elements[rightchild].priority)
maxchild = rightchild;
else
maxchild = leftchild;
}
if (elements[root].priority < elements[maxchild].priority)
{
Swap(elements, root, maxchild);
ReheapDown(maxchild, bottom);
}
}
}
【问题讨论】:
标签: c# queue priority-queue