目录
一,二项堆
二项堆是一些高度互不相同的二项树组成的森林,其中每个二项树的根节点都是这棵树的最大值或最小值。
二,二项堆的初始化
用Sum记录森林中节点总数,N表示最多有N棵树,那么森林中最多能存储2^N-1个节点
int Sum;//节点总数
const int N=30;//最多树数
struct TreeNode {
int val;
TreeNode* child[N-1];
};
TreeNode* tree[N];
void init()
{
Sum=0;
for(int i=0;i<N;i++)tree[i]=NULL;
}
直接用数组把所有树的根存起来,根据Sum的值就可以知道哪些树是森林中有的,哪些没有。
三,二项堆查找堆顶
以大顶堆为例,下同。
如果需要小顶堆,只需要修改cmp函数。
template<typename T>
bool cmp(T a, T b)
{
return a < b; //大顶堆
}
int topId()
{
int ans=0;
bool flag=true;
for(int i=0;i<N;i++) {
if (((Sum >> i) & 1) == 0)continue;
if(flag || cmp(tree[ans]->val,tree[i]->val))ans=i,flag=false;
}
return ans;
}
int top()
{
return tree[topId()]->val;
}
四,二项堆的插入
生成一个单节点的二项树,并依次检查是否有同高度的树,如果有就合并为更高1层的树,并继续检查。
//把a和b合并
TreeNode* merge(TreeNode* a,TreeNode* b,int d)
{
if(d<0 || d>=N)return NULL;
if(cmp(a->val,b->val)){
b->child[d]=a;
return b;
}
a->child[d]=b;
return a;
}
//把p合入到a里面
TreeNode* merge(TreeNode* a[],TreeNode* p,int s,int d)
{
for(int i=d;i<N;i++){
if(((s>>i)&1)==0){
tree[i]=p;
break;
}
p = merge(tree[i],p,i);
tree[i]=NULL;
}
}
void push(int x)
{
if(Sum>=(1<<N))return; //满载
TreeNode*p=new TreeNode[1];
p->val=x;
for(int i=0;i<N;i++)p->child[i]=NULL;
merge(tree,p,Sum,0);
Sum++;
}
这个操作过程可以保证,以任意节点为根的树都是堆,即对于最大堆,从根到叶子节点的路径的所有节点上的值是递减的。
五,二项堆的合并
合并2个二项堆,可以从小到大,也可以从大到小,
其实就相当于2个二进制数的加法,可以从低位开始,也可以从高位开始。
//把b合入到a里面,b不复存在
void merge(TreeNode* a[],TreeNode* b[],int &sa,int sb)
{
for(int i=0;i<N;i++) {
if (((sb >> i) & 1) == 0)continue;
merge(a,b[i],sa);
sa+=(1<<i),sb-=(1<<i);
}
}
六,二项堆的删除
找到最大值所在的树,直接把根节点删掉,就得到一个二项堆,然后把这个二项堆和原堆合并即可。
void pop()
{
int id=topId();
TreeNode* b[N];
for(int i=0;i<N;i++)b[i]=tree[id]->child[i];
Sum-=(1<<id);
tree[id]=NULL;
merge(tree,b,Sum);
}
七,二项堆完整代码
#include<iostream>
#include <vector>
using namespace std;
int Sum;//节点总数
const int N=30;//最多树数
struct TreeNode {
int val;
TreeNode* child[N-1];
};
TreeNode* tree[N];
void init()
{
Sum=0;
for(int i=0;i<N;i++)tree[i]=NULL;
}
template<typename T>
bool cmp(T a, T b)
{
return a < b; //大顶堆
}
int topId()
{
int ans=0;
bool flag=true;
for(int i=0;i<N;i++) {
if (((Sum >> i) & 1) == 0)continue;
if(flag || cmp(tree[ans]->val,tree[i]->val))ans=i,flag=false;
}
return ans;
}
int top()
{
return tree[topId()]->val;
}
//把a和b合并
TreeNode* merge(TreeNode* a,TreeNode* b,int d)
{
if(d<0 || d>=N)return NULL;
if(cmp(a->val,b->val)){
b->child[d]=a;
return b;
}
a->child[d]=b;
return a;
}
//把p合入到a里面
TreeNode* merge(TreeNode* a[],TreeNode* p,int s,int d)
{
for(int i=d;i<N;i++){
if(((s>>i)&1)==0){
tree[i]=p;
break;
}
p = merge(tree[i],p,i);
tree[i]=NULL;
}
}
void push(int x)
{
if(Sum>=(1<<N))return; //满载
TreeNode*p=new TreeNode[1];
p->val=x;
for(int i=0;i<N;i++)p->child[i]=NULL;
merge(tree,p,Sum,0);
Sum++;
}
//把b合入到a里面,b不复存在
void merge(TreeNode* a[],TreeNode* b[],int &sa)
{
for(int i=0;i<N;i++) {
if (b[i]) {
merge(a,b[i],sa,i);
sa+=(1<<i);
}
}
}
void pop()
{
int id=topId();
TreeNode* b[N];
for(int i=0;i<N;i++)b[i]=tree[id]->child[i];
Sum-=(1<<id);
tree[id]=NULL;
merge(tree,b,Sum);
}
int main()
{
init();
push(4);
push(2);
push(6);
push(3);
push(4);
push(5);
cout<<top()<<" ";
pop();
cout<<top()<<" ";
pop();
cout<<top()<<" ";
pop();
cout<<top()<<" ";
pop();
cout<<top()<<" ";
pop();
cout<<top()<<" ";
pop();
cout<<endl<<Sum;
return 0;
}
运行结果:
6 5 4 4 3 2
0