队列具有先进先出的特性,而优先级队列是按照优先级来进行处理。即:优先级队列的出队列操作不是直接将队头元素出队列,而是把队列中优先级最高的元素出队列。
要选出优先级最高的元素则可以借助堆来完成。
堆
堆分为:最小堆和最大堆。
最小堆:
将数据按照二叉树的方式进行排列,其中每个节点的根节点的值小于其左子树和右子树,与左、右孩子的大小无关,称为最小堆。
最大堆:
将数据按照二叉树的方式进行排列,其中每个节点的根节点的值大于其左子树和右子树,与左、右孩子的大小无关,称为最大堆。
堆:
#include <iostream>
#include <windows.h>
#include <vector>
#include <assert.h>
using namespace std;
template<class T>
struct Less
{
bool operator()(const T& left, const T& right)
{
return left->_weight < right->_weight;
}
};
template<class T>
struct Greater
{
bool operator()(const T& left, const T& right)
{