霍夫曼编码的编码效率怎么求?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了霍夫曼编码的编码效率怎么求?相关的知识,希望对你有一定的参考价值。

主要是为了比较霍夫曼编码与费诺编码孰优孰劣

求效率首先要求得信号的熵,也就是最小的编码长度,比如是2.3,然后再求霍夫曼码的平均编码长度(各个概率和码位相乘再求和)比如是2.7,那么效率就是0.85。

霍夫曼编码的编码效率,我想可以用压缩率来表示吧。随机选取一段字符,计算其编码长度为 n。再对其用霍夫曼编码,得到长度为 m。于是 m/n 就是压缩率。

霍夫曼编码是变长编码,思路:对概率大的编的码字短,概率小的编的码字长,这样一来所编的总码长就小,这样编码效率就高。

扩展资料:

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

参考资料来源:百度百科-霍夫曼编码

参考技术A 求效率首先要求得信号的熵,也就是最小的编码长度,比如是2.3,然后再求霍夫曼码的平均编码长度(各个概率和码位相乘再求和)比如是2.7,那么效率就是0.85 参考技术B 霍夫曼编码是变长编码,思路:对概率大的编的码字短,概率小的编的码字长,这样一来所编的总码长就小,这样编码效率就高。你上面那样求是不对的,除非你 参考技术C 霍夫曼编码的编码效率,我想可以用压缩率来表示吧。随机选取一段字符,计算其编码长度为 n。再对其用霍夫曼编码,得到长度为 m。于是 m/n 就是压缩率。

霍夫曼编码(急求啊!!)

霍夫曼编码现有一个由5个不同符号组成的30个符号的字符串:BABACACADADABBCBABEBEDDABEEEBB
计算
(1) 该字符串的霍夫曼码
(2) 该字符串的熵
(3) 该字符串的平均码长
(4) 编码前后的压缩比

参考技术A #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>

typedef struct
unsigned int weight;
unsigned int parent,lchild,rchild;
HTNode,*HuffmanTree;

typedef char **HuffmanCode;

typedef struct
unsigned int s1;
unsigned int s2;
MinCode;

void Error(char *message);
HuffmanCode HuffmanCoding(HuffmanTree HT,HuffmanCode HC,unsigned int *w,unsigned int n);
MinCode Select(HuffmanTree HT,unsigned int n);

void Error(char *message)

clrscr();
fprintf(stderr,"Error:%s\n",message);
exit(1);


HuffmanCode HuffmanCoding(HuffmanTree HT,HuffmanCode HC,unsigned int *w,unsigned int n)

unsigned int i,s1=0,s2=0;
HuffmanTree p;
char *cd;
unsigned int f,c,start,m;
MinCode min;
if(n<=1) Error("Code too small!");
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for(p=HT,i=0;i<=n;i++,p++,w++)

p->weight=*w;
p->parent=0;
p->lchild=0;
p->rchild=0;

for(;i<=m;i++,p++)

p->weight=0;
p->parent=0;
p->lchild=0;
p->rchild=0;

for(i=n+1;i<=m;i++)

min=Select(HT,i-1);
s1=min.s1;
s2=min.s2;
HT[s1].parent=i;
HT[s2].parent=i;
HT[i].lchild=s1;
HT[i].rchild=s2;
HT[i].weight=HT[s1].weight+HT[s2].weight;

printf("HT List:\n");
printf("Number\t\tweight\t\tparent\t\tlchild\t\trchild\n");
for(i=1;i<=m;i++)
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
i,HT[i].weight,HT[i].parent,HT[i].lchild,HT[i].rchild);
HC=(HuffmanCode)malloc((n+1)*sizeof(char *));
cd=(char *)malloc(n*sizeof(char *));
cd[n-1]='\0';
for(i=1;i<=n;i++)

start=n-1;
for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
if(HT[f].lchild==c) cd[--start]='0';
else cd[--start]='1';
HC[i]=(char *)malloc((n-start)*sizeof(char *));
strcpy(HC[i],&cd[start]);

free(cd);
return HC;


MinCode Select(HuffmanTree HT,unsigned int n)

unsigned int min,secmin;
unsigned int temp;
unsigned int i,s1,s2,tempi;
MinCode code;
s1=1;s2=1;
for(i=1;i<=n;i++)
if(HT[i].parent==0)

min=HT[i].weight;
s1=i;
break;

tempi=i++;
for(;i<=n;i++)
if(HT[i].weight<min&&HT[i].parent==0)

min=HT[i].weight;
s1=i;

for(i=tempi;i<=n;i++)
if(HT[i].parent==0&&i!=s1)

secmin=HT[i].weight;
s2=i;
break;

for(i=1;i<=n;i++)
if(HT[i].weight<secmin&&i!=s1&&HT[i].parent==0)

secmin=HT[i].weight;
s2=i;

if(s1>s2)

temp=s1;
s1=s2;
s2=temp;

code.s1=s1;
code.s2=s2;
return code;


void main()

HuffmanTree HT=NULL;
HuffmanCode HC=NULL;
unsigned int *w=NULL;
unsigned int i,n;
clrscr();
printf("Input n:\n");
scanf("%d",&n);
w=(unsigned int *)malloc((n+1)*sizeof(unsigned int *));
w[0]=0;
printf("Enter weight:\n");
for(i=1;i<=n;i++)

printf("w[%d]=",i);
scanf("%d",&w[i]);

HC=HuffmanCoding(HT,HC,w,n);
printf("HuffmanCode:\n");
printf("Number\t\tWeight\t\tCode\n");
for(i=1;i<=n;i++)
printf("%d\t\t%d\t\t%s\n",i,w[i],HC[i]);



程序运行:
首先用户先输入一个数n,以实现n个节点的Huffman Tree
之后输入权值w[1]~w[n],注意是unsigned int型数值。
然后程序自动生成Huffman Tree的存储形式的一张表格。
最后是Huffman Coding。

Sample Input:

Input n:
8
Enter weight:
w[1]=5
w[2]=29
w[3]=7
w[4]=8
w[5]=14
w[6]=23
w[7]=3
w[8]=11

Sample Output:

HT List:
Number weight parent lchild rchild
1 5 9 0 0
2 29 14 0 0
3 7 10 0 0
4 8 10 0 0
5 14 12 0 0
6 23 13 0 0
7 3 9 0 0
8 11 11 0 0
9 8 11 1 7
10 15 12 3 4
11 19 13 8 9
12 29 14 5 10
13 42 15 6 11
14 58 15 2 12
15 100 0 13 14
HuffmanCode:
Number Weight Code
1 5 0110
2 29 10
3 7 1110
4 8 1111
5 14 110
6 23 00
7 3 0111
8 11 010本回答被提问者采纳

以上是关于霍夫曼编码的编码效率怎么求?的主要内容,如果未能解决你的问题,请参考以下文章

为啥.NET BASE64编码效率这么低?

编码效率定义及计算

霍夫曼编码(急求啊!!)

求高手写个关于哈夫曼编码的算法

哈夫曼编码问题,高手帮我

哈夫曼树——按字符出现频率自动编码