最大堆及基于最大堆的最大优先队列

堆也是一类特殊的数据结构

最大堆具有的性质:父节点的值大于子节点的值

在最大堆的类中我们定义了主要函数有:维护最大堆,建立最大堆,利用最大堆进行排序

以下是最大堆类的定义:

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值