#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 6
typedef struct Huff
{
int freq;
Huff *left;
Huff *middle;
Huff *right;
char c;
}HuffNode,*pHuff;
typedef struct
{
int len;
pHuff *arr;
}MinHeap;
void swop(pHuff *a,pHuff *b)
{
pHuff temp=*a;
*a=*b;
*b=temp;
}
void minHeapify(pHuff *a,int len,int i)
{
if(len<=0)
return;
int l=i*2,r=i*2+1,min=a[i]->freq,p=i;
if(l<len && a[l]->freq<a[i]->freq)
{
min=a[l]->freq;
p=l;
}
if(r<len && a[r]->freq<min)
{
min=a[r]->freq;
p=r;
}
if(p!=i)
{
swop(&a[i],&a[p]);
minHeapify(a,len,p);
}
}
MinHeap* buildHeap(int *a,int len,char *cs)
{
MinHeap *m=(MinHeap *)malloc(sizeof(MinHeap));
m->arr=(pHuff*)malloc(len*sizeof(pHuff));
m->len=len;
for(int i=0;i<len;i++)
{
m->arr[i]=(pHuff)malloc(sizeof(
算法导论 练习题 16.3-7
最新推荐文章于 2023-04-22 23:15:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)