gpt4 book ai didi

c++ - 实现最小函数

转载 作者:行者123 更新时间:2023-11-30 01:55:33 24 4
gpt4 key购买 nike

美好的一天,我找到了这个优先级队列实现,我正在尝试获取它的最小版本(而不是最大版本)。我不知道从哪里开始。我尝试混合函数的符号(天真的尝试)但它并没有让我走得太远。任何关于如何实现它的帮助和几句话的解释都是非常受欢迎的。来源如下:注意我已经留下了它的评论

#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;
}

最佳答案

通常在此类问题中,即基于元素比较的算法,您可以重新定义 (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) .并在使用比较的地方使用此功能。所以每当你想改变行为时,你只需要改变一个地方,它就会保持一致。但这主要是为了避免寻找每一个这样的事件、愚蠢的错误以及当你错过一个错误时。

关于c++ - 实现最小函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20628812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com