优先队列(priority queue)
是至少允许下列两种操作的数据结构:插入(insert);删除最小项(deleteMin),即找出、返回和删除最小项。
优先队列的应用:
1)操作系统的任务调度(优先调度任务重要或任务短小的任务);
2)使用反复求最小元方式的贪心算法(greedy algorithm);
3)在离散模拟中的应用;
二叉堆(堆):
堆是一棵完全填满的二叉树,可能的例外是底层,底层的元素从左到右填入。因为二叉堆的有序排列,因此可以使用数组而不是链表来表示二叉堆(如下图所示)【注:此处从下标1开始,则对任意位置上的元素,左儿子的位置为,右儿子的位置为,父亲的位置在】。
最大堆:
父结点的键值总是大于或等于任何一个子节点的键值
最小堆:
父结点的键值总是小于或等于任何一个子节点的键值
堆的插入:从数组尾部插入,通过上滤(percolate up)完成保证堆的性质;
堆的删除:删除最小值,从顶部开始,通过下滤(percolate down)保证堆的性质;
实例:建立优先队列ADT,完成队列的建立、插入、删除、打印等功能。
1、heap.h
//heap.h
#ifndef HEAP_H_
#define HEAP_H_
#include<iostream>
#include<vector>
#include<algorithm>
using std::cout;
using std::endl;
using std::vector;
template<class T>
class Heap{
public:
//Heap constructor
explicit Heap(int cap = 10):array(cap + 1),currentSize(0){
};
//another Heap constructor
explicit Heap(const vector<T> &arr): array(arr.size() + 10), currentSize(arr.size()){
for(int i = 0; i < arr.size(); i++){
array[i + 1] = arr[i];
}
buildHeap();
};
//judge whether the array is empty
bool isEmpty() const{
return currentSize == 0;
};
//make the array empty
void makeEmpty(){
if(isEmpty()){
return;
}
for(int i = array.size(); i > 0; i--){
array.pop_back();
}
currentSize = 0;
};
//find the minimum item
const T & findMin() const{
return array[1];
};
//insert item into array
void insert(const T &item){
if(currentSize == array.size() - 1){
array.resize(array.size() * 2);
}
int hole = ++currentSize;
for(; hole > 1 && item < array[hole / 2]; hole /= 2){
array[hole] = array[hole / 2];
}
array[hole] = item;
};
//delete minimum item in array
void deleteMin(){
if(isEmpty()){
cout << " the array is empty, failed to deleteMin" << endl;
return;
}
array[1] = array[currentSize--];
percolateDown(1);
};
//deleteMin overload
void deleteMin(T &item){
if(isEmpty()){
cout << " the array is empty, failed to deleteMin" << endl;
return;
}
item = array[1];
array[1] = array[currentSize--];
percolateDown(1);
};
//print the array
void print(){
for(int i = 0; i < currentSize; i++){
cout << array[i] << " ";
}
cout << endl;
};
private:
//build heap
void buildHeap(){
for(int i = currentSize / 2; i >0 ; i--){
percolateDown(i);
}
};
//percolate down
void percolateDown(int hole){
int child;
T temp = array[hole];
for(; hole * 2 < currentSize; hole = child){
child = hole * 2;
if(child != currentSize && array[child + 1] < array[child]){
child++;
}
if(array[child] < temp){
array[hole] = array[child];
}else{
break;
}
}
array[hole] = temp;
};
private:
int currentSize;
vector<T> array;
};
#endif
2、main.cpp
//main.cpp
#include<iostream>
#include"heap.h"
using namespace std;
int main(){
int arr[] = {13, 21, 16, 24, 31, 19, 68, 65, 26, 32, 5};
vector<int> v;
int n = sizeof(arr)/ sizeof(arr[0]);
for(int i = 0; i < n; i++){
v.push_back(arr[i]);
}
Heap<int> heap;
cout << "********** insert **********" << endl;
for(int i = 0; i < n; i++){
heap.insert(v[i]);
}
cout << "********** print **********" << endl;
heap.print();
cout << "********** findMin **********" << endl;
cout << "the minimum item is " << heap.findMin() << endl;
cout << "********** deleteMin **********" << endl;
heap.deleteMin();
heap.print();
cout << "********** insert(14) **********" << endl;
heap.insert(14);
heap.print();
cout << "done ." << endl;
return 0;
}
peractice makes perfect !