哈夫曼树的建立以及哈夫曼编码

 

通信的电文由字符集中的字母构成,每个字母在电文中会有一个出现的频率。为这些字母设计相应的哈夫曼编码!方法:每次在哈夫曼树构造过程中,两个最小数的选择总是最小的在左,而次小的在右。

输入输出样例:1组

#1

  • 样例输入:
    abcdefg# //#代表结束符
    0.31
    0.16
    0.10
    0.08
    0.11
    0.20
    0.04 //代表每个字母的出现频率
  • 样例输出:
    a:11
    b:101
    c:010
    d:1001
    e:011
    f:00
    g:1000
#include 
#include 
#include 
#define  max 100
typedef struct
{
    float weight;
    char lchild;
    char rchild;
    int parent;
} HType;
typedef struct
{
    char ch;
    int hfm[max];
    int start;
} HCode;
void Creat_huffMTree(HType HFMTree[],int n)
{
    int y1,y2,x1,x2;
    int i,j;
    HCode hc;
    for(i=0; i<2*n-1; i++) //HFMTree初始化
    {
        HFMTree[i].weight=0;
        HFMTree[i].parent=-1;
        HFMTree[i].lchild=-1;
        HFMTree[i].rchild=-1;
    }
    for(i=0; i

 

你可能感兴趣的:(数据结构,二叉树)