1.堆的性质
(1)完全二叉树的层次序列,可以用数组表示
(2)堆中储存的数是局部有序的,堆不唯一
——PS:节点的值与其孩子的值之间存在限制,任何一个节点与其兄弟之间都没有直接的限制。
(3)从逻辑角度看,堆实际上是一种树形结构
(4)堆是一种经过排序的完全二叉树,其中任一非终端节点的数据值均不大于(或不小于)其左子节点和右子节点的值。
2.堆和优先队列的关系
堆可以用于实现优先队列
3.堆的实现
我这里写的是最小堆的一系列操作,最大堆类似。
其中有两个重要的函数siftdown和siftup。
先讲一下siftdown的过程
下图中的72节点不满足最小堆的性质,需要对其siftdown,下面展示过程。
先将72保存在temp中,找出23、5其中较小的那个进行覆盖。
然后在16,和71中找出较小的那个,进行覆盖。
此时发现,已经是叶子节点。将保存的temp赋值给叶子节点。
现在就完成了siftdown的过程。
贴代码
void MinHeap::siftDown(int position) //对最小堆用筛选法调整
{
int i=position; //标识父节点
int j=2*i+1; //标识值较小的子节点
int temp=heapArry[i]; //保存父节点
while(j<CurrentSize) //CurrentSize为当前堆中的节点个数
{
if( j+1<CurrentSize && heapArry[j+1]<heapArry[j]) //j指向数值较小的子节点
j++;
if(temp>heapArry[j])
{
heapArry[i]=heapArry[j];
i=j;
j=2*j+1;
}
else
break;
}
heapArry[i] = temp;
}
当前堆中的节点个数为CurrentSize。
建堆的过程就是从CurrentSize/2-1不断siftdown的过程。
void MinHeap::BuildHeap()
{
for(int i=CurrentSize/2-1;i>=0;i--)
siftDown(i);
}
现在说一下siftup,siftup相对siftdown简单。
现在向堆中添加一个数3,此时不满足最小堆的性质,需要对3进行siftup。
上面就是siftup的过程。
代码
void MinHeap::siftUp(int position) //对最小堆用筛选法向上调整
{
int temppos=position;
int temp = heapArry[position];
while( temppos>0 && heapArry[parent(temppos)]> temp) //Parent()的返回值为找到当前节点的父节点下标
{
heapArry[temppos] = heapArry[parent(temppos)];
temppos = parent(temppos);
}
heapArry[temppos]=temp; //找到最终位置
}
添加节点:在尾部添加一个节点,然后siftup到合适的位置。
删除节点:把尾部的元素值赋值给被删除元素值,判断新覆盖的值是不是在合适位置,根据情况选择siftup和siftdown。
下面贴个完整代码
#include<iostream>
using namespace std;
class MinHeap //最小堆
{
int * heapArry; //存放堆数据的数组
int CurrentSize; //当前堆中元素数目
int MaxSize; //堆所能容纳的最大元素的数目
public:
MinHeap(int n); //构造函数
~MinHeap() //析构函数
{
delete [] heapArry;
}
int leftchild(int pos); //返回左孩子位置
int rightchild(int pos); //返回右孩子位置
int parent(int pos); //返回父节点位置
bool Remove(int pos,int & node); //删除给定下标的元素
bool Insert(int & newnode); //向堆中插入新元素
int & RemoveMin(); //从堆顶删除最小值
void siftUp(int position); //从position向上开始调整,使序列成为堆
void siftDown(int position); //筛选法函数,参数position表示开始处理的数组下标
void print(); //输出堆
void BuildHeap(); //建最小堆
};
void MinHeap::print() //输出元素
{
for(int i=0;i<CurrentSize;i++)
cout<<heapArry[i]<<" ";
cout<<endl;
}
int MinHeap::leftchild(int pos) //返回左孩子节点
{
return pos*2+1;
}
int MinHeap::rightchild(int pos) //返回右孩子节点
{
return pos*2+2;
}
int MinHeap::parent(int pos) //返回父节点
{
return (pos-1)/2;
}
MinHeap::MinHeap(int n) //构造函数
{
heapArry=new int[n];
heapArry[0]=72; heapArry[1]=73;
heapArry[2]=71; heapArry[3]=23;
heapArry[4]=94; heapArry[5]=16;
heapArry[6]=5; heapArry[7]=68;
MaxSize=n; //最大存储个数
CurrentSize=8; //当前个数
}
void MinHeap::siftDown(int position) //对最小堆用筛选法调整
{
int i=position; //标识父节点
int j=2*i+1; //标识值较小的子节点
int temp=heapArry[i]; //保存父节点
while(j<CurrentSize)
{
if( j+1<CurrentSize && heapArry[j+1]<heapArry[j]) //j指向数值较小的子节点
j++;
if(temp>heapArry[j])
{
heapArry[i]=heapArry[j];
i=j;
j=2*j+1;
}
else
break;
}
heapArry[i] = temp;
}
void MinHeap::siftUp(int position) //对最小堆用筛选法向上调整
{
int temppos=position;
int temp = heapArry[position];
while( temppos>0 && heapArry[parent(temppos)]> temp)
{
heapArry[temppos] = heapArry[parent(temppos)];
temppos = parent(temppos);
}
heapArry[temppos]=temp; //找到最终位置
}
void MinHeap::BuildHeap() //建堆
{
for(int i=CurrentSize/2-1;i>=0;i--)
siftDown(i);
}
bool MinHeap::Insert(int & newnode) //插入元素
{
if(CurrentSize == MaxSize) //堆空间已满,插入失败
return false;
heapArry[CurrentSize]=newnode;
siftUp(CurrentSize);
CurrentSize++;
return true;
}
bool MinHeap::Remove(int pos,int &node) //删除元素
{
if(pos<0 || pos>=CurrentSize) //删除节点下标非法
return false;
node = heapArry[pos];
heapArry[pos] = heapArry[--CurrentSize];
if( heapArry[pos] < heapArry[parent(pos)] )
siftUp(pos); //向上筛
else
siftDown(pos); //向下筛
return true;
}
int & MinHeap::RemoveMin() //删除最小值
{
int min_node = heapArry[0];
heapArry[0] = heapArry[--CurrentSize];
siftDown(0);
return min_node;
}
int main()
{
MinHeap heap(10);
heap.print();
heap.BuildHeap();
heap.print();
cout<<"请输入要插入的元素"<<endl;
int node_insert;
cin>>node_insert;
heap.Insert(node_insert);
heap.print();
cout<<"请输入要删除元素的下标"<<endl;
int node_delete;
int pos;
cin>>pos;
heap.Remove(pos,node_delete);
heap.print();
cout<<"删除堆中的最小元素"<<endl;
int min;
min=heap.RemoveMin();
cout<<min<<endl;
heap.print();
return 0;
}
验证建堆数据72、73、71、23、94、16、05、68
4.性能分析
建堆的过程是相当高效的,时间复杂度为O(log(n)).
插入节点、删除普通节点和删除最小元素的平均时间代价和最差时间代价都是O(log(n)).