实验5 哈夫曼树
来源:互联网 发布:中国外汇储备结构知乎 编辑:程序博客网 时间:2024/06/07 07:07
#include<stdio.h>#include<malloc.h>#include<stdlib.h>#define MAXNUM 60typedef struct{int weight;int parent;int lchild;int rchild;}HTNode,*huffmantree;void Select(huffmantree HT, int g, int &s1, int &s2);//输入叶子的权值,并输出所有的权值void creattree(huffmantree &HF,int n){ int i,m; if(n<=1) return; m=2*n-1; HF=(huffmantree)malloc((m+1)*sizeof(HTNode)); for(i=1;i<=m;++i) { HF[i].parent=0; HF[i].lchild=0; HF[i].rchild=0; } printf("输入叶子权值\n"); for(i=1;i<=n;++i) { scanf("%d",&HF[i].weight); } for(i=n+1;i<=m;++i) { int g,s1,s2; Select(HF,i-1,s1,s2); HF[s1].parent=i; HF[s2].parent=i; HF[i].lchild=s1; HF[i].rchild=s2; HF[i].weight=HF[s1].weight+HF[s2].weight; } printf("输出权值\n"); for(i=1;i<=m;i++) printf("%d ",HF[i].weight); printf("\n");}//Select函数找两个最小值void Select(huffmantree HT, int g, int &s1, int &s2) { int j, k, m, n; for(k=1; k<=g; k++) { if(HT[k].parent==0) { s1=k; break; } } for(j=1; j<=g; j++) { if((HT[j].weight<=HT[k].weight)&&(HT[j].parent==0)) s1=j; } for(m=1; m<=g; m++) { if((HT[m].parent==0)&&(m!=s1)) { s2=m; break; } } for(n=1; n<=g; n++) { if((HT[n].weight<HT[m].weight)&&(HT[n].parent==0)&&(n!=s1)) s2=n; } }int main() { int n; huffmantree HF; printf("输入叶子个数\n"); scanf("%d",&n); creattree(HF,n); }
阅读全文
0 0
- 实验5 哈夫曼树
- 实验5 RIP实验
- 实验5
- 实验5
- 实验5
- 实验5
- 实验5
- 实验5
- 实验5
- 移动终端设计实验 实验5:数据存储实验
- 【Machine Learning实验5】SVM实验
- 实验四 之 5阶幻方实验
- 【Machine Learning实验5】SVM实验
- 实验:C++实验5-项目1
- 实验:C++实验5—数组分离
- 数据压缩原理实验5_实验报告
- 【实验报告】高软实验5
- 算法实验5《算法综合实验》
- Python爬虫学习纪要(八):Requests 库学习笔记3
- codeforces 894B
- 怎么获取到View的位置View.getLocationInWindow()的为0
- Java基础知识梳理--IO
- tensorflow初学代码系列一(基于莫烦视频)
- 实验5 哈夫曼树
- 引用类型(数组)
- 多线程新浪新闻搜索网络爬虫-基于关键字
- Linux学习6:文件打包与压缩
- LeetCode刷题(45)--Remove Duplicates from Sorted List
- Linux下配置jdk
- 第一天
- Codeforces Round #447
- Win10系统强制禁用驱动程序签名