堆也是一类特殊的数据结构
最大堆具有的性质:父节点的值大于子节点的值
在最大堆的类中我们定义了主要函数有:维护最大堆,建立最大堆,利用最大堆进行排序
以下是最大堆类的定义:
//name:myMaxHeap.h
//最大堆,利用vector存储其中的元素
#ifndef MY_MAX_STACK1_H
#define MY_MAX_STACK1_H
#include<vector>
#include<algorithm>
using std::vector;
using namespace std;
template<typename T>
class myMaxHeap {
//将最大堆作为最大优先队列的友元
template<typename T>
friend class myMaxPriQue;
vector<T> maxHeap;
public:
myMaxHeap() { }
myMaxHeap(vector<T> vec) {
maxHeap = vec;
build_max_heap(maxHeap);
}
//建立最大堆
void build_max_heap(vector<T> &vec) {
int i;
for (i = getParent(vec.size()) ; i >= 0; i--)
max_heapfy(vec, i);
}
//堆排序
void heapSort(vector<T> &vec, vector<T> &sortedVec) {
build_max_heap(vec);
for (int i = vec.size() - 1; i > 0; i--) {
swap(vec[i], vec[0]);
sortedVec.push_back(vec[i]);
vec.pop_back();
max_heapfy(vec, 0);
}
sortedVec.push_back(vec[0]);
}
private:
int getParent(int i) {
return (i - 1) / 2;
}
int getLChild(int i) {
return 2 * i + 1;
}
int getRChild(int i) {
return 2 * i + 2;
}
//维护堆的性质
void max_heapfy(vector<T> &maxHeap, int i) {
int lChild, rChild;
int largest=i;
lChild = getLChild(i);
rChild = getRChild(i);
if (lChild<maxHeap.size() && maxHeap[lChild]>maxHeap[i])
largest = lChild;
if (rChild<maxHeap.size() && maxHeap[rChild]>maxHeap[largest])
largest = rChild;
if (largest != i) {
swap(maxHeap[i], maxHeap[largest]);
max_heapfy(maxHeap, largest);
}
}
};
#endif
以下是基于最大堆定义的最大优先队列
//name:myMaxPriQue.h
//最大优先队列
#include"myMaxHeap.h"
#define MINNUMBER -10000
template<typename T>
class myMaxPriQue {
private:
myMaxHeap<T> maxPriQue;//最大堆
public:
//构造函数
myMaxPriQue(vector<T> vec){
maxPriQue = myMaxHeap<T>(vec);
}
//返回堆中最大的元素
T heap_maximum() {
return maxPriQue.maxHeap[0];
}
//删除并返回最大堆中的元素
T heap_extract_max() {
if (maxPriQue.maxHeap.size() < 1)
cout << "heap underflow" << endl;
int maxNum = maxPriQue.maxHeap[0];
maxPriQue.maxHeap.erase(maxPriQue.maxHeap.begin());
maxPriQue.max_heapfy(maxPriQue.maxHeap, 0);
return maxNum;
}
//将i位置的关键字增大至value的值
void heap_increase_key(int i, T value) {
if (value < maxPriQue.maxHeap[i])
cout << "new value is smaller than current key" << endl;
maxPriQue.maxHeap[i] = value;
while (i > 0 && maxPriQue.maxHeap[maxPriQue.getParent(i)] < maxPriQue.maxHeap[i]) {
swap(maxPriQue.maxHeap[i], maxPriQue.maxHeap[maxPriQue.getParent(i)]);
i = maxPriQue.getParent(i);
}
}
//将关键字的值value插入到队列中
void max_heap_insert(T value) {
maxPriQue.maxHeap.push_back((T)MINNUMBER);
int heap_size = maxPriQue.maxHeap.size() - 1;
heap_increase_key(heap_size, value);
}
int sizeOf() {
return maxPriQue.maxHeap.size();
}
};
//name:main.cpp
#include<iostream>
#include"myMaxHeap.h"
#include"myMaxPriQue.h"
using namespace std;
int main(void) {
/*(
//对最大堆的测试
vector<int> vec = { 6,9,12,4,35,1,5,24 };
vector<int> sortedVec;
myMaxHeap<int> maxHeap = myMaxHeap<int>();
myMaxHeap<int> maxHeap1 = myMaxHeap<int>(vec);
myMaxPriQue<int> maxPriQue1=myMaxPriQue<int>(vec);
maxHeap.heapSort(vec, sortedVec);
for (int i = 0; i < sortedVec.size(); i++) {
cout << sortedVec[i] << " ";
}
cout << endl;
*/
//对最大优先队列的测试
vector<int> vec = { 6,9,12,4,35,1,5,24 };
myMaxPriQue<int> maxQueue = myMaxPriQue<int>(vec);
maxQueue.max_heap_insert(20);
vector<int> maxVec;
int scount = maxQueue.sizeOf();
for (int i = 0; i < scount; i++) {
int temp = maxQueue.heap_extract_max();
maxVec.push_back(temp);
}
return 0;
}