社区
C语言
帖子详情
在线求助C语言解决二叉树(huffman)的问题
K_zErRo
2003-12-15 10:06:03
huffman树和应用
对一篇报文 abcdaabcdefbcdefabcdabaabcaabbabcbabfdff
(40个字符)
中每个字符进行huffman树编码
1画出报文传输流程示意图
2构选huffman树
3编写huffman树编码
...全文
67
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语言
常用算法集
几个不错的
c语言
算法
C语言
实现
二叉树
(纯新手向)
目录 一、
二叉树
的基本概念 二、
二叉树
的初始化 三、释放
二叉树
四、前中后序遍历
二叉树
五、主函数和效果截图 六、拓展时间 这是我这段时间学习
c语言
二叉树
的成果,希望对大家有所帮助,共同进步! 一、
二叉树
的基本概念
二叉树
(binary tree)是指树中节点的度不大于2的有序树,它是一种最简单且最重要的树。
二叉树
的递归定义为:
二叉树
是一棵空树,或者是一棵由一个根节点和两棵互不相交的,分别称作根的左子树和右子树组成的非空树;左子树和右子树又同样都是
二叉树
。定义结构体可以这样设计 代码
C语言
实现
二叉树
的各种遍历及求解深度
C语言
实现
二叉树
的各种遍历及求解深度 一、介绍
二叉树
是一种重要的数据结构,在很多方面都有重要的应用,此文主要记录了
二叉树
的基础知识,包括
二叉树
的建立、前中后序遍历方式、层次遍历方式、求解
二叉树
的深度、求解
二叉树
的节点总数、求解
二叉树
每层的节点数目等。 二、实现思路 主要借助栈和队列方式实现
二叉树
的非递归访问等操作,
二叉树
的建立采用递归方式。层次遍历时,
【数据结构】
C语言
实现
二叉树
【数据结构】第五章——树与
二叉树
详细介绍如何通过
C语言
实现一棵
二叉树
……
深入浅出
C语言
——
C语言
实现
二叉树
二叉树
的遍历时按照那个某种特定的规则,依次对
二叉树
中的节点进行相应的操作,并且每个节点只操作一次。
二叉树
的基本属性例如:节点的总数、叶子节点的个数、节点值的查找、
二叉树
的深度,这里都统一使用递归来处理。双亲节点或父节点:若一个节点含有子节点,则这个节点称为其子节点的父节点;孩子节点或子节点:一个节点含有的子树的根节点称为该节点的子节点;叶节点或终端节点:度为0的节点称为叶节点;子孙:以某节点为根的子树中任一节点都称为该节点的子孙。节点的层次:从根开始定义起,根为第1层,根的子节点为第2层,以此类推。
C语言
70,035
社区成员
243,244
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章