1、自定义实现:
使用时为防止出现不能运行的情况(最好.h和.cpp文件都导入,似乎使用模板类时有时会出现只导入.h文件会一直报错的情况)
(1)小根堆实现:
①MinHeap.h
#include <iostream>
/**
* 小根堆
*/
template <class T>
class MinHeap{
public:
MinHeap();//构造函数
~MinHeap(){delete []heap;};//析构函数
bool isEmpty(){ return (heapSize==0);};//当堆为空时返回true
int size(){ return heapSize;};//返回堆的大小
T top();//返回堆的根节点
T pop();//返回并删除堆的根节点
void push(const T& element);//向堆中插入元素,当堆满时扩展堆的容量
private:
int capacity;//堆容量
int heapSize;//堆的元素数量
T* heap;//保存堆数据的数组
};
②MinHeap.cpp
#include "MinHeap.h"
/**
* 小根堆的具体实现
*/
template<class T>
MinHeap<T>::MinHeap() {//构造函数
//this->capacity=capacity;
capacity=16;
heapSize=0;
heap=new T[capacity+1];
}
template<class T>
T MinHeap<T>::top(){//返回堆的根节点
return heap[1];
}
template<class T>
T MinHeap<T>::pop(){//返回并删除堆的根节点
T result=heap[1];//保存需要返回的根节点
int currentNode=1;
int child=2;
//获取到堆的最后一个元素
T lastElement=heap[heapSize--];
while(child<=heapSize){
//获取较小的子节点位置
if(child<heapSize&&heap[child]>heap[child+1])
child++;
if(heap[child]>lastElement)//找到合适的插入位置了
break;
heap[currentNode]=heap[child];
currentNode=child;
child*=2;
}
heap[currentNode]=lastElement;
return result;
}
template<class T>
void MinHeap<T>::push(const T& element){//向堆中插入元素,当堆满时扩展堆的容量
if(heapSize==capacity){//必要时扩展数组长度
capacity*=2;
T* temp=new T[capacity];
for (int i = 1; i <=heapSize; ++i) {
temp[i]=heap[i];
}
delete []heap;
heap=temp;
}
//为元素element寻找插入位置
//currentNode位置为新增叶节点位置
int currentNode=++heapSize;
while (currentNode!=1&&heap[currentNode/2]>element){
heap[currentNode]=heap[currentNode/2];//把元素向下移动
currentNode/=2;
}
//将元素插入
heap[currentNode]=element;
}
(2)大根堆实现:
①MaxHeap.h
#include <iostream>
/**
* 大根堆
*/
template <class T>
class MaxHeap{
public:
MaxHeap(int capacity);//构造函数
~MaxHeap(){delete []heap;};//析构函数
bool isEmpty(){ return (heapSize==0);};//当堆为空时返回true
int size(){ return heapSize;};//返回堆的大小
T top();//返回堆的根节点
T pop();//返回并删除堆的根节点
void push(const T &element);//向堆中插入元素,当堆满时扩展堆的容量
private:
int capacity;//堆容量
int heapSize;//堆的元素数量
T* heap;//保存堆数据的数组
};
②MinHeap.cpp
/**
* 大根堆的具体实现
*/
#include "MaxHeap.h"
template<class T>
MaxHeap<T>::MaxHeap(int capacity) {//构造函数
this->capacity=capacity;
heapSize=0;
heap=new T[capacity+1];
}
template<class T>
T MaxHeap<T>::top(){//返回堆的根节点
return heap[1];
}
template<class T>
T MaxHeap<T>::pop(){//返回并删除堆的根节点
T result=heap[1];//保存需要返回的根节点
int currentNode=1;
int child=2;
//获取到堆的最后一个元素
T lastElement=heap[heapSize--];
while(child<=heapSize){
//获取较大的子节点位置
if(child<heapSize&&heap[child]<heap[child+1])
child++;
if(heap[child]<lastElement)//找到合适的插入位置了
break;
heap[currentNode]=heap[child];
currentNode=child;
child*=2;
}
heap[currentNode]=lastElement;
return result;
}
template<class T>
void MaxHeap<T>::push(const T &element){//向堆中插入元素,当堆满时扩展堆的容量
if(heapSize==capacity){//必要时扩展数组长度
capacity*=2;
T* temp=new T[capacity];
for (int i = 1; i <=heapSize; ++i) {
temp[i]=heap[i];
}
delete []heap;
heap=temp;
}
//为元素element寻找插入位置
//currentNode位置为新增叶节点位置
int currentNode=++heapSize;
while (currentNode!=1&&heap[currentNode/2]<element){
heap[currentNode]=heap[currentNode/2];//把元素向下移动
currentNode/=2;
}
//将元素插入
heap[currentNode]=element;
}
2、c++ STL库中的堆:
①大根堆:
#include< queue >
//创建一个大根堆
priority_queue<T> maxHeap;
②小根堆:
#include< queue >
//创建一个小根堆
priority_queue<T,vector<T>,greater<T> > minHeap;