#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <limits>
using namespace std;
class PriorityQueue
{
public:
truePriorityQueue() { };
true~PriorityQueue() { };
trueint getParent(int i) { return (i - 1) / 2; }
trueint getLeftChild(int i) { return i * 2 + 1; }
trueint getRightChild(int i) { return i * 2 + 2; }
trueinline void addElem(string, int);
truevoid swapTwoElem(int, int);
truevoid iniHeapSize() { heapSize = elemQueue.size() - 1; }
truevoid display();
truevoid maxHeapify(int);
truevoid buildMaxHeap();
truevoid heapSort();
true
trueconst string getElemVal(int elemIndex) { return elemQueue[elemIndex]; }
trueconst int getElemPriVal(int elemIndex) { return priQueue[elemIndex]; }
trueconst string maximum() { return getElemVal(0); }
truevoid extractMax();
truevoid increaseKey(int, int);
truevoid heapInsert(string, int);
truevoid heapDelete(int);
truevoid heapDelete(string);
private:
truevector<string> elemQueue;
truevector<int> priQueue;
truevector<int>::size_type heapSize;
};
void PriorityQueue::addElem(string elem, int key)
{
trueelemQueue.push_back(elem);
truepriQueue.push_back(key);
}
void PriorityQueue::swapTwoElem(int i, int j)
{
trueint temp1 = priQueue[i];
truepriQueue[i] = priQueue[j];
truepriQueue[j] = temp1;
truestring temp2 = elemQueue[i];
trueelemQueue[i] = elemQueue[j];
trueelemQueue[j] = temp2;
}
void PriorityQueue::display()
{
truecout << "----------------------------------" << endl;
truefor (unsigned int i = 0; i <= heapSize; i++)
truetruecout << getElemVal(i) << "\t";
truecout << endl;
truefor (unsigned int i = 0; i <= heapSize; i++)
truetruecout << getElemPriVal(i) << "\t";
truecout << endl;
truecout << "----------------------------------" << endl;
}
true保持堆的最大化
true运行时间:O(lgn)
true(练习6.2-5)使用循环取代了递归
*/
void PriorityQueue::maxHeapify(int i)
{
trueint largest = i;
trueint isHeapify = 0;
truewhile (isHeapify == 0) {
truetrueunsigned int l = getLeftChild(i);
truetrueunsigned int r = getRightChild(i);
truetrueif (l <= heapSize && priQueue[l] > priQueue[largest])
truetruetruelargest = l;
truetrueif (r <= heapSize && priQueue[r] > priQueue[largest])
truetruetruelargest = r;
truetrueif (largest != i) {
truetruetrueswapTwoElem(i, largest);
truetruetruei = largest;
truetrue} else
truetruetrueisHeapify = 1;
true}
}
true在无序的输入数组基础上构造出最大堆
true线性时间运行
*/
void PriorityQueue::buildMaxHeap()
{
truefor (int i = heapSize / 2; i >= 0; i--)
truetruemaxHeapify(i);
}
true对一个数组进行原地排序
true运行时间:O(nlgn)
*/
void PriorityQueue::heapSort()
{
truebuildMaxHeap();
truefor (int i = heapSize; i >= 1; i--) {
truetrueswapTwoElem(0, i);
truetrue--heapSize;
truetruemaxHeapify(0);
true}
}
void PriorityQueue::extractMax()
{
trueif (heapSize < 0)
truetruethrow "heap underflow";
truecout << "被移除的元素是: "<< getElemVal(0) << " 优先级: " << getElemPriVal(0) << endl;
truepriQueue[0] = priQueue[heapSize];
trueelemQueue[0] = elemQueue[heapSize];
true--heapSize;
truemaxHeapify(0);
}
void PriorityQueue::increaseKey(int elemIndex, int newKey)
{
trueif (newKey < priQueue[elemIndex])
truetruethrow "new key is smaller than current key";
truepriQueue[elemIndex] = newKey;
truewhile (elemIndex > 0 && priQueue[getParent(elemIndex)] < priQueue[elemIndex]) {
truetrueswapTwoElem(elemIndex, getParent(elemIndex));
truetrueelemIndex = getParent(elemIndex);
true}
}
void PriorityQueue::heapInsert(string elem, int key)
{
true++heapSize;
truepriQueue[heapSize] = INT_MIN;
trueelemQueue[heapSize] = elem;
trueincreaseKey(heapSize, key);
}
void PriorityQueue::heapDelete(int elemIndex)
{
trueelemQueue[elemIndex] = elemQueue[heapSize];
truepriQueue[elemIndex] = priQueue[heapSize];
true--heapSize;
truemaxHeapify(elemIndex);
}
void PriorityQueue::heapDelete(string elem)
{
trueunsigned int elemIndex;
truefor (elemIndex = 0; elemIndex <= heapSize; elemIndex++)
truetrueif (elemQueue[elemIndex] == elem) {
truetruetruebreak;
truetrue}
trueelemQueue[elemIndex] = elemQueue[heapSize];
truepriQueue[elemIndex] = priQueue[heapSize];
true--heapSize;
truemaxHeapify(elemIndex);
}
int main()
{
truePriorityQueue queue;
truestring elem;
trueint key;
truewhile (cin >> elem >> key)
truetruequeue.addElem(elem, key);
true
truequeue.iniHeapSize();
truequeue.heapSort();
truequeue.iniHeapSize();
truecout << "堆排序:" << endl;
truequeue.display();
true
truecout << "构建最大优先级队列:" << endl;
truequeue.buildMaxHeap();
truequeue.display();
truecout << "返回最大优先级元素/移除元素/提升某元素优先级/插入新元素:" << endl;
truecout <<"堆中优先级最高的元素是: " << queue.maximum() << "优先级: " << queue.getElemPriVal(0) << endl;
truequeue.extractMax();
truequeue.increaseKey(2, 99);
truequeue.heapInsert("shit", 999);
truequeue.heapDelete("music");
truequeue.display();
truesystem("pause");
truereturn 0;
}