1)MaxHeap :最大堆;
操作集:
MaxHeap CreateHeap(int Maxsize); //创建最大堆,只是给堆创建一个存储空间。
bool IsFull(MaxHeap H); //判断最大堆是否已满;
bool Insert(MaxHeap H,,ElementType x); //将元素x插入到最大堆H中;
bool IsEmpty(MaxHeap H); //判断最大堆是否为空;
ElementType Delete(MaxHeap H); //删除并返回H中最大元素;
void downAdjust(MaxHeap H,int p); //下滤:将H中以H->data[p] 为根的子堆调整为最大堆;
void BuildHeap(MaxHeap H); //建堆;由于MaxHeap本身就是指针,所以不需要加引用。
void add_full_binary_tree(MaxHeap H,int *matr,int n); //创建一棵满二叉树;
1)浙大数据结构代码:相比下一个代码略显繁琐,但对抽象化数据描述更加合适。
/**
1)MaxHeap :最大堆;
操作集:
MaxHeap CreateHeap(int Maxsize); //创建最大堆,只是给堆创建一个存储空间。
bool IsFull(MaxHeap H); //判断最大堆是否已满;
bool Insert(MaxHeap H,,ElementType x); //将元素x插入到最大堆H中;
bool IsEmpty(MaxHeap H); //判断最大堆是否为空;
ElementType Delete(MaxHeap H); //删除并返回H中最大元素;
void downAdjust(MaxHeap H,int p); //下滤:将H中以H->data[p] 为根的子堆调整为最大堆;
void BuildHeap(MaxHeap H); //建堆;由于MaxHeap本身就是指针,所以不需要加引用。
void add_full_binary_tree(MaxHeap H,int *matr,int n); //创建一棵满二叉树;
data:
12
79 66 43 83 30 87 38 55 91 72 49 9
*/
/**
//浙大数据结构代码:
#include <iostream>
#include <algorithm>
using namespace std;
#define error -1e9
const int MAXDATA=1e9; // MAXDATA 定义为哨兵
const int MaxSize =110;
typedef int ElementType;
typedef struct HNode * Heap;
struct HNode
{
ElementType *data;
int Size;
int capacity;
};
typedef Heap MaxHeap;
MaxHeap CreateHeap(int Maxsize); //创建最大堆;
bool IsFull(MaxHeap H); //判断最大堆是否已满;
bool Insert(MaxHeap H,ElementType x); //将元素x插入到最大堆H中;
bool IsEmpty(MaxHeap H); //判断最大堆是否为空;
ElementType Delete(MaxHeap H); //删除并返回H中最大元素;
void downAdjust(MaxHeap H,int p); //下滤:将H中以H->data[p] 为根的子堆调整为最大堆;
void BuildHeap(MaxHeap H); //建堆;
void add_full_binary_tree(MaxHeap H,int *matr,int n); //创建一棵满二叉树;
int main()
{
int n;
cin >> n;
int a[n+1];
for(int i=1;i<=n;++i)
cin >> a[i];
MaxHeap MaxH=CreateHeap(MaxSize);
add_full_binary_tree(MaxH,a,n);
for(int i=1;i<=MaxH->Size;++i)
cout << MaxH->data[i] << ' ';
cout << endl;
BuildHeap(MaxH);
for(int i=1;i<=MaxH->Size;++i)
cout << MaxH->data[i] << ' ';
cout << endl;
ElementType val=Delete(MaxH);
cout << "delete:" << val << endl;
cout << "删除最大元素后的最大堆:\n";
for(int i=1;i<=MaxH->Size;++i)
cout << MaxH->data[i] << ' ';
cout << endl;
val=105;
Insert(MaxH,val);
cout << "插入一个元素后的最大堆:\n";
for(int i=1;i<=MaxH->Size;++i)
cout << MaxH->data[i] << ' ';
cout << endl;
return 0;
}
MaxHeap CreateHeap(int Maxsize) //创建最大堆;
{
MaxHeap H=new HNode;
H->data=new ElementType[MaxSize+1];
H->Size=0;
H->capacity=Maxsize;
H->data[0]=MAXDATA;
return H;
}
bool IsFull(MaxHeap H) //判断最大堆是否已满;
{
return H->Size==H->capacity;
}
bool Insert(MaxHeap H,ElementType x) //将元素x插入到最大堆H中;
{
if(IsFull(H))
{
printf("最大堆已满\n");
return false;
}
else
{
int i=++H->Size;
for(;H->data[i/2]<x;i/=2) //找插入节点的这一条路上的最小值,与x交换
H->data[i]=H->data[i/2];
H->data[i]=x;
return true;
}
}
bool IsEmpty(MaxHeap H) //判断最大堆是否为空;
{
return H->Size==0;
}
ElementType Delete(MaxHeap H) //删除并返回H中最大元素;
{
if(IsEmpty(H))
{
printf("最大堆已空\n");
return error;
}
else
{
ElementType MaxVal=H->data[1];
ElementType x=H->data[H->Size--];
int parent,child;
for(parent=1;2*parent<=H->Size;parent=child)
{
child=2*parent;
if(child < H->Size && H->data[child+1] > H->data[child])
++child;
if(x>=H->data[child])
break;
else
H->data[parent]=H->data[child];
}
H->data[parent]=x;
return MaxVal;
}
}
void add_full_binary_tree(MaxHeap H , int *matr,int n) //创建一棵满二叉树;
{
for(int i=1;i<=n;++i)
H->data[++H->Size]=matr[i];
}
void downAdjust(MaxHeap H,int p) //下滤:将H中以H->data[p] 为根的子堆调整为最大堆;
{
ElementType x=H->data[p];
int parent,child;
for(parent =p;2*parent<=H->Size;parent=child)
{
child=2*parent;
if(child<H->Size && H->data[child] < H->data[child+1])
++child;
if(x >= H->data[child])
break;
else
H->data[parent]=H->data[child];
}
H->data[parent]=x;
}
void BuildHeap(MaxHeap H) //建堆;
{
for(int i=H->Size/2;i>=0;--i)
downAdjust(H,i);
}
*/
2) 算法笔记代码:
/**
2) 算法笔记代码:
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=110; //堆的最大元素个数
int heap[maxn],num; //heap 为堆,n 为元素个数
void downAdjust(int low,int high); // 将heap 在[low,high] 范围内向下调整
void createHeap(); //建最大堆
int deleteTop(); //删除堆顶元素;
void upAdjust(int low,int high); //对heap在[low,high] 范围内向上调整;
void Insert(int x); //在堆中添加元素x;
int main()
{
int n;
cin >> n ;
for(int i=1;i<=n;++i)
cin >> heap[i];
num = n;
createHeap();
cout << "最大堆序列:\n";
for(int i=1;i<=num;++i)
cout << heap[i] << ' ';
cout << endl;
int val=deleteTop();
cout << "delete:" << val << endl;
cout << "删除最大元素后的最大堆:\n";
for(int i=1;i<=num;++i)
cout << heap[i] << ' ';
cout << endl;
val=105;
Insert(val);
cout << "增加一个元素后的最大堆:\n";
for(int i=1;i<=num;++i)
cout << heap[i] << ' ';
cout << endl;
return 0;
}
void downAdjust(int low,int high) // 将heap 在[low,high] 范围内向下调整
{
int parent=low,child=2*parent;
while(child<=high)
{
if(child!=high && heap[child+1] > heap[child])
++child;
if(heap[child]>heap[parent])
{
swap(heap[parent],heap[child]);
parent=child;
child=2*parent;
}
else
break;
}
}
void createHeap() //建最大堆
{
for(int i=num/2;i>=1;--i)
downAdjust(i,num);
}
int deleteTop() //删除堆顶元素;
{
int maxval=heap[1];
heap[1]=heap[num--];
downAdjust(1,num);
return maxval;
}
void upAdjust(int low,int high) //对heap在[low,high] 范围内向上调整;
{
int child=high , parent=child/2;
while(parent >= low)
{
if(heap[child] > heap[parent])
{
swap(heap[child],heap[parent]);
child=parent;
parent=child/2;
}
else
break;
}
}
void Insert(int x) //在堆中添加元素x;
{
heap[++num]=x;
upAdjust(1,num);
}