美好的一天,我找到了这个优先级队列实现,我正在尝试获得它的最小版本(而不是max)。我不知道从哪里开始。我尝试混合功能的迹象(天真的尝试)但它并没有让我走得太远。任何有关如何实现它的帮助以及解释它的一些文字都非常受欢迎。来源如下: 注意我已经留下了它的评论
#include <iostream>
#include <vector>
#include <assert.h>
using namespace std;
class PriorityQueue
{
vector<int> pq_keys;
void shiftRight(int low, int high);
void shiftLeft(int low, int high);
void buildHeap();
public:
PriorityQueue(){}
PriorityQueue(vector<int>& items)
{
pq_keys = items;
buildHeap();
}
/*Insert a new item into the priority queue*/
void enqueue(int item);
/*Get the maximum element from the priority queue*/
int dequeue();
/*Just for testing*/
void print();
};
void PriorityQueue::enqueue(int item)
{
pq_keys.push_back(item);
shiftLeft(0, pq_keys.size() - 1);
return;
}
int PriorityQueue::dequeue()
{
assert(pq_keys.size() != 0);
int last = pq_keys.size() - 1;
int tmp = pq_keys[0];
pq_keys[0] = pq_keys[last];
pq_keys[last] = tmp;
pq_keys.pop_back();
shiftRight(0, last-1);
return tmp;
}
void PriorityQueue::print()
{
int size = pq_keys.size();
for (int i = 0; i < size; ++i)
cout << pq_keys[i] << " ";
cout << endl;
}
void PriorityQueue::shiftLeft(int low, int high)
{
int childIdx = high;
while (childIdx > low)
{
int parentIdx = (childIdx-1)/2;
/*if child is bigger than parent we need to swap*/
if (pq_keys[childIdx] > pq_keys[parentIdx])
{
int tmp = pq_keys[childIdx];
pq_keys[childIdx] = pq_keys[parentIdx];
pq_keys[parentIdx] = tmp;
/*Make parent index the child and shift towards left*/
childIdx = parentIdx;
}
else
{
break;
}
}
return;
}
void PriorityQueue::shiftRight(int low, int high)
{
int root = low;
while ((root*2)+1 <= high)
{
int leftChild = (root * 2) + 1;
int rightChild = leftChild + 1;
int swapIdx = root;
/*Check if root is less than left child*/
if (pq_keys[swapIdx] < pq_keys[leftChild])
{
swapIdx = leftChild;
}
/*If right child exists check if it is less than current root*/
if ((rightChild <= high) && (pq_keys[swapIdx] < pq_keys[rightChild]))
{
swapIdx = rightChild;
}
/*Make the biggest element of root, left and right child the root*/
if (swapIdx != root)
{
int tmp = pq_keys[root];
pq_keys[root] = pq_keys[swapIdx];
pq_keys[swapIdx] = tmp;
/*Keep shifting right and ensure that swapIdx satisfies
heap property aka left and right child of it is smaller than
itself*/
root = swapIdx;
}
else
{
break;
}
}
return;
}
void PriorityQueue::buildHeap()
{
/*Start with middle element. Middle element is chosen in
such a way that the last element of array is either its
left child or right child*/
int size = pq_keys.size();
int midIdx = (size -2)/2;
while (midIdx >= 0)
{
shiftRight(midIdx, size-1);
--midIdx;
}
return;
}
int main()
{
//example usage
PriorityQueue asd;
asd.enqueue(2);
asd.enqueue(3);
asd.enqueue(4);
asd.enqueue(7);
asd.enqueue(5);
asd.print();
cout<< asd.dequeue() << endl;
asd.print();
return 0;
}
答案 0 :(得分:2)
更容易:
std::prioroty_queue<int, std::vector<int>, std::greater<int>> my_queue;
如果这是练习的一部分,那么我建议遵循标准库的设计原则:将问题分开:
std::make_heap
等。)你的班级应该给你一些余地来独立改变这些。有了这个,你可以轻而易举地改变&#34;少于&#34;订购&#34;大于&#34;之一。
答案 1 :(得分:2)
通常在这些问题中,即基于元素比较的算法,您可以重新定义(a < b)
的含义。 (这就是标准库中的工作方式。您可以定义自己的比较器。)
所以,如果你改变它的含义则相反。你将颠倒订购。
您需要识别元素的每个比较,并切换它。所以对于像这样的每一段代码
/*if child is bigger than parent we need to swap*/
if (pq_keys[childIdx] > pq_keys[parentIdx])
颠倒它的意思/逻辑。
简单的否定应该可以解决问题:
/*if child is NOT bigger than parent we need to swap*/
if !(pq_keys[childIdx] > pq_keys[parentIdx])
您甚至不需要了解算法。只是反映了较小元素的含义。
编辑:
附加说明。你实际上可以将它重构为某种bool compare(T a, T b)
。并使用此功能进行比较。因此,无论何时您想要更改行为,您只需要更改一个位置,它就会保持一致。但这主要是为了避免工作来查找每一个这样的事件,以及愚蠢的错误以及何时错过。