题目描述

已知某段通信报文内容,对该报文进行哈弗曼编码,并计算平均码长。
(1)统计报文中各字符出现的频度。(字符集范围为52个英文字母,空格,英文句号。报文长度<=200)
(2)构造一棵哈弗曼树,依次给出各字符编码结果。
(3)给字符串进行编码。
(4)给编码串进行译码。
(5)计算平均码长。
规定:
(1)结点统计:以ASCII码的顺序依次排列,例如:空格,英文句号,大写字母,小写字母。
(2)构建哈弗曼树时:左子树根结点权值小于等于右子树根结点权值。
(3)选择的根节点权值相同时,前者构建为双亲的左孩子,后者为右孩子。
(4)生成编码时:左分支标0,右分支标1。
输入
第一部分:报文内容,以’#'结束。
第二部分:待译码的编码串。
输出
依次输出报文编码串、译码串、平均码长,三者之间均以换行符间隔。
平均码长,保留小数点2位。
样例输入 Copy
Data structure is the way of computer storage and organization data. A data structure is a collection of data elements that exist between one or more specific relationships.#
000111111110101111110100101010000110111100010011011000001110011110011100101011010011100001101111011001011010101011001101110010101011101011110110101000110001101001010101010001111000101001110111111101000001101010100000010111111110101110110011111101001111010111011100000011110101111000100110000111011000000111101011111101001010100001101111000100110110000011100111100111011111101110010100000100001001111000100111101011101110101010110011000000111101011111100010000100110111000111101010100111001010110111110101100010110001011110010101100110000001010000110001001111011101010111010011101010100011010111010101000001110100110111100111100011110110001111110011010000010000111110100111101011101100110110101111011111001000100
样例输出 Copy
000111111110101111110100101010000110111100010011011000001110011110011100101011010011100001101111011001011010101011001101110010101011101011110110101000110001101001010101010001111000101001110111111101000001101010100000010111111110101110110011111101001111010111011100000011110101111000100110000111011000000111101011111101001010100001101111000100110110000011100111100111011111101110010100000100001001111000100111101011101110101010110011000000111101011111100010000100110111000111101010100111001010110111110101100010110001011110010101100110000001010000110001001111011101010111010011101010100011010111010101000001110100110111100111100011110110001111110011010000010000111110100111101011101100110110101111011111001000100
Data structure is the way of computer storage and organization data. A data structure is a collection of data elements that exist between one or more specific relationships.
4.11

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct hafutree
{char ch;int  parent;int  weight;int  lchild;int  rchild;
}hafutree;
float wt[200];
char ch_ch[200];
///初始化哈夫曼树
hafutree **inithafutree(int wet[],char diff_zf[],int n)
{ //printf("初始化哈夫曼树\n");//getchar();hafutree **head;head=(hafutree **)malloc(sizeof(hafutree *)*n*2);for(int i=1;i<=n;i++){head[i]=(hafutree *)malloc(sizeof(hafutree));head[i]->ch=diff_zf[i-1];head[i]->weight=wet[i-1];head[i]->parent=0;head[i]->lchild=0;head[i]->rchild=0;}for(int i=n+1;i<2*n;i++){head[i]=(hafutree *)malloc(sizeof(hafutree));head[i]->ch='#';head[i]->weight=0;head[i]->parent=0;head[i]->lchild=0;head[i]->rchild=0;}return head;
}
///创建好哈夫曼树
void create_hafutree(hafutree **head,int x)
{  //printf("创建哈弗树\n");//getchar();int m,n;//m第一个最小,n第二个最小int k=x;//记录下一次存放位置while(k<2*x-1){for(int j=1;j<=k;j++){if(head[j]->parent==0){m=j;break;}}for(int i=1;i<=k;i++){ if(head[m]->weight>head[i]->weight&&head[i]->parent==0){m=i;}}for(int j=1;j<=k;j++){if(head[j]->parent==0&&m!=j){n=j;break;}}         for(int i=1;i<=k;i++){ if(i==m){continue;}if(head[n]->weight>head[i]->weight&&head[i]->parent==0){n=i;}}head[++k]->weight=head[m]->weight+head[n]->weight;head[k]->lchild=m;head[k]->rchild=n;head[m]->parent=k;head[n]->parent=k;}/*getchar();printf("完成哈夫曼树\n");for(int i=1;i<2*x;i++){//printf("%d",head[i]->weight);printf("%d-节点值%c-",i,head[i]->ch);printf("父亲%d-",head[i]->parent);printf("左孩子%d-",head[i]->lchild);printf("右孩子%d\n",head[i]->rchild);}getchar();*/
}
//对预处理的权值和信息进行排序
void paixu(int getwt[],char getch[])
{ int a;char b;for(int i=0;i<strlen(getch)-1;i++){for(int j=0;j<strlen(getch)-1-i;j++){if(getch[j]>getch[j+1]){b=getch[j+1];getch[j+1]=getch[j];getch[j]=b;a=getwt[j+1];getwt[j+1]=getwt[j];getwt[j]=a;}}}for(int i=0;i<strlen(getch);i++){wt[i]=getwt[i];}
}
//计算码长
/*void vacaluct(char **ch,int getwt[],char getch[],int n,int m)
{  printf("%d,%d",n,m);getchar();int sum=0;for(int i=1;i<=n;i++){ printf("%d",getwt[i-1]);sum=sum+(getwt[i-1]/m)*strlen(ch[i]);}printf("%d",sum);}*/
//得到不同字符信息(名称和权值)
hafutree **getdifferentchar(char *text,int *number)
{   hafutree **head;//返回初始化后的哈弗树int m=0;//表示text的总数int n=0;//记录不同字符//int k=0;int flog=0;//用来做判断不同字符条件char *getch;//保存不同字符int *getwt;//保存不同字符的权值for(int i=0;text[i]!='#';i++){m++;}text[m]='\0';getch=(char *)malloc(sizeof(char)*m);getwt=(int *)malloc(sizeof(int)*m);for(int i=0;i<=m;i++)//初始化权值数组{getwt[i]=0;}getch[n]=text[n];getch[++n]='\0';for(int i=0;text[i]!='\0';i++){    flog=0;for(int j=0;getch[j]!='\0';j++){if(text[i]==getch[j]){getwt[j]++;flog=1;break;                                                                                                                                        }else{continue;}}if(flog==0){getch[n]=text[i];getwt[n]++;getch[++n]='\0';}}//调用初始化函数*number=strlen(getch);paixu(getwt,getch);//strcpy(getch,".ADabcdefghilmnoprstuwxyz");//puts(getch);head=inithafutree(getwt,getch,strlen(getch));return head;
}
进行哈夫曼编码
char **makecode(hafutree **head,int n)
{char a[n];int start;int p;int k;char **ch;ch=(char **)malloc(sizeof(char *)*n);ch[0]=(char *)malloc(sizeof(char)*5);for(int i=1;i<=n;i++){start=n-1;a[start]='\0';p=head[i]->parent;k=i;while(p){if(head[p]->lchild==k){a[--start]='0';}else if(head[p]->rchild==k){a[--start]='1';}k=p;p=head[p]->parent;}ch[i]=(char *)malloc(sizeof(char)*(n-start));strcpy(ch[i],&a[start]);}return ch;
}
///进行哈夫曼译码
char *translatecode(hafutree **head,char hafu_number[],int y)
{int k=0;//记录边缘后位置 int m,x,n;char *tr;tr=(char *)malloc(sizeof(char)*500);x=0;for(int i=k;hafu_number[i]!='\0';){ m=2*y-1;n=i;while(head[m]->lchild&&head[m]->rchild){if(hafu_number[n]=='0'){m=head[m]->lchild;}else if(hafu_number[n]=='1'){m=head[m]->rchild;}n++;i++; }tr[x++]=head[m]->ch;tr[x]='\0';}return tr;
}
int main()
{char hafuch[500];//待编译码char hafu_number[1000];//待解译码char hafuch_num[1000]="";hafutree **head;char *tr;//保存译码后的信息;char **ch;int n;//记录不同字符个数int j;gets(hafuch);gets(hafu_number);//初始化哈弗树head=getdifferentchar(hafuch,&n);//创建好哈弗树.create_hafutree(head,n);//哈弗编码ch[]保存信息ch=makecode(head,n); //哈弗译码//puts(hafuch);for(int i=0;hafuch[i]!='\0';i++){ j=1;while(j<=n){ if(head[j]->ch==hafuch[i]){ strcat(hafuch_num,ch[j]);break;}j++;}}puts(hafuch_num);// getchar();tr=translatecode(head,hafu_number,n);puts(tr);// getchar();float sum=0;for(int i=0;i<n;i++){ //printf("%f,%d,%d,%f\n",wt[i],strlen(hafuch),strlen(ch[i+1]),wt[i]*strlen(ch[i+1])/strlen(hafuch));// getchar();sum=sum+wt[i]*strlen(ch[i+1])/strlen(hafuch);// getchar();}printf("%0.2f",sum);// printf("%f",sum);
}

《考研-数据结构-哈弗曼树-已知某段通信报文内容,对该报文进行哈弗曼编码,并计算平均码长》相关推荐

  1. ComeFuture英伽学院——2020年 全国大学生英语竞赛【C类初赛真题解析】(持续更新)

    视频:ComeFuture英伽学院--2019年 全国大学生英语竞赛[C类初赛真题解析]大小作文--详细解析 课件:[课件]2019年大学生英语竞赛C类初赛.pdf 视频:2020年全国大学生英语竞赛 ...

  2. ComeFuture英伽学院——2019年 全国大学生英语竞赛【C类初赛真题解析】大小作文——详细解析

    视频:ComeFuture英伽学院--2019年 全国大学生英语竞赛[C类初赛真题解析]大小作文--详细解析 课件:[课件]2019年大学生英语竞赛C类初赛.pdf 视频:2020年全国大学生英语竞赛 ...

  3. 信息学奥赛真题解析(玩具谜题)

    玩具谜题(2016年信息学奥赛提高组真题) 题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业.有一天, 这些玩具小人把小南的眼镜藏了起来.小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的 ...

  4. 信息学奥赛之初赛 第1轮 讲解(01-08课)

    信息学奥赛之初赛讲解 01 计算机概述 系统基本结构 信息学奥赛之初赛讲解 01 计算机概述 系统基本结构_哔哩哔哩_bilibili 信息学奥赛之初赛讲解 02 软件系统 计算机语言 进制转换 信息 ...

  5. 信息学奥赛一本通习题答案(五)

    最近在给小学生做C++的入门培训,用的教程是信息学奥赛一本通,刷题网址 http://ybt.ssoier.cn:8088/index.php 现将部分习题的答案放在博客上,希望能给其他有需要的人带来 ...

  6. 信息学奥赛一本通习题答案(三)

    最近在给小学生做C++的入门培训,用的教程是信息学奥赛一本通,刷题网址 http://ybt.ssoier.cn:8088/index.php 现将部分习题的答案放在博客上,希望能给其他有需要的人带来 ...

  7. 信息学奥赛一本通 提高篇 第六部分 数学基础 相关的真题

    第1章   快速幂 1875:[13NOIP提高组]转圈游戏 信息学奥赛一本通(C++版)在线评测系统 第2 章  素数 第 3 章  约数 第 4 章  同余问题 第 5 章  矩阵乘法 第 6 章 ...

  8. 信息学奥赛一本通题目代码(非题库)

    为了完善自己学c++,很多人都去读相关文献,就比如<信息学奥赛一本通>,可又对题目无从下手,从今天开始,我将把书上的题目一 一的解析下来,可以做参考,如果有错,可以告诉我,将在下次解析里重 ...

  9. 信息学奥赛一本通(C++版) 刷题 记录

    总目录详见:https://blog.csdn.net/mrcrack/article/details/86501716 信息学奥赛一本通(C++版) 刷题 记录 http://ybt.ssoier. ...

  10. 最近公共祖先三种算法详解 + 模板题 建议新手收藏 例题: 信息学奥赛一本通 祖孙询问 距离

    首先什么是最近公共祖先?? 如图:红色节点的祖先为红色的1, 2, 3. 绿色节点的祖先为绿色的1, 2, 3, 4. 他们的最近公共祖先即他们最先相交的地方,如在上图中黄色的点就是他们的最近公共祖先 ...

最新文章

  1. 怎样向IT行业的朋友说明《圣经》的重要性
  2. tomcat 和 jdk 版本 对应关系
  3. Linux-鸟菜-6-文件搜索
  4. web.xml中的ContextLoaderListener和DispatcherServlet区别
  5. ORACLE 10046 Trace
  6. AppiumDriver java部分api
  7. java 先进先出的map_「 深入浅出 」java集合Collection和Map
  8. Kotlin学习记录1
  9. 内部类、抽象类、接口基本知识详解
  10. Python机器学习:SVM002最大化margin
  11. NOIP模拟题——计数
  12. 为什么8位有符号数的范围为“-128 — +127”?(转载加补充)
  13. jquery中的map()方法与js中的map()方法
  14. 跟着偶像学大数据——开端篇
  15. django filter查询多选_Django重置密码漏洞(CVE201919844)复现和分析
  16. ZStack实践汇 | 快照和备份的区别
  17. 2.2中文分词和新词识别
  18. 相机快门和曝光时间的确定
  19. 【大学生辩论赛】如何练习自己的辩论口才
  20. 项目开发中遇到接收串口数据时序混乱的问题

热门文章

  1. c语言 1为真还是0为真,C语言中逻辑表达式和关系表达式的值为真时,到底是用非0的任何数表示还是只能用1来表示?...
  2. SpringBoot整合log4j2
  3. HyperLedger Fabric ChainCode开发——shim.ChaincodeStubInterface用法
  4. 【白嫖系列-怕违规】告别BDWP龟速下载,体验飞一般的感觉
  5. pmos管驱动原理图
  6. DDR功能点 ODT ZQ校准
  7. Tensorflow-saver模型参数保存及载入
  8. 零基础html5网站开发学习步骤方法
  9. pe系统 服务器维护,FirPE 维护系统
  10. XP桌面图标阴影的去除