数据结构与算法分析-C++描述 第6章 优先队列ADT(二叉堆)

优先队列(priority queue)

     是至少允许下列两种操作的数据结构:插入(insert);删除最小项(deleteMin),即找出、返回和删除最小项。

优先队列的应用

    1)操作系统的任务调度(优先调度任务重要或任务短小的任务);

    2)使用反复求最小元方式的贪心算法(greedy algorithm);

    3)在离散模拟中的应用;

二叉堆(堆)

     堆是一棵完全填满的二叉树,可能的例外是底层,底层的元素从左到右填入。因为二叉堆的有序排列,因此可以使用数组而不是链表来表示二叉堆(如下图所示)【注:此处从下标1开始,则对任意位置i上的元素,左儿子的位置为2i,右儿子的位置为2i+ 1,父亲的位置在[i / 2]】。

最大堆:

    父结点的键值总是大于或等于任何一个子节点的键值

最小堆:

    父结点的键值总是小于或等于任何一个子节点的键值

堆的插入:从数组尾部插入,通过上滤(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 !

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值