霍夫曼编码 c语言程序 求能运行的能算出结果的已知信源:s1=0.20,s2=0.18,s3=0.17,s4=0.15,s5=0.15,s6=0.05,s7=0.05,s8=0.05,给出其中一个霍夫曼码,并求其平均码长和编码效率.
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/15 20:31:29
霍夫曼编码 c语言程序 求能运行的能算出结果的已知信源:s1=0.20,s2=0.18,s3=0.17,s4=0.15,s5=0.15,s6=0.05,s7=0.05,s8=0.05,给出其中一个霍夫曼码,并求其平均码长和编码效率.
霍夫曼编码 c语言程序 求能运行的能算出结果的
已知信源:s1=0.20,s2=0.18,s3=0.17,s4=0.15,s5=0.15,s6=0.05,s7=0.05,s8=0.05,给出其中一个霍夫曼码,并求其平均码长和编码效率.
霍夫曼编码 c语言程序 求能运行的能算出结果的已知信源:s1=0.20,s2=0.18,s3=0.17,s4=0.15,s5=0.15,s6=0.05,s7=0.05,s8=0.05,给出其中一个霍夫曼码,并求其平均码长和编码效率.
#include "stdio.h"
#include "stdlib.h"
#include
#include
#define n 8
#define m 2*n-1
typedef struct
{
float weight;
int lchild,rchild,parent;
}HTNode;
typedef HTNode HuffmanTree[m];
void InitHuffmanTree(HuffmanTree T)
{
for(int i = 0;i < m;i++)
{
T[i].lchild = T[i].rchild = T[i].parent = -1;
T[i].weight = 0.0;
}
}
void InputWeight(HuffmanTree T)
{
float temp[n] = {0.20,0.18,0.17,0.15,0.15,0.05,0.05,0.05};
for(int i = 0;i < n;i++)
T[i].weight = temp[i];
}
void SelectMin(HuffmanTree T,int i,int *p1,int *p2)
{
int j;
*p1 = *p2 = m-1;
T[m-1].weight = FLT_MAX;
for(j = 0;j = 0;j--)
printf("%d",arr[j]);
printf("\n");
return count;
}
int main()
{
HuffmanTree T;
CreateHuffmanTree(T);
float ave = 0;
for(int i = 0;i < n;i++)
ave += T[i].weight*PrintHuffmanCode(T,i);
printf("平均码长:\t%f\n",ave);
printf("编码效率:\t%f%%\n",ave/ceil(log((double)n)/log((double)2))*100);
system("pause");
}