• 全部
• 问答

# 在线求助C语言解决二叉树（huffman）的问题

K_zErRo 2003-12-15 10:06:03
huffman树和应用

(40个字符)

1画出报文传输流程示意图
2构选huffman树
3编写huffman树编码
...全文
15 点赞 收藏 4

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语言

6.3w+

C语言相关问题讨论

2003-12-15 10:06