社区
C语言
帖子详情
在线求助C语言解决二叉树(huffman)的问题
K_zErRo
2003-12-15 10:06:03
huffman树和应用
对一篇报文 abcdaabcdefbcdefabcdabaabcaabbabcbabfdff
(40个字符)
中每个字符进行huffman树编码
1画出报文传输流程示意图
2构选huffman树
3编写huffman树编码
...全文
49
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语言大人??
免费下载:
C语言
难点分析整理.doc
21.
C语言
编程常见
问题
分析 108 22.
C语言
编程易犯毛病集合 112 23.
C语言
缺陷与陷阱(笔记) 119 24.
C语言
防止缓冲区溢出方法 126 25.
C语言
高效编程秘籍 128 26. C运算符优先级口诀 133 27. do/while(0)的妙用 134 ...
C语言
实现
二叉树
的各种遍历及求解深度
C语言
实现
二叉树
的各种遍历及求解深度 一、介绍
二叉树
是一种重要的数据结构,在很多方面都有重要的应用,此文主要记录了
二叉树
的基础知识,包括
二叉树
的建立、前中后序遍历方式、层次遍历方式、求解
二叉树
的深度...
C语言
实现
二叉树
(纯新手向)
这是我这段时间学习
c语言
二叉树
的成果,希望对大家有所帮助,共同进步! 一、
二叉树
的基本概念
二叉树
(binary tree)是指树中节点的度不大于2的有序树,它是一种最简单且最重要的树。
二叉树
的递归定义为:
二叉树
...
深入浅出
C语言
——
C语言
实现
二叉树
二叉树
的遍历时按照那个某种特定的规则,依次对
二叉树
中的节点进行相应的操作,并且每个节点只操作一次。
二叉树
的基本属性例如:节点的总数、叶子节点的个数、节点值的查找、
二叉树
的深度,这里都统一使用递归来处理...
二叉树
的先中后序遍历以及
c语言
实现
二叉树
二叉树
的先中后序遍历以及
c语言
实现
二叉树
目录
二叉树
的先中后序遍历以及
c语言
实现
二叉树
先序遍历中序遍历后序遍历规律分析代码实现(递归)
二叉树
完整代码
二叉树
主要有三种遍历方式 先序遍历(根节点>左节点&...
C语言
69,336
社区成员
243,078
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章