社区
C语言
帖子详情
在线求助C语言解决二叉树(huffman)的问题
K_zErRo
2003-12-15 10:06:03
huffman树和应用
对一篇报文 abcdaabcdefbcdefabcdabaabcaabbabcbabfdff
(40个字符)
中每个字符进行huffman树编码
1画出报文传输流程示意图
2构选huffman树
3编写huffman树编码
...全文
95
4
打赏
收藏
在线求助C语言解决二叉树(huffman)的问题
huffman树和应用 对一篇报文 abcdaabcdefbcdefabcdabaabcaabbabcbabfdff (40个字符) 中每个字符进行huffman树编码 1画出报文传输流程示意图 2构选huffman树 3编写huffman树编码
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
ymcymc
2003-12-15
打赏
举报
回复
huffmancoding 、hffmancoding1两种建码方法,以前做的练习,hffmancoding1函数你删除就行了
ymcymc
2003-12-15
打赏
举报
回复
#include "stdio.h"
#include "malloc.h"
#include "string"
#define SIZE 6
using namespace std;
typedef struct{
float weight;
char ch;
int parent,lchild,rchild;
}HTnode,*Huffmantree;
typedef struct{
char ch;
char *bits;
}Codenode,*Huffmancode;
typedef struct {
char ch;
float weight;
}Node,*Huffmanweight;
void selectnode(Huffmantree &ht,int n,int &s1,int &s2)
{
int i;
float min=1;
for(i=0;i<=n;++i)
if(ht[i].parent==0&&ht[i].weight<min)
{
min=ht[i].weight;
s1=i;
}
ht[s1].parent=1;
min=1;
for(i=0;i<=n;++i)
if(ht[i].parent==0&&ht[i].weight<min)
{
min=ht[i].weight;
s2=i;
}
}
void Huffmant(Huffmantree &ht,Huffmanweight w,int n)
{
int m=0,i,s1,s2;
if(n<=1)return;
m=2*n-1;
ht=(Huffmantree)malloc(sizeof(Codenode)*m);
for(i=0;i<n;++i)
{
ht[i].weight=w[i].weight;
ht[i].ch=w[i].ch;
ht[i].parent=0;
ht[i].lchild=0;
ht[i].rchild=0;
}
for(i=n;i<m;++i)
{
ht[i].weight=0;
ht[i].ch='0';
ht[i].parent=0;
ht[i].lchild=0;
ht[i].rchild=0;
}
for(i=n;i<m;++i)
{
selectnode(ht,i-1,s1,s2);
ht[s1].parent=i;
ht[s2].parent=i;
ht[i].lchild=s1;
ht[i].rchild=s2;
ht[i].weight=ht[s1].weight+ht[s2].weight;
}
}
void Huffmancoding(Huffmantree ht,Huffmancode &hc)
{
char cd[SIZE];
int c,f,start,i;
hc=(Huffmancode)malloc(sizeof(Codenode)*SIZE);
cd[SIZE-1]='\0';
for(i=0;i<SIZE;++i)
{
start=SIZE-1;
hc[i].ch=ht[i].ch;
for(c=i,f=ht[c].parent;f!=0;c=f,f=ht[c].parent)
if(ht[f].lchild==c)cd[--start]='0';
else cd[--start]='1';
hc[i].bits=(char*)malloc(sizeof(char)*(SIZE-start));
strcpy(hc[i].bits,&cd[start]);
}
}
void Huffmancoding1(Huffmantree ht,Huffmancode &hc)
{
int p=SIZE*2-1,cdlen=0,i;
char cd[SIZE];
for(i=0;i<p;++i)
ht[i].weight=0;
while(p)
{
if(ht[p].weight==0)
{
ht[p].lchild=1;
if(ht[p].lchild!=0)
{
p=ht[p].lchild;cd[cdlen++]='0';
}
else if(ht[p].rchild==0)
{
cd[cdlen]='\0';
hc[p].bits=(char*)malloc(sizeof(char)*cdlen);
strcpy(hc[p].bits,&cd[0]);
hc[p].ch=ht[p].ch;
}
}
else if(ht[p].weight==1)
{
ht[p].weight=2;
if(ht[p].rchild!=0)
{
p=ht[p].rchild;cd[cdlen++]='1';
}
}
else{
ht[p].weight=0;
p=ht[p].parent;
--cdlen;
}
}
}
void main()
{
int i;
Huffmantree ht;
Huffmanweight w;
Huffmancode hc,hc1;
w=(Huffmanweight)malloc(sizeof(Node)*SIZE);
w[0].ch='a';w[0].weight=0.45;w[1].ch='b';
w[1].weight=0.13;w[2].ch='c';w[2].weight=0.12;
w[3].ch='d';w[3].weight=0.16;w[4].ch='e';
w[4].weight=0.09;w[5].ch='f';w[5].weight=0.05;
Huffmant(ht,w,SIZE);
Huffmancoding(ht,hc);
for(i=0;i<SIZE;++i)
printf("%c:%s",hc[i].ch,hc[i].bits);
Huffmancoding(ht,hc1);
printf("\n");
for(i=0;i<SIZE;++i)
printf("%c:%s",hc1[i].ch,hc1[i].bits);
}
ymcymc
2003-12-15
打赏
举报
回复
a的概率为0.45,b为0.13,c为0.12,d为0.16,e为0.09,f为0.05构选huffman树,你把a\b\c\d\e\f的出现概率算好
huffman编码,VC6.0下通过,TC把#include malloc.h改为#include alloc.h,去掉#include "string"
using namespace std;
#include "stdio.h"
#include "malloc.h"
#include "string"
#define SIZE 6
using namespace std;
typedef struct{
float weight;
char ch;
int parent,lchild,rchild;
}HTnode,*Huffmantree;
typedef struct{
char ch;
char *bits;
}Codenode,*Huffmancode;
typedef struct {
char ch;
float weight;
}Node,*Huffmanweight;
void selectnode(Huffmantree &ht,int n,int &s1,int &s2)
{
int i;
float min=1;
for(i=0;i<=n;++i)
if(ht[i].parent==0&&ht[i].weight<min)
{
min=ht[i].weight;
s1=i;
}
ht[s1].parent=1;
min=1;
for(i=0;i<=n;++i)
if(ht[i].parent==0&&ht[i].weight<min)
{
min=ht[i].weight;
s2=i;
}
}
void Huffmant(Huffmantree &ht,Huffmanweight w,int n)
{
int m=0,i,s1,s2;
if(n<=1)return;
m=2*n-1;
ht=(Huffmantree)malloc(sizeof(Codenode)*m);
for(i=0;i<n;++i)
{
ht[i].weight=w[i].weight;
ht[i].ch=w[i].ch;
ht[i].parent=0;
ht[i].lchild=0;
ht[i].rchild=0;
}
for(i=n;i<m;++i)
{
ht[i].weight=0;
ht[i].ch='0';
ht[i].parent=0;
ht[i].lchild=0;
ht[i].rchild=0;
}
for(i=n;i<m;++i)
{
selectnode(ht,i-1,s1,s2);
ht[s1].parent=i;
ht[s2].parent=i;
ht[i].lchild=s1;
ht[i].rchild=s2;
ht[i].weight=ht[s1].weight+ht[s2].weight;
}
}
void Huffmancoding(Huffmantree ht,Huffmancode &hc)
{
char cd[SIZE];
int c,f,start,i;
hc=(Huffmancode)malloc(sizeof(Codenode)*SIZE);
cd[SIZE-1]='\0';
for(i=0;i<SIZE;++i)
{
start=SIZE-1;
hc[i].ch=ht[i].ch;
for(c=i,f=ht[c].parent;f!=0;c=f,f=ht[c].parent)
if(ht[f].lchild==c)cd[--start]='0';
else cd[--start]='1';
hc[i].bits=(char*)malloc(sizeof(char)*(SIZE-start));
strcpy(hc[i].bits,&cd[start]);
}
}
void Huffmancoding1(Huffmantree ht,Huffmancode &hc)
{
int p=SIZE*2-1,cdlen=0,i;
char cd[SIZE];
for(i=0;i<p;++i)
ht[i].weight=0;
while(p)
{
if(ht[p].weight==0)
{
ht[p].lchild=1;
if(ht[p].lchild!=0)
{
p=ht[p].lchild;cd[cdlen++]='0';
}
else if(ht[p].rchild==0)
{
cd[cdlen]='\0';
hc[p].bits=(char*)malloc(sizeof(char)*cdlen);
strcpy(hc[p].bits,&cd[0]);
hc[p].ch=ht[p].ch;
}
}
else if(ht[p].weight==1)
{
ht[p].weight=2;
if(ht[p].rchild!=0)
{
p=ht[p].rchild;cd[cdlen++]='1';
}
}
else{
ht[p].weight=0;
p=ht[p].parent;
--cdlen;
}
}
}
void main()
{
int i;
Huffmantree ht;
Huffmanweight w;
Huffmancode hc,hc1;
w=(Huffmanweight)malloc(sizeof(Node)*SIZE);
w[0].ch='a';w[0].weight=0.45;w[1].ch='b';
w[1].weight=0.13;w[2].ch='c';w[2].weight=0.12;
w[3].ch='d';w[3].weight=0.16;w[4].ch='e';
w[4].weight=0.09;w[5].ch='f';w[5].weight=0.05;
Huffmant(ht,w,SIZE);
Huffmancoding(ht,hc);
for(i=0;i<SIZE;++i)
printf("%c:%s",hc[i].ch,hc[i].bits);
Huffmancoding(ht,hc1);
printf("\n");
for(i=0;i<SIZE;++i)
printf("%c:%s",hc1[i].ch,hc1[i].bits);
}
K_zErRo
2003-12-15
打赏
举报
回复
up一下。。有没有c语言大人??
Huffman
编码项目教程
本教程介绍
Huffman
编码项目,该编码是无损数据压缩算法,通过构建
Huffman
树实现数据压缩。项目用
C语言
编写,适用于高效数据压缩场景。还介绍了项目快速启动步骤,以及在文件压缩、数据通信、数据库系统等方面的应用,同时列举了相关生态项目。
【离散数学】——期末刷题题库(图论应用题)
该篇文章是关于算法设计与分析、Java、
C语言
、数据库MySQL、数据结构、C++、C51单片机、HTML5网页设计、Python以及人工智能基础知识的综述,还包括了
Huffman
算法和图论
问题
的解答示例。
霍夫曼编码法三进制
c语言
实现,三进制霍夫曼编码
本文探讨了如何将经典的霍夫曼编码推广到三进制编码,详细介绍了编码过程,并通过实例展示了编码步骤。同时,文章提供了伪代码来说明三进制霍夫曼编码的构造,并证明了其最优性。最后,给出了一段
C语言
实现的编码示例代码。
数据结构【
huffman
树】
huffman
编码_考试大纲 | 2020年云南普通专升本数据结构考试大纲...
博客介绍了专升本阶段班,称其更适合学员。还提到可参加预存活动,报班能获得减免,并且点击图片进入小程序可观看免费试听课。
计算机研究生入学考试试题,大学计算机系研究生入学考试试题001
本文档详细解读了浙江大学2000年的计算机科学基础研究生入学考试题目,涵盖算术运算、逻辑表达式、循环控制、递归函数、数值计算、程序设计、数组操作、算法理解、数据结构应用以及
C语言
编程实践,是备考者的宝贵资源。
C语言
70,038
社区成员
243,247
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章