编写函数实现哈夫曼编码。输入结点个数(保证个数>1)及各结点的权值,为各结点进行编码。
函数接口定义:
CreateHuffman_tree(HuffmanTree &HT,int n);/*建立n个叶子结点的哈夫曼树*/
Huffman_code(HuffmanTree HT,HuffmanCode &HC,int n);//求哈夫曼编码
其中 HT
为哈夫曼树,n
为叶子结点个数, HC
为哈夫曼编码。
裁判测试程序样例:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct hnode
{ int weight;
int lchild,rchild,parent;
}HTNode,*HuffmanTree;/*定义二叉树的存储结点*/
typedef char **HuffmanCode;
void Select(HTNode HT[],int len,int &s1,int &s2)//选出权值最小的两个结点,下标通过s1和s2传出去
{
int i,min1=32767,min2=32767;
for(i=1;i<=len;i++)
{
if(HT[i].weight<min1&&HT[i].parent==0)
{
s2=s1;
min2=min1;
min1=HT[i].weight;
s