作业帮 > 综合 > 作业

关于构造赫夫曼树的问题

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/30 12:41:14
关于构造赫夫曼树的问题
构造赫夫曼树
要求:根据任意给定若干结点的权值,构造一棵最优二叉树并给出对应的编码.
怎么写算法,
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
typedef char ElemType;
typedef struct
{
ElemType elem;
unsigned int m_weight;
unsigned int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char** HuffmanCode;
typedef int Status;
typedef struct weight
{
char elem;
unsigned int m_weight;
}Weight; // save the information of the symbolizes;
void HuffmanCoding(HuffmanTree *,HuffmanCode *,Weight *,int);
void Select(HuffmanTree,int,int *,int *);
void OutputHuffmanCode(HuffmanTree,HuffmanCode,int);
Status main(void)
{
HuffmanTree HT;
HuffmanCode HC;
Weight *w;
char c; // the symbolizes;
int i,n; // the number of elements;
int wei; // the weight of a element;
printf("请输入要编码的字符种类数:" );
scanf("%d",&n);
w=(Weight *)malloc(n*sizeof(Weight));
for(i=0;i