大根堆
大根堆是根节点比其儿子结点值要大的二叉堆,故优先队列的队首数值是最大的。具体实现见代码:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 1000;
class Priority_queue //大根堆,优先队列
{
public:
int heap[maxn], tot;
Priority_queue();
~Priority_queue();
void push(int num);
void pop();
int front();
bool empty();
private:
};
Priority_queue::Priority_queue() {
tot = 0;
}
Priority_queue::~Priority_queue()
{
}
void Priority_queue::push(int num) {
heap[tot++] = num;
int k = tot - 1, t, tmp;
while (k > 0) {
t = (k - 1) / 2;
if (heap[t] < heap[k]) {
tmp = heap[t];
heap[t] = heap[k