1、堆用陣列來儲存。
2、堆按完全二元樹的結構描述,因此「下標 i 的父節點與孩子節點」這層關係,就成了取得堆中資料的入口。若起始下標不同,孩子節點的下標算法也會略有差異;本文的實作從 0 開始,左孩子為 2*i+1、右孩子為 2*i+2。
3、最大堆可用於排序,複雜度為 O(Nlog(N)),同時也是實作優先權佇列的基礎。
4、下面的程式碼詳細呈現了最大堆的幾個關鍵操作。
#ifndef MAXHEAP_H
#define MAXHEAP_H
#include <memory.h>
#include <iostream>
using namespace std;
/**
最大堆
*/
class MaxHeap
{
public:
int heapSize;
int * heap;
public:
MaxHeap();
MaxHeap(int heapSize);
virtual ~MaxHeap();
void output_heap();
void init_heap(int a[],int n);
int left(int i);
int right(int i);
void max_heapify(int i);
void max_heapify2(int i);
void build_max_heap();
void heap_sort();
};
#endif // MAXHEAP_H
#include "MaxHeap.h"
MaxHeap::MaxHeap(){
}
MaxHeap::~MaxHeap(){
delete []heap;
}
MaxHeap::MaxHeap(int heapSize):heapSize(heapSize){
heap = new int [heapSize] ;
memset(heap,0,sizeof(int)*heapSize);
}
//左孩子的index
int MaxHeap::left(int i){//下標從0開始的原因
return 2*i+1;
}
//右孩子的index
int MaxHeap::right(int i){
return 2*i +2;
}
/**
輸出堆的內容
**/
void MaxHeap::output_heap(){
for(int i=0;i<heapSize;++i){
cout<<heap[i]<<" ";
}
cout<<endl;
}
/**
利用數組初始化堆內容
**/
void MaxHeap::init_heap(int a[], int n){
if(n>heapSize)return ;
else{
heapSize=n;
for(int i=0;i<heapSize;++i){
heap[i]=a[i];
}
}
}
/**
調整堆內數組使得其滿足堆的性質
調整從i節點開始,這個操作可以保證i節點及其子孫節點都滿足最大堆的特點
*/
void MaxHeap::max_heapify(int i){
int l= left(i);
int r= right(i);
int max_index=0;
//最大孩子的下標
if(l<heapSize&&heap[l]>heap[i]){
max_index=l;
}else {
max_index=i;
}
if(r<heapSize&&heap[r]>heap[max_index]){
max_index=r;
}
if(max_index!=i){
swap(heap[i],heap[max_index]);
max_heapify(max_index);//防止造成子孫節點中出現不滿足堆的性質的情況發生
}
}
//堆調整的非遞歸代碼實現
void MaxHeap::max_heapify2(int i){
while(i<heapSize/2){
int l = left (i);
int r = right (i);
int max_index=0;
if(r<heapSize){
max_index = heap[l] > heap[r] ? l : r;
max_index = heap[max_index] > heap[i] ? max_index : i;
}else{
max_index = heap[l] > heap[i] ? l:i;
}
if(max_index!=i){
swap(heap[i],heap[max_index]);
i = max_index;
}
}
}
/**
堆的建立操作
*/
void MaxHeap::build_max_heap(){
for(int i= heapSize/2 +1;i>=0;--i){//從第一個非葉子節點開始調整為最大堆特征
max_heapify(i);
}
}
/**
堆排序,將最後一個元素同堆頂元素交換
每次都能保證取得的是當前最大的元素
max_heapfy(0),調整第一個元素
**/
void MaxHeap::heap_sort(){
build_max_heap();
int t = heapSize;
for(int i=heapSize-1;i>0;--i){
swap(heap[i],heap[0]);
heapSize--;
max_heapify(0);//取出之後對造成的不滿足最大堆進行調整
}
heapSize=t;
}
整份實作的骨架就三件事:build_max_heap() 從最後一個非葉子節點回頭逐一調整,把任意陣列變成最大堆;max_heapify() 負責把某個節點往下沉到正確位置(遞迴版與迴圈版 max_heapify2() 效果相同);heap_sort() 則反覆把堆頂與尾端交換、縮小堆並重新調整,於是排序由大到小自然完成。