题目：优先级队列（封装堆）

template<class T>
struct GreaterCompare
{
bool operator()(const T& leftChild, const T& rightChild)
{
return leftChild > rightChild;
}
};

template<class T>
struct LessCompare
{
bool operator()(const T& leftChild, const T& rightChild)
{
return leftChild < rightChild;
}
};

#pragma once
#include<iostream>
using namespace std;
#include<vector>
//仿函数（函数对象）
template<class T>
struct GreaterCompare
{
bool operator()(const T& leftChild, const T& rightChild)
{
return leftChild > rightChild;
}
};

template<class T>
struct LessCompare
{
bool operator()(const T& leftChild, const T& rightChild)
{
return leftChild < rightChild;
}
};

//模板类型的模板形参
template<class T,template<class> class Compare = GreaterCompare> //缺省参数
class Heap
{
public:
Heap()
{}

Heap(const T* arr,int len)
{
if (arr == NULL || len <= 0)
return;

for (int i = 0; i < len; ++i)
{
_heapArr.push_back(arr[i]);
}

//建堆：找第一个非叶子结点的父亲，然后向下调整
for (int i = len - 1; i >= 0; --i)
{
int root = (i - 1) / 2;
}
}

//末尾增加一个结点后，向上调整
void Push(const T& x)
{
_heapArr.push_back(x);
}

//删除一个结点时，先将堆顶数据与末尾数据交换，然后删除末尾数据，最后向下调整
void Pop()
{
swap(_heapArr[0], _heapArr[_heapArr.size() - 1]);
_heapArr.pop_back();
}

T& Size()
{
return _heapArr.size();
}

bool Empty()
{
return _heapArr.empty();
}

T& Top()
{
return _heapArr[0];
}

void Print()
{
for (int i = 0; i < (int)_heapArr.size(); ++i)
{
cout << _heapArr[i] << " ";
}
cout << endl;
}

~Heap()
{}

protected:
//向下调整
{
Compare<T> com;
int child = root * 2 + 1;
while (child < (int)_heapArr.size())
{
if (child + 1 < (int)_heapArr.size() && com(_heapArr[child + 1], _heapArr[child]))
{
++child;
}

if (com(_heapArr[child], _heapArr[root]))
{
swap(_heapArr[child], _heapArr[root]);
root = child;
child = root * 2 + 1;
}
else
{
break;
}
}
}

//向上调整
{
Compare<T> com;
int root = (child - 1) / 2;
while (child > 0)
{
if (com(_heapArr[child], _heapArr[root]))
{
swap(_heapArr[child], _heapArr[root]);
child = root;
root = (child - 1) / 2;
}
else
{
break;
}
}
}

private:
vector<T> _heapArr;
};

//测试用例
void TestHeap()
{
int arr[10] = { 10, 11, 13, 12, 16, 18, 15, 17, 14, 19 };
int len = sizeof(arr) / sizeof(arr[0]);
//大堆
Heap<int> hp1(arr, len);
hp1.Print();
//小堆
Heap<int,LessCompare> hp(arr, len);
hp.Print();

hp.Push(21);
hp.Print();

hp.Pop();
hp.Print();
}

//将堆封装成优先级队列
template <class T>
class PriorityQueue
{
public:
PriorityQueue(Heap<T>& heap)
:_heap(heap)
{}

~PriorityQueue()
{}

void QueuePush(const T& x)
{
_heap.Push();
}

void QueuePop()
{
_heap.Pop();
}

bool QueueEmpty()
{
return _heap.Empty();
}

int QueueSize
{
return _heap.Size();
}

T& QueueFront()
{
return _heap.Top();
}

private:
Heap<T> _heap;
};



优先级队列是一种什么样的数据结构

2016-09-07 20:52:28

优先级队列几个应用详解

2012-08-12 13:52:31

c语言实现优先级队列

2013年05月27日 3KB 下载

关于优先级队列的实现

2009-01-05 15:48:00

JDK中优先级队列PriorityQueue实现分析

2015-04-21 22:54:55

第十章·优先级队列

2016-04-22 00:53:47

Java 多线程 优先级队列 PriorityBlockingQueue

2017-01-05 17:38:12

【STL学习】优先级队列Priority Queue详解与C++编程实现

2013-01-30 16:43:11

优先级队列与堆的关系

2017-05-11 15:45:50

python多线程-queue队列类型优先级队列，FIFO,LIFO

2017-05-30 20:00:37