求赫夫曼编码,急呀!

来源:百度知道 编辑:UC知道 时间:2024/06/22 00:54:07
利用哈夫曼编码进行通讯可以大大提高信道利用率,缩短信息传输时间,降低传输成本,但是,这要求在发送端通过一个编码系统对待传数据进行预先编码;在接受端将传来的数据进行解码(复原)对于双工信道(即可以双向传输的信道),每端都要有一个完整的编/译码系统。试为这样的信息收发站写一个哈夫曼的编译码系统。
测试数据 用下表中给出的字符集和频度的实际统计数据建立哈夫曼树:
字符 A B C D E F G H I J K L M N
频度 64 13 22 32 103 21 15 47 57 1 5 32 20 57
字符 O P Q R S T U V W X Y Z 空格
频度 63 15 1 48 51 80 23 8 18 1 16 1 168
并实现以下报文的译码和输出:“THIS PROGRAM IS MY FAVORITE”。
程序的输入数据,功能和运行结果

#include <stdio,h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
typedef struct{
char data;
int weight;
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char * * HuffmanCode;
void Select(HuffmanTree &HT,int n,int m)
{HuffmanTree p=HT;
int tmp;
for(int j=n+1;j<=m;j++)
{int tag1,tag2,s1,s2;
tag1=tag2=32767;
for(int x=1;x<=j-1;x++)
{ if(p[x].parent==0&&p[x].weight<tag1)
{ tag1=p[x].weight;s1=x;}
}
for(int y=1;y<=j-1;y++)
{ if(p[y].parent==0&&y!=s1&&p[y].weight<tag2)
{ tag2=p[y].weight;s2=y;}
}
if(s1>s2) //将选出的两个节点中的序号较小的始终赋给s1
{ tmp=s1; s1=s2; s2=tmp;}
p[s1].parent=j;
p[s2].parent=j;
p[j].lchild=s1;
p[j].rchild=s2;
p[j].weight=p[s1].weight+p[s2].weight;
}
}
void HuffmanCoding(HuffmanTree