树和二叉树的基本运算实现
来源:互联网 发布:安卓微信数据能恢复吗 编辑:程序博客网 时间:2024/06/15 05:06
问题描述:树和二叉树的基本运算实现
设计一个程序exp7-6.cpp,构造一棵哈夫曼树,输出对应的哈夫曼编码和平均查找长度。并用表7.8所示的数据进行验证
表7.8 单词及出现的频度
单词
The
of
a
to
and
in
that
he
is
at
on
for
His
are
be
出现频度
1192
677
541
518
462
450
242
195
190
181
174
157
138
124
123
实验要求结果:算法设计:哈弗曼树建立和哈弗曼编码的创建
完成时间:2017年6月22日
代码:
/*问题描述:树和二叉树的基本运算实现作者:何知令完成时间:2017年6月22日*/#include <stdio.h>#define N 30typedef struct{ char data[5]; double weight; int parent; int lchild; int rchild;} HTNode;typedef struct{ char cd[N/2]; int start;} HCode;void CreatHt(HTNode ht[],int n){ int i,k,lnode,rnode; double min1,min2; for(i=0; i<2*n-1; ++i) ht[i].parent=ht[i].lchild=ht[i].rchild=-1; for(i=n; i<2*n-1; ++i) { min1=min2=32767; lnode=rnode=-1; for(k=0; k<=i-1; ++k) { if(ht[k].parent==-1) { if(ht[k].weight<min1) { min2=min1; rnode=lnode; min1=ht[k].weight; lnode=k; } else if(ht[k].weight<min2) { min2=ht[k].weight; rnode=k; } } } ht[i].weight=ht[lnode].weight+ht[rnode].weight; ht[i].lchild=lnode; ht[i].rchild=rnode; ht[lnode].parent=i; ht[rnode].parent=i; }}void CreatHCode(HTNode ht[],HCode hcd[],int n){ int i,f,c; HCode hc; for(i=0; i<n; ++i) { hc.start=n; c=i; f=ht[i].parent; while(f!=-1) { if(ht[f].lchild==c) hc.cd[--hc.start]='0'; else hc.cd[--hc.start]='1'; c=f; f=ht[f].parent; } hcd[i]=hc; }}void DispHCode(HTNode ht[],HCode hcd[],int n){ int i,k; double sum=0,m=0; int j; printf("输出哈夫曼编码:\n"); //输出哈夫曼编码 for (i=0; i<n; i++) { j=0; printf("%s:\t",ht[i].data); for (k=hcd[i].start; k<=n; k++) { printf("%c",hcd[i].cd[k]); j++; } m+=ht[i].weight; sum+=ht[i].weight*j; printf("\n"); } printf("平均长度%f",1.0*sum/m-1);}int main(){ HTNode ht[N]= {{"the",1192},{"of",677},{"a",541},{"to",518},{"and",462},{"in",450},{"that",242},{"he",195},{"is",190},{"at",181},{"on",174,},{"for",157},{"His",138},{"are",124},{"be",123}}; HCode hcd[N/2]; CreatHt(ht,N/2); CreatHCode(ht,hcd,N/2); DispHCode(ht,hcd,N/2); return 0;}程序运行结果展示:
知识点总结:哈弗曼树的建立和哈弗曼编码的实现
学习心得:...
阅读全文
0 0
- 数据结构 - 树和二叉树的基本运算实现
- 树和二叉树的基本运算实现
- Java实现二叉树的基本运算
- 二叉树的基本运算
- 二叉树的基本运算
- 二叉树的基本运算
- 二叉树的基本运算
- 二叉树的基本运算及其实现的例程
- 实现二叉树的各种基本运算的算法
- 实现二叉树各种基本运算的算法
- 第十周:二叉树的基本运算及其实现
- java 实现二叉树结构的基本运算详细代码
- 实现二叉树的各种基本运算方法
- 树和二叉树的基本运算实现-哈夫曼树/哈夫曼编码
- 第7次数据结构上机--树和二叉树的基本运算实现
- 二叉树的基本运算实验
- 二叉树的基本运算实验
- 遍历二叉树的基本运算
- Android自定义view动态绘制百分比圆环进度条
- MySQL数据库锁介绍
- 性能测试的并发数如何计算
- 书写高性能的css
- tablayout之tab之间加分割线
- 树和二叉树的基本运算实现
- Android(九图工具RecyclerView聊天界面)
- Kotlin设计模式
- java——基于zxing的二维码生成器
- 获取公网IP
- HttpWatch和jmeter的一次性能测试
- Win7 64位+Python3.6.1(Anaconda3-4.4.0)安装配置 OpenCV3.2.0-v14
- 财阀都市V 全专业街 全职业 全居民 简体中文版
- Android studio创建第一个app