求高手帮编写用数据结构实现哈夫曼树的编码和译码的程序(c++)最好能有300行 急用
答案:2 悬赏:10 手机版
解决时间 2021-02-04 07:49
- 提问者网友:我是我
- 2021-02-03 20:42
求高手帮编写用数据结构实现哈夫曼树的编码和译码的程序(c++)最好能有300行 急用
最佳答案
- 五星知识达人网友:上分大魔王
- 2021-02-03 21:23
#include<iostream.h>
#include<string.h>
#define maxsize 100
#define n 5
#define m 2*n-1
int start=0;
typedef struct
{
int weight;
int parent,lchild,rchild;
}htnode;
typedef htnode huffmantree[maxsize]; // 动态分配数组存储赫夫曼树
int min(huffmantree t,int i)// 函数void select()调用
{
int j,flag;
int k=10000; // 取k为不小于可能的值
for(j=0;j<=i;j++)
{
if(t[j].weight<k&&t[j].parent==0)
k=t[j].weight,flag=j;
}
t[flag].parent=-1;
return flag;
}
void select(huffmantree t,int i,int &s1,int &s2)//取出最小权值的节点
{ // s1为最小的两个值中序号小的那个
s1=min(t,i);
s2=min(t,i);
}
void huffmancode(huffmantree ht)
{
int i;
int s1,s2;
int a[n];
cout<<"依次输入"<<n<<"个结点的值:"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)// 0号单元不用
{
ht[i].weight=a[i];
ht[i].parent=0;
ht[i].lchild=0;
ht[i].rchild=0;
}
for(i=n;i<m;i++) // 建赫夫曼树
{
select(ht,i-1,s1,s2);
cout<<s1<<'\t'<<s2<<endl;
ht[s1].parent=ht[s2].parent=i;
cout<<ht[s1].parent<<'\t'<<ht[s2].parent<<endl;
ht[i].lchild=s1;
ht[i].rchild=s2;
cout<<ht[i].lchild<<'\t'<<ht[i].rchild<<endl;
ht[i].weight=ht[s1].weight+ht[s2].weight;
ht[i].parent=0;
}
int hc[n][n];
for(i=0;i<n;i++)
{
for(int j=i,f=ht[j].parent;j<m-1,f!=0;)
{
if(ht[f].lchild==j)
{
hc[i][start]=0;
cout<<hc[i][start]<<endl;
start+=1;
j=f;
f=ht[f].parent;
}
else
{
hc[i][start]=1;
cout<<hc[i][start]<<endl;
start+=1;
j=f;
f=ht[f].parent;
}
}
hc[i][start]=2;
}
}
void main()
{
huffmantree ht;
huffmancode(ht);
}
好久前的作业了,你看看符合你的要求不
#include<string.h>
#define maxsize 100
#define n 5
#define m 2*n-1
int start=0;
typedef struct
{
int weight;
int parent,lchild,rchild;
}htnode;
typedef htnode huffmantree[maxsize]; // 动态分配数组存储赫夫曼树
int min(huffmantree t,int i)// 函数void select()调用
{
int j,flag;
int k=10000; // 取k为不小于可能的值
for(j=0;j<=i;j++)
{
if(t[j].weight<k&&t[j].parent==0)
k=t[j].weight,flag=j;
}
t[flag].parent=-1;
return flag;
}
void select(huffmantree t,int i,int &s1,int &s2)//取出最小权值的节点
{ // s1为最小的两个值中序号小的那个
s1=min(t,i);
s2=min(t,i);
}
void huffmancode(huffmantree ht)
{
int i;
int s1,s2;
int a[n];
cout<<"依次输入"<<n<<"个结点的值:"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)// 0号单元不用
{
ht[i].weight=a[i];
ht[i].parent=0;
ht[i].lchild=0;
ht[i].rchild=0;
}
for(i=n;i<m;i++) // 建赫夫曼树
{
select(ht,i-1,s1,s2);
cout<<s1<<'\t'<<s2<<endl;
ht[s1].parent=ht[s2].parent=i;
cout<<ht[s1].parent<<'\t'<<ht[s2].parent<<endl;
ht[i].lchild=s1;
ht[i].rchild=s2;
cout<<ht[i].lchild<<'\t'<<ht[i].rchild<<endl;
ht[i].weight=ht[s1].weight+ht[s2].weight;
ht[i].parent=0;
}
int hc[n][n];
for(i=0;i<n;i++)
{
for(int j=i,f=ht[j].parent;j<m-1,f!=0;)
{
if(ht[f].lchild==j)
{
hc[i][start]=0;
cout<<hc[i][start]<<endl;
start+=1;
j=f;
f=ht[f].parent;
}
else
{
hc[i][start]=1;
cout<<hc[i][start]<<endl;
start+=1;
j=f;
f=ht[f].parent;
}
}
hc[i][start]=2;
}
}
void main()
{
huffmantree ht;
huffmancode(ht);
}
好久前的作业了,你看看符合你的要求不
全部回答
- 1楼网友:时间的尘埃
- 2021-02-03 22:54
#define int_max 10000
#define encoding_length 1000
#include "stdio.h"
#include "string.h"
#include "malloc.h"
typedef enum{none,left_child,right_child} which;//标记是左孩子还是右孩子
typedef char elemtype;
typedef struct tnode{
elemtype letter;
int weight;
int parent;
which sigh;
char *code;
}htnode,*huffmantree;
int n;
char coding[50];//储存代码
char str[encoding_length];//保存要翻译的句子
void inittreenode(huffmantree &ht){//初始前n个结点,后m-n个结点置空
int i;int w;char c;
int m=2*n-1;
huffmantree p;
ht=(huffmantree)malloc((m)*sizeof(htnode));
printf("input %d database letter and weight",n);
p=ht;
getchar();
for (i=1;i<=n;i++){
scanf("%c%d",&c,&w);
p->code='\0';
p->letter=c;
p->parent=0;
p->sigh=none;
p->weight=w;
p++;
getchar();
}
for (;i<=m;i++,p++){
p->code='\0';
p->letter=' ';
p->parent=0;
p->sigh=none;
p->weight=0;
}
}//inittreenode
void select(huffmantree ht,int end,int *s1,int *s2){//在0~end之间,找出最小和次小的两个结点序号,返回s1,s2
int i;
int min1=int_max;
int min2;
for (i=0;i<=end;i++){//找最小的结点序号
if (ht[i].parent==0&&ht[i].weight*s1=i;
min1=ht[i].weight;
}
}
min2=int_max;
for(i=0;i<=end;i++){//找次小结点的序号
if (ht[i].parent==0&&(*s1!=i)&&min2>ht[i].weight){
*s2=i;
min2=ht[i].weight;
}
}
}
void huffmantreecreat(huffmantree &ht){//建立huffman树
int i;int m=2*n-1;
int s1,s2;
for(i=n;iselect(ht,i-1,&s1,&s2);
ht[s1].parent=i;
ht[s2].parent=i;
ht[s1].sigh=left_child;
ht[s2].sigh=right_child;
ht[i].weight=ht[s1].weight+ht[s2].weight;
}
}
void huffmantreecode(huffmantree ht){//huffman译码
int i;
char *temp;
temp=(char *)malloc(n*sizeof(char));
temp[n-1]='\0';
int p;int s;
for (i=0;ip=i;
s=n-1;
while (ht[p].parent!=0){//从结点回溯,左孩子为0,右孩子为1
if (ht[p].sigh==left_child)
temp[--s]='0';
else if (ht[p].sigh==right_child)
temp[--s]='1';
p=ht[p].parent;
}
ht[i].code=(char *)malloc((n-s)*sizeof(char));//分配结点码长度的内存空间
strcpy(ht[i].code,temp+s);
printf("%s\n",ht[i].code);
}
}
void getcodingsen(char *sencence){//输入要编码的句子
int l;
gets(sencence);
l=strlen(sencence);
sencence[l]='\0';
}
void huffmantreeencoding(char sen[],huffmantree ht){//将句子进行编码
int i=0;int j;
while(sen[i]!='\0'){
for(j=0;jif (ht[j].letter==sen[i]) //字母吻合则用代码取代
{strcat(coding,ht[j].code);
break;
}
}
i++;
if (sen[i]==32) i++;
}
printf("\n%s",coding);
}
void huffmantreedecoding(huffmantree ht,char code[]){//huffman译码过程,将代码翻译为句子
char sen[100];
char temp[50];
char voidstr[]=" ";
int i;int j;
int t=0;int s=0;
for(i=0;itemp[t++]=code[i];
for(j=0;jif (strcmp(ht[j].code,temp)==0){//代码段吻合
sen[s]=ht[j].letter;s++;
strcpy(temp,voidstr);//将temp置空
t=0;
break;
}
}
}
printf("\n%s",sen);
}
void main(){
htnode hnode;
huffmantree huff;
huff=&hnode;
printf("input the letter for coding number\n");
scanf("%d",&n);
inittreenode(huff);
huffmantreecreat(huff);
huffmantreecode(huff);
getcodingsen(str);
huffmantreeencoding(str,huff);
huffmantreedecoding(huff,coding);
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯