码迷,mamicode.com
首页 > 其他好文 > 详细

霍夫曼编码

时间:2015-05-22 00:28:51      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

数据结构与算法---霍夫曼编码:
在计算机数据处理中,霍夫曼编码使用变长编码表对源符号(如文件中的一个字母)进行编码,其中变长编码表是通过一种评估来源符号出现几率的方法得到的,出现几率高的字母使用较短的编码,反之出现几率低的则使用较长的编码,这便使编码之后的字符串的平均长度、期望值降低,从而达到无损压缩数据的目的。

例如,在英文中,e的出现机率最高,而z的出现概率则最低。当利用霍夫曼编码对一篇英文进行压缩时,e极有可能用一个比特来表示,而z则可能花去25个比特(不是26)。用普通的表示方法时,每个英文字母均占用一个字节(byte),即8个比特。二者相比,e使用了一般编码的1/8的长度,z则使用了3倍多。倘若我们能实现对于英文中各个字母出现概率的较准确的估算,就可以大幅度提高无损压缩的比例。

(1)实现输入的英文字符串输入,并设计算法分别统计不同字符在该字符串中出现的次数,字符要区分大小写;
(2)实现赫夫曼树的构建算法;
(3)遍历赫夫曼生成每个字符的二进制编码,显示输出每个字母的编码。

技术分享
  1 #include<stdio.h>
  2 
  3 #define MAXBIT 100
  4 #define MAXVALUE 100000
  5 #define MAXNODE 100
  6 #define MAXLEAF MAXNODE * 2 - 1
  7 
  8 typedef struct{
  9     int weight;
 10     int parent, lchild, rchild;
 11 }HNode;
 12 typedef struct{
 13     int bit[MAXBIT];
 14     int start;
 15 }HCode;
 16 typedef struct{
 17     char ch;
 18     int count;
 19 }count;
 20 //实现赫夫曼树的构建算法
 21 void HuffmanCoding(HNode HuffNode[MAXLEAF], count *count, int n)
 22 {
 23     int i, j, m1, m2, x1, x2;
 24     for (i=0; i < 2*n-1; i++)
 25     {
 26         HuffNode[i].weight = 0;
 27         HuffNode[i].parent =-1;
 28         HuffNode[i].lchild =-1;
 29         HuffNode[i].lchild =-1;
 30     }
 31     for (i = 0; i < n; i++)
 32     {
 33         HuffNode[i].weight = count[i].count;
 34     }
 35     for (i=0; i<n-1; i++)
 36     {
 37         m1=m2=MAXVALUE;
 38         x1=x2=0;
 39         for (j=0; j<n+i; j++)
 40         {
 41             if (HuffNode[j].weight < m1 && HuffNode[j].parent==-1)
 42             {
 43                 m2=m1; 
 44                 x2=x1; 
 45                 m1=HuffNode[j].weight;
 46                 x1=j;
 47             }
 48             else if (HuffNode[j].weight < m2 && HuffNode[j].parent==-1)
 49             {
 50                 m2=HuffNode[j].weight;
 51                 x2=j;
 52             }
 53         }
 54         HuffNode[x1].parent  = n+i;
 55         HuffNode[x2].parent  = n+i;
 56         HuffNode[n+i].weight = HuffNode[x1].weight + HuffNode[x2].weight;
 57         HuffNode[n+i].lchild = x1;
 58         HuffNode[n+i].rchild = x2;
 59     }
 60 };
 61 
 62 int main(void)
 63 {
 64     int i, j, n, c, p;
 65     HNode HuffTree[MAXNODE];
 66     HCode HuffCode[MAXLEAF], cd;
 67     count count[MAXNODE];
 68     FILE* fp;
 69     char ch;
 70     for(i = 0; i < MAXNODE; i++)
 71     {
 72         count[i].count = 0;
 73         count[i].ch = 0;
 74     }
 75     i = 0;
 76     fp=fopen("test.txt","r");
 77     if(fp)
 78     while (fscanf(fp,"%c",&ch)!=EOF)
 79     {
 80         if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))
 81         {
 82         for (j = 0; j < MAXNODE; j++)
 83         {
 84             if(count[j].ch == ch)
 85             {
 86                 count[j].count++;
 87                 printf("%c", count[i].ch);
 88                 break;
 89             }
 90             else if (j == MAXNODE - 1)
 91             {
 92                 count[i].count++;
 93                 count[i++].ch = ch;
 94                 printf("%c", count[i].ch);
 95             }
 96         }
 97         }
 98     }
 99     fclose(fp);
100     n = i;
101     HuffmanCoding(HuffTree, count, n);
102     
103     for (i=0; i < n; i++)
104     {
105         cd.start = n-1;
106         c = i;
107         p = HuffTree[c].parent;
108         while (p != -1)
109         {
110             if (HuffTree[p].lchild == c)
111                 cd.bit[cd.start] = 0;
112             else
113                 cd.bit[cd.start] = 1;
114             cd.start--;
115             c=p;                    
116             p=HuffTree[c].parent;
117         } 
118         
119         for (j=cd.start+1; j<n; j++)
120         { HuffCode[i].bit[j] = cd.bit[j];}
121         HuffCode[i].start = cd.start;
122     }
123     
124     for (i=0; i<n; i++)
125     {
126         printf ("%c ‘s Huffman code is: ", count[i].ch);
127         for (j=HuffCode[i].start+1; j < n; j++)
128         {
129             printf ("%d", HuffCode[i].bit[j]);
130         }
131         printf ("\n");
132     }
133     return 0;
134 }
huffman

外部输入文件test.txt:

技术分享
Napoleon to Josephine : I have your letter, my adorable love. It has filled my heart with joy ... since I left you I have been sad all the time. My only happiness is near you. I go over endlessly in my thought of your kisses, your tears, your delicious jealousy. The charm of my wonderful Josephine kindles a living, blazing fire in my heart and senses. When shall I be able to pass every minute near you, with nothing to do but to love you and nothing to think of but the pleasure of telling you of it and giving you proof of it? I loved you some time ago; since then I feel that I love you a thousand times better. Ever since I have known you I adore you more every day. That proves how wrong is that saying of La Bruyere "Love comes all of a sudden." Ah, let me see some of your faults; be less beautiful, less graceful, less tender, less good. But never be jealous and never shed tears. Your tears send me out of my mind ... they set my very blood on fire. Believe me that it is utterly impossible for me to have a single thought that is not yours, a single fancy that is not submissive to your will. Rest well. Restore your health. Come back to me and then at any rate before we die we ought to be able to say: "We were happy for so very many days!" Millions of kisses even to your dog.
test.txt

 

霍夫曼编码

标签:

原文地址:http://www.cnblogs.com/jywt/p/4520934.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!