哈夫曼编码实现电文字符串编码
电文编码
从键盘接收一串电文字符,输出对应的Huffman编码。同时,能翻译由Huffman编码生成的代码串,输出对应的电文字符串
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 50
typedef char DataType;
typedef struct // 哈夫曼树结点的结构
{
DataType data; // 数据用字符表示
int weight; // 权值
int parent; // 双亲
int lchild,rchild; // 左右孩子
} HuffNode;
typedef struct // 哈夫曼编码的存储结构
{
DataType cd[MAXSIZE]; // 存放编码位串
int start; // 编码的起始位置
} HuffCode;
int HuffmanCreate(HuffNode *ht)
{
int n=0;
int i,j,p1,p2,m1,m2;
char c;
int frequency[58]={
0};
while((c=getchar())!='\n'){
frequency[c-65]++;
}
for(int i=0;i<58;i++){
if(frequency[i]!=0){
n++;
ht[n].data=i+65;
ht[n].weight=frequency[i];
}
}
for(i=1;i<n+1;i++)
{
printf("%c" ,ht