资源简介
任务:按给定的数据建立赫夫曼树
要求:可以建立函数输入二叉树,并输出其赫夫曼树。在上交资料中请写明:存储结构、基本算法(可以使用程序流程图)、输入输出、源程序、测试数据和结果。提供良好的菜单操作界面

代码片段和文件信息
#include
int s1 s2;
const int maxn = 101;
typedef struct{
int weight;
int leftchild rightchild parent;
}huffuman;
void select(huffuman tree[] int k);
void huffumantree(huffuman tree[] int w[] int n);
void huffumancode(huffuman tree[] int n);
int main()
{
int n;
int x;
int w[maxn];
char cha[maxn];
huffuman tree[maxn];
printf (“-----来建立一棵huffumantree吧!!!-----\n“);
while (1) {
printf (“ 1.建立哈夫曼树\n“);
printf (“ 2.输出哈夫曼树\n“);
printf (“ 3.退出\n“);
printf (“请输入选项:“);
scanf (“%d“ &x);
// printf (“x = %d\n“ x);
if (x == 1){
printf (“***开始建树操作***\n“);
printf (“请输入带编码字符个数:“);
scanf (“%d“ &n);
getchar();
printf (“开始输入字符和对应的权值:\n“);
for (int i=1; i<=n; i++) {
printf (“字符 对应的权值:\n“);
scanf (“%c%d“ &cha[i] &w[i]);
getchar();
}
huffumantree(tree w n);
}
if (x == 2) {
printf (“***开始输出操作***\n“);
huffumancode(tree n);
}
if (x == 3) {
return 0;
}
if (x != 1 && x != 2 && x!= 3)
printf (“请输入正确的选项!!!\n“);
}
return 0;
}
void select (huffuman tree[] int k)
{
int i;
for (i=1; i<=k && tree[i].parent != 0; i++);
s1 = i;
for (i=1; i<=k; i++) {
if (tree[i].weight < tree[s1].weight && tree[i].parent ==0)
s1 = i;
}
for (i=1; i<=k; i++)
if (i != s1 && tree[i].parent == 0)
break;
s2 = i;
for (i=1; i<=k; i++) {
if (i != s1 && tree[i].parent == 0)
if (tree[i].weight s2 = i;
}
}
void huffumantree(huffuman tree[] int w[] int n)
{
if (n <= 1)
return;
int i;
int m = 2 * n - 1;
for (i=1; i<=n; i++) {
tree[i].weight = w[i];
tree[i].parent = 0;
tree[i].leftchild = 0;
tree[i].rightchild = 0;
}
for (i=n+1; i<=m; i++) {
tree[i].weight = 0;
tree[i].parent = 0;
tree[i].leftchild = 0;
tree[i].rightchild = 0;
}
for (i=n+1; i<=m; i++) {
select(tree i-1);
tree[s1].parent = i;
tree[s2].parent = i;
tree[i].leftchild = s1;
tree[i].rightchild = s2;
tree[i].weight = tree[s1].weight + tree[s2].weight;
}
}
void huffumancode(huffuman tree[] int n)
{
printf (“***i***weight***parent***leftchild***rightchild***\n“);
for (int i=1; i<=2*n-1; i++) {
printf (“--%2d %6d %6d %9d %10d---\n“ i tree[i].weight
tree[i].parent tree[i].leftchild tree[i].rightchild);
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-08-10 12:40 数据结构huffumantree课程设计\
文件 2506 2018-07-03 10:16 数据结构huffumantree课程设计\huffuman.cpp
文件 239104 2018-08-10 12:39 数据结构huffumantree课程设计\课程设计报告书.doc
- 上一篇:订票信息管理系统
- 下一篇:基于PLC的变频恒压供水的设计
相关资源
- FTP课程设计(服务端+客户端)
- 数据结构年终考题范围和答案 耿国华
- 高频电子线路课程设计报告收音机
- 直流稳压电源的课程设计、安装及调
- EDA课程设计_密码锁
- 数据结构 朱战力 习题解答 数据结构
- 单片机课程设计 篮球计分器
- 数据结构课程设计 6 1 彩票系统
- 端口扫描课程设计详细的报告
- 教学计划编制系统
- 步进电机课程设计(个人设计)
- 校园网络规划与设计课程设计
- 大数(链表、数组)实现
- 编译原理课程设计:词法语法编译器
-
simuli
nk 课程设计 qpsk - 武汉理工大学 单片机课程设计 16*16点
- 数据库VFP课程设计
- 分页系统模拟实验 操作系统 课程设
- 自己写的航空订票系统c 版--数据结构
- 数据结构实验魔王语言
- 模拟段页式虚拟存储管理中地址转换
- 硬件课程设计—流水灯(quartus软件
- 超市收银系统eclipse access大学课程设计
- 航空订票系统_数据结构课程设计
- c 课程设计 职工信息管理系统
- 汇编语言,课程设计,红绿灯
- 机床液压系统课程设计卧式钻床动力
- 多项式求和(数据结构C 版)
- 尚观培训linux董亮老师关于数据结构的
- 课程设计蔬菜大棚自动控制系统,包
评论
共有 条评论