永发信息网

哈夫曼编程问题 会的帮我下 急急急·····

答案:1  悬赏:10  手机版
解决时间 2021-05-09 11:46
  • 提问者网友:趣果有间
  • 2021-05-08 17:04

谁来帮我改下这个编程

要求

请输入第1个权值:8

请输入第2个权值:5

请输入第3个权值:7

请输入第4个权值:12

请输入第5个权值:15

请输入字符ABCDE

得出结果

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define n 5
#define m 2*n-1
typedef struct
{ int weight;
int lchild,rchild,parent;
}HTNode;
typedef HTNode Huffmantree[m];

typedef struct
{ char ch;
char bits[n+1];
}CodeNode;
typedef CodeNode HuffmanCode[n];

void InitHuffmanTree(HuffmanTree t)
{ int i;
for (i=0;i<m;i++)
{ t[i].weight=0;
t[i].lchild=[i].rchild=[i].parent=0;
}
}

void InputWeight(HuffmanTree t)
{int i;
char noslip;
for(i=o;i<n;i++)
{
printf("请输入第%d个权值:".i+1);
scanf("%d",&t[i].weight);
}
noslip=getchar();printf("%c",noslip);
}
void SelectMin(HuffmanTree t,int i,int *p1,int *p2)
{int j,min1,min2;
min1=min2=-1;
for(j=0;j<=i;j++)
if(t[j].parent==0)
{
if(t[j].weight<min1||min2==-1)
{if(min1!=-1)
{
min2=min1; *p2=*p1; }
min1=t[j].weight; *p1=j;
}
else
if(t[j].weight<min2||min2==-1)
{min2=t[j].weight;
*p2=j;

}
}
}
viod CreateHuffmanTree(HuffmanTree t)
{int i,p1,p2;
InitHuffmanTree(t);
Inputweught(t);
for(i=n;i<m;i++)
{selectmin(t,i-1,&p1,&p2);
t[p1].parent=T[p2].parent=i;
t[i].lchild=p1;
t[i].rchild=p2;
t[i].weight=t[p1].weight+t[p2].weight;
}
}

void HuffmanCode(Huffmantree t,HuffmanCode h)
{int c,p,i;
char cd[n+1];
int start;
cd[n]='\0';
printf("请输入字符");
for(i=0;i<n;i++)
{ //printf("%d",i);
h[i].ch=getchar();
start=n;
c=i;
while((p=t[c].parent) !=NULL)
{
ch[--start]=(t[p].lchild==c)?'0';'1';
c=p;
}
strcpy(h[i].bits,&cd[start]);
}
printf("\n");
for(i=0;i<n;i++)
printf("第%d个字符%c的编码为%s\n",i+1,h[i].ch,h[i].bits);
}

void main()
{HuffmanTree t;
HuffmanCode h;
printf("\n请输入5个权值\n");
CreateHuffmanTree(t);
HuffmanCode(t,h);
}

最佳答案
  • 五星知识达人网友:孤独的牧羊人
  • 2021-05-08 18:06

cpp:


#include<iostream.h>
#include<stdlib.h>
typedef int ElemType;
#include"Haffman.h"
void main()
{
int n,i;
BTreeNode *fbt=NULL;
cout<<"input the amount of leaves with weight: ";
cin>>n;
ElemType *a=new ElemType[n];
cout<<"input "<<n<<" integer as weights: ";
for(i=0;i<n;i++)
cin>>a[i];
fbt=CreateHuffman(a,n);
cout<<"printing huffman tree: ";
PrintBTree(fbt);
cout<<endl;
cout<<"weight of huffman tree:";
HuffManCoding(fbt,0);
}
.h


struct BTreeNode{
ElemType data;
BTreeNode* left;
BTreeNode* right;
};
BTreeNode* CreateHuffman(ElemType a[],int n)
{
BTreeNode **b,*q;
b=new BTreeNode*[n];
int i,j;
for(i=0;i<n;i++){
b[i]=new BTreeNode;
b[i]->data=a[i];
b[i]->left=b[i]->right=NULL;
}
for(i=1;i<n;i++){
int k1=-1,k2;
for(j=0;j<n;j++){
if(b[j]!=NULL&&k1==-1){
k1=j;
continue;
}
if(b[j]!=NULL){
k2=j;
break;
}
}
for(j=k2;j<n;j++){
if(b[j]!=NULL){
if(b[j]->data<b[k1]->data){
k2=k1;
k1=j;
}
else if(b[j]->data<b[k2]->data)
k2=j;
}
}
q=new BTreeNode;
q->data=b[k1]->data+b[k2]->data;
q->left=b[k1];
q->right=b[k2];
b[k1]=q;
b[k2]=NULL;
}
delete []b;
return q;
}
void PrintBTree(BTreeNode *BT)
{
if(BT!=NULL){
cout<<BT->data;
if(BT->left!=NULL||BT->right!=NULL){
cout<<'(';
PrintBTree(BT->left);
if(BT->right!=NULL)
cout<<',';
PrintBTree(BT->right);
cout<<')';
}
}
}
void HuffManCoding(BTreeNode *BT, int len)
{
static int a[10];
if(BT!=NULL){
if(BT->left==NULL&&BT->right==NULL){
cout<<"结点权值为"<<BT->data<<"的编码:";
for(int i=0;i<len;i++)
cout<<a[i]<<' ';
cout<<endl;
}
else{
a[len]=0;
HuffManCoding(BT->left,len+1);
a[len]=1;
HuffManCoding(BT->right,len+1);
}
}
}


我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯