关于构造赫夫曼树的问题 构造赫夫曼树要求:根据任意给定若干结点的权值,构造一棵最优二叉树并给出对应的编码.怎么写算法,

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/02 20:23:33

关于构造赫夫曼树的问题 构造赫夫曼树要求:根据任意给定若干结点的权值,构造一棵最优二叉树并给出对应的编码.怎么写算法,
关于构造赫夫曼树的问题
构造赫夫曼树
要求:根据任意给定若干结点的权值,构造一棵最优二叉树并给出对应的编码.
怎么写算法,

关于构造赫夫曼树的问题 构造赫夫曼树要求:根据任意给定若干结点的权值,构造一棵最优二叉树并给出对应的编码.怎么写算法,
#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