puk1521 赫夫曼树编码

时间:2021-07-17 07:07:41

Description

An entropy encoder is a data encoding method that achieves lossless data compression by encoding a message with "wasted" or "extra" information removed. In other words, entropy encoding removes information that was not necessary in the first place to accurately encode the message. A high degree of entropy implies a message with a great deal of wasted information; english text encoded in ASCII is an example of a message type that has very high entropy. Already compressed messages, such as JPEG graphics or ZIP archives, have very little entropy and do not benefit from further attempts at entropy encoding. 

English text encoded in ASCII has a high degree of entropy because all characters are encoded using the same number of bits, eight. It is a known fact that the letters E, L, N, R, S and T occur at a considerably higher frequency than do most other letters in english text. If a way could be found to encode just these letters with four bits, then the new encoding would be smaller, would contain all the original information, and would have less entropy. ASCII uses a fixed number of bits for a reason, however: it’s easy, since one is always dealing with a fixed number of bits to represent each possible glyph or character. How would an encoding scheme that used four bits for the above letters be able to distinguish between the four-bit codes and eight-bit codes? This seemingly difficult problem is solved using what is known as a "prefix-free variable-length" encoding. 

In such an encoding, any number of bits can be used to represent any glyph, and glyphs not present in the message are simply not encoded. However, in order to be able to recover the information, no bit pattern that encodes a glyph is allowed to be the prefix of any other encoding bit pattern. This allows the encoded bitstream to be read bit by bit, and whenever a set of bits is encountered that represents a glyph, that glyph can be decoded. If the prefix-free constraint was not enforced, then such a decoding would be impossible. 

Consider the text "AAAAABCD". Using ASCII, encoding this would require 64 bits. If, instead, we encode "A" with the bit pattern "00", "B" with "01", "C" with "10", and "D" with "11" then we can encode this text in only 16 bits; the resulting bit pattern would be "0000000000011011". This is still a fixed-length encoding, however; we’re using two bits per glyph instead of eight. Since the glyph "A" occurs with greater frequency, could we do better by encoding it with fewer bits? In fact we can, but in order to maintain a prefix-free encoding, some of the other bit patterns will become longer than two bits. An optimal encoding is to encode "A" with "0", "B" with "10", "C" with "110", and "D" with "111". (This is clearly not the only optimal encoding, as it is obvious that the encodings for B, C and D could be interchanged freely for any given encoding without increasing the size of the final encoded message.) Using this encoding, the message encodes in only 13 bits to "0000010110111", a compression ratio of 4.9 to 1 (that is, each bit in the final encoded message represents as much information as did 4.9 bits in the original encoding). Read through this bit pattern from left to right and you’ll see that the prefix-free encoding makes it simple to decode this into the original text even though the codes have varying bit lengths. 

As a second example, consider the text "THE CAT IN THE HAT". In this text, the letter "T" and the space character both occur with the highest frequency, so they will clearly have the shortest encoding bit patterns in an optimal encoding. The letters "C", "I’ and "N" only occur once, however, so they will have the longest codes. 

There are many possible sets of prefix-free variable-length bit patterns that would yield the optimal encoding, that is, that would allow the text to be encoded in the fewest number of bits. One such optimal encoding is to encode spaces with "00", "A" with "100", "C" with "1110", "E" with "1111", "H" with "110", "I" with "1010", "N" with "1011" and "T" with "01". The optimal encoding therefore requires only 51 bits compared to the 144 that would be necessary to encode the message with 8-bit ASCII encoding, a compression ratio of 2.8 to 1. 

Input

The input file will contain a list of text strings, one per line. The text strings will consist only of uppercase alphanumeric characters and underscores (which are used in place of spaces). The end of the input will be signalled by a line containing only the word “END” as the text string. This line should not be processed.

Output

For each text string in the input, output the length in bits of the 8-bit ASCII encoding, the length in bits of an optimal prefix-free variable-length encoding, and the compression ratio accurate to one decimal point.

Sample Input

AAAAABCD
THE_CAT_IN_THE_HAT
END

Sample Output

64 13 4.9
144 51 2.8
/*
赫夫曼树的应用:
统计一个字符串中不同字符的数量,分别用ASCII、赫夫曼树进行编码。比较两者所占空间的大小。
这里举例统一由大写字母和下划线组成。
输入:
AAAAABCD
THE_CAT_IN_THE_HAT
END ------结束字符串
输出:
64 13 4.9
144 51 2.8
*/
#include<stdio.h>
#include<string.h>
# define N 28
typedef struct{
char ch; //存放字符
int num; //存放字符出现次数
int F,L,R; //父节点 左子节点 右子节点
int sum; //赫夫曼树表示这个字符的编码长度
char code[20];
}Hcode;
Hcode HT[N*2];
//统计字符及字符出现次数
int Order(Hcode HT[],char *s)
{
int i,j,x,l=0,flag;
i=strlen(s); //字符串长度
for(j=0;j<i;j++){
flag = 0; // flag作为标识符
for(x=1;x<=l;x++){
if(s[j]==HT[x].ch){
flag=x; //如果这到字符与数组中的某一个相同 ,用标识符记下位置 并跳出循环
break;
}else{
flag=0; // 如果这到字符与数组中的都不相同,,标识符为 0
}
}
if(flag==0){ //标识符为 0,表示 这个字符没有保存过,这时就要把这个字符存入数组
l++;
HT[l].ch=s[j];
HT[l].num=1;
HT[l].F=0;
HT[l].sum=0;
}else{ // 标识符不为 0,表示这个字符保存过, 这是要吧数组中这个字符的数量加 1
HT[flag].num++;
}
}
/*for(i=1;i<=l;i++){ //这串代码可以用来测试上面的处理结果的正误
printf("\n===%c======%d\n",HT[i].ch,HT[i].num);
}*/
return l;
}
//用赫夫曼树对字符进行编码
void Haffman(Hcode HT[],int l){
int i,m,x,y,z,p,min1,min2;
m=2*l-1;
for(i=l+1;i<=m;i++){
p=i-1;
min1=9999;min2=9999;
for(x=1;x<=p;x++){
// printf("==%d===%c===%d\n",HT[x].num,HT[x].ch,min1);
if((HT[x].F==0)&&(HT[x].num<=min1)){
min1=HT[x].num;
y=x;
}
}
// printf("==%d===%c===\n",HT[y].num,HT[y].ch);
for(x=1;x<=p;x++){
// printf("==%d===%c===%d\n",HT[x].num,HT[x].ch,min2);
if((x!=y)&&(HT[x].F==0)&&(HT[x].num<=min2)){
min2=HT[x].num;
z=x;
}
}
//上面的两个for循环,是获取两个权值最小的位置。然后下面 为这两个位置创建父节点,同时保存父子节点的关系
// printf("==%d===%c===\n",HT[z].num,HT[z].ch);
HT[i].num=HT[y].num + HT[z].num;
HT[i].F=0; HT[i].sum=0;
HT[i].L=y; HT[i].R=z;
HT[y].F=i; HT[z].F=i; // printf("==%d===%d=====%d===\n",HT[i].num,HT[i].L,HT[i].R);
}
}
//编码
void Tree(Hcode HT[],int l)
{
int i,x,f,a,b;
char str[20];
for(i=1;i<=l;i++){
a=0;
//对这位字符进行编码
for(x=i,f=HT[x].F;f!=0;x=f,f=HT[x].F){
if(HT[f].L==x) str[a++]='1';
else str[a++]= '0';
}
b=0;
a--;
//把编码传给这位字符的编码位置
while(a>=0){
HT[i].code[b++]=str[a--];
str[a+1]='\0';
}
printf("==%s==%c==\n",HT[i].code,HT[i].ch);
}
}
int main()
{
char str[1000];
int l,i,all,max,x,f;
float a,c,b;
char str1[4]={"END"};
scanf("%s",str);
while(strcmp(str,str1)!=0){ //结束标志
all=0;
for(i=1;i<=N*2;i++) HT[i].ch=' ';//每次处理都要对全局变量数组进行初始化
l=Order(HT,str);
max=strlen(str)*8;
if(l==1) HT[l].sum=1; //只有一种字符时,要注意这种情况要特殊处理。
else{
Haffman(HT,l); // 用赫夫曼树对字符进行编码
Tree(HT,l); //输入每位字符对应的编码
}
for(i=1;i<=l;i++){
//测出所需编码位数
for(x=i,f=HT[x].F;f!=0;x=f,f=HT[x].F){
HT[i].sum++;
}
all=all+HT[i].num*HT[i].sum;
}
a=max; c=all;
b=a/c;
printf("%d %d %.1f\n",max,all,b); scanf("%s",str);
}
return 0;
}