通过下面的五个方法实现:
- 生成赫夫曼树 *void creHuffmanTree(int n,int m,int w, HTree HT)
- 创建赫夫曼编码 void cretHuffmanCoding(HTree HT, HuffmanCode hc, int n)
- 筛选最小值和次小值
void select(HTree HT, int n, int * smallestIndex, int * secondSmallestIndex) - 打印赫夫曼表 void printtable(HTree HT, int n)
- 打印赫夫曼码 void printCodes(HTree HT, HuffmanCode hc, int n)
代码:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define INT_MAX 10000
//author : huxuehao
//自定义结构体
typedef struct{
int weight;
int parent;
int lchild;
int rchild;
}HTNode, *HTree;
//存放赫夫曼编码
typedef char ** HuffmanCode;
//一、生成赫夫曼树
void creHuffmanTree(int n,int m,int *w, HTree HT);
//二、创建赫夫曼编码
void cretHuffmanCoding(HTree HT, HuffmanCode hc, int n);
//三、筛选最小值和次小值
void select(HTree HT, int n, int * smallestIndex, int * secondSmallestIndex);
//四、打印赫夫曼表
void printtable(HTree HT, int n);
//五、打印赫夫曼码
void printCodes(HTree HT, HuffmanCode hc, int n);
// 主程序
int main()
{
int w[5] = {8,3,6,1,4};
int n = 5;
int m = 2 * n - 1;
// 生成HTree类型的数组HT, 长度是m+1
HTree HT = (HTree)malloc((m + 1) * sizeof(HTNode));
creHuffmanTree(n, m, w, HT);
//创建存放哈弗曼码的“二维数组”
HuffmanCode HC = (HuffmanCode)malloc((n+1)*sizeof(char *));
cretHuffmanCoding(HT, HC, n);
printf("\nok\n");
return 0;
}
// 一、生成赫夫曼树
// n 表示叶子节点的个数
// m 表示所生成赫夫曼树的节点的个数
// w 表示数组的首地址
// HT表示哈夫曼树
void creHuffmanTree(int n,int m,int *w, HTree HT)
{
// 保证生成的赫夫曼树有意义
if(n < 1 || HT == NULL){
return;
}
// *HT相等于HT[0]
//*HT = (HTNode){INT_MAX, 0, 0, 0};
HT[0].weight = INT_MAX;
HT[0].parent = 0;
HT[0].lchild = 0;
HT[0].rchild = 0;
// 此处的 *p 相等于 HT[1]
//HTree p = HT+1;
int i;
//将HTree数组中的下标1~n的‘值’初始化
for(i=1; i<=n; ++i/*++p,++w*/){
//*p=(HTNode){*w, 0, 0, 0};
HT[i].weight = w[i-1];
HT[i].parent = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
}
//将HTree数组中的下标n+1 ~ m的‘值’初始化
for(; i<=m; ++i/*,++p*/){
//*p=(HTNode){0, 0, 0, 0};
HT[i].weight = 0;
HT[i].parent = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
}
//打印一下目前的赫夫曼表
//printtable(HT, n);
// 第一次从1~n中寻找,第二次从1~(n+1)中寻找,...
for(i=n; i <= m-1; ++i){
// s(smalllest)用于存储最小的值, ss(second-smalllest)用于存储次小的值
// 并将s、ss对应的值置为最大
int s = 0;
int ss= 0;
select(HT, i, &s, &ss);
HT[s].parent = i+1;
HT[ss].parent = i+1;
HT[i+1].lchild = s;
HT[i+1].rchild = ss;
// 给生成的父节点赋权值
HT[i+1].weight = HT[s].weight + HT[ss].weight;
}
//打印一下目前的赫夫曼表
printtable(HT, n);
}
//二、创建赫夫曼编码
void cretHuffmanCoding(HTree HT, HuffmanCode hc, int n)
{
char *cd;
int i, start, p;
int c;
cd = (char *)malloc(n*sizeof(char)); //申请存放当前编码的工作空间
cd[n-1] = '\0'; //因为从右向左逐位存放编码,所以首先存放编码结束符
for(i=1; i<=n; i++) //计算n个叶子结点对应的赫夫曼编码
{
start = n-1; //初始化编码起始指针
for(c=i,p=HT[i].parent; p!=0; c=p,p=HT[p].parent){//从叶子到根结点求编码
if(HT[p].lchild==c) {
cd[--start]='0'; //c为左节点 标0
}
else {
cd[--start]='1'; //c为右节点 标1
}
}
hc[i] = (char *)malloc((n-start)*sizeof(char)); //为第i个编码分配空间
//strcpy(hc[i],&cd[start]);
for(int j=0; j<n-start; j++)
hc[i][j] = cd[start + j];
}
free(cd);
//打印赫夫曼
printCodes(HT, hc, n);
}
/*
*三、筛选最小值和次小值
*筛选最小值和次小值时,一个重要的条件是在父节点为0的节点中寻找
*HT:表示哈夫曼树
*n:表示在1~n中寻找
*smallestIndex:用于存放最小的索引
*secondSmallestIndex:用于存放次小的索引
*/
void select(HTree HT, int n, int * smallestIndex, int * secondSmallestIndex)
{
int i;
//遍历1~n的元素
for(i=1;i<=n;++i){
if(HT[i].parent == 0){
if(HT[i].weight < HT[*smallestIndex].weight){
//如果当前的值小于当前找到的最小的值
*secondSmallestIndex = *smallestIndex;
*smallestIndex = i;
}else if(HT[i].weight < HT[*secondSmallestIndex].weight){
//如果当前的值小于当前找到的次小的值
*secondSmallestIndex = i;
}
}
}
}
//四、打印赫夫曼表
void printtable(HTree HT, int n) {
printf("\nHTTABLE:\n————————————————————\n");
printf("| code\t|weight\t|parent\t|lchild\t|rchild|\n");
for(int q=1; q<=2*n-1; q++){
printf("————————————————————\n");
printf("|%4d\t|%4d\t|%4d\t|%4d\t|%4d |\n",q,HT[q].weight,HT[q].parent,HT[q].lchild,HT[q].rchild);
}
printf("————————————————————\n\n");
}
//五、打印赫夫曼码
void printCodes(HTree HT, HuffmanCode hc, int n){
printf("\nCODE:\n————————————\n");
printf("| 字符\t| 赫夫曼编码 |\n");
for(int i=1; i<=n; i++){
printf("————————————\n");
printf("|%4d\t| %10s |\n",HT[i].weight,hc[i]);
}
// printf("\n");
printf("————————————\n\n");
}
测试结果: