A - the binary tree data structure of six experiments: Huffman coding

Description

There are a variety of character encoding, in addition to the familiar ASCII coding, Huffman coding (Huffman Coding) encoding is also a way, it is a variable word length coding. The method is fully based on the occurrence probability of the character to construct the shortest average code length, called optimal coding. Huffman coding is often used to compress the data file, the compression ratio is typically between 20% to 90%. Your job is the ratio of its length and the ASCII code for a Huffman code length of the string entered from the keyboard is obtained.
Input

A plurality of input data sets, each data line representing a string to be encoded.
Output

ASCII code corresponding to the character length La, the value of the length lh and huffman coding la / lh (the one decimal), a space between the data interval.
Sample

Input

AAAAABCD
THE_CAT_IN_THE_HAT

Output

64 13 4.9
144 51 2.8

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char a[10000];
int b[1000];

int main()
{
    int i,n,m,k,x1,x2;
    priority_queue<int,vector<int>,greater<int> >que;//优先队列,从小到大排列,最上面的最小
    while(scanf("%s",a)!=EOF)
    {
        k=0;
        memset(b,0,sizeof(b));
        n=strlen(a);
        m=n*8;
        for(i=0;i<n;i++)
            b[a[i]]++;
        for(i=0;i<150;i++)
            if(b[i]!=0)
            que.push(b[i]);
        while(!que.empty())
        {
           x1=que.top();
           que.pop();
           if(!que.empty())
           {
               x2=que.top();
           que.pop();
           k+=(x1+x2);//每次都取最小的和次小的元素相加。
           que.push(x1+x2);//将这两个的和放进去,再重新排列,最后求出是所有非叶子节点的和
           }
        }
        printf("%d %d %.1lf\n",m,k,1.0*m/k);//ASCII编码长度为字符串长度乘8
        }
        return 0;
}
Published 177 original articles · won praise 7 · views 30000 +

Guess you like

Origin blog.csdn.net/Fusheng_Yizhao/article/details/104873442