主题:急求各种大牛帮忙~~小女子不胜感激
数据结构作业 输入n个字母及其权值 建造一棵huffman树 然后输入编码再译码。。。可以运行 但是走到一半就走不下去了。。。大牛们帮帮忙吧~~不胜感激!!鞠躬~
#include<stdio.h>
#include<string.h>
#include<iostream.h>
#include<malloc.h>
typedef struct
{
char ch;
unsigned int weight;
unsigned int parent,lchild,rchild;
}NTNode,*HuffmanTree;
typedef char ** Huffmancode;
void select(HuffmanTree &HT,int l,int *s1,int *s2)
{
HuffmanTree p=&HT[2],q=&HT[1];
for(p,q;q<=&HT[l]&&p<=&HT[l];)
{
if(q->parent==0&&p->parent==0)
{
if(q->weight<=p->weight) p++;
else
{
q=p;
p++;
}
}
else
{
if(q->parent!=0) q++;
if(p->parent!=0) p++;
}
}
HT[*s1]=*q;
for(p=&HT[2],q=&HT[1];p<=&HT[l];)
{
if(q==&HT[*s1]) q++;
if(p==&HT[*s1]) p++;
if(q->parent==0&&p->parent==0)
{
if(q->weight<=p->weight) p++;
else
{
q=p;
p++;
}
}
else
{
if(q->parent!=0) q++;
if(p->parent!=0) p++;
}
}
HT[*s2]=*q;//?
}
int Huffmancoding(HuffmanTree &HT,Huffmancode &HC,int n,int m)
{
int *s1,*s2;
char * cd;
int i;
printf("请以“符号,权值”的格式输入n组符号及其权值.\n");
for(i=1;i<=n;i++)
{
scanf("%c,%d",&HT[i].ch,&HT[i].weight);
}
for(i=n+1;i<=m;++i)
{
select(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;
}
HC=(Huffmancode)malloc((n+1)*sizeof(char*));
cd=(char*)malloc(n*sizeof(char));
if(cd==NULL) return 0;
else{
cd[n-1]='\0';
for(i=1;i<=n;++i)
{
unsigned int start,c,f;
start=n-1;
for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
if(HT[f].lchild==c) cd[--start]='0';
else cd[--start]='1';
HC[i]=(char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
}
free(cd);
return n;
}
int *compare(char *sstr,int *str2,Huffmancode &HC,int n)
{
int n1=-1,k=0,/*j=0,*/i,*m;
char *str1,*p,*q;
p=sstr;
m=str2;
q=&HC[1][0];//HC[1][0]的地址?
for(i=1;i<=n;i++)
{
n1=strlen(HC[i]);
str1=(char*)malloc(30*sizeof(char));
str2=(int*)malloc(30*sizeof(int));
for(q=&HC[i][0],k=0;k<=n1;q++,p++,k++)
{
if(*p==*q);
else
{
p=p-k;
break;
}
}
if(k==n1)
{
*(++str2)=i;
i--;
}
}
return m;
}
void print(int *str,HuffmanTree &HT)
{
int x;
for(str;*str!=0;str++)
{
x=*str;
printf("%c\n",HT[x].ch);
}
}
void main()
{
int n=0,*str2;
char *sstr,*e;
printf("请输入所需符号的个数n.\n");
scanf("%d",&n);
int m;
m=2*n-1;
HuffmanTree HT;
HT=(HuffmanTree)malloc((m+1)*sizeof(NTNode));
Huffmancode HC;
Huffmancoding(HT,HC,n,m);
sstr=(char*)malloc(100*sizeof(char));
printf("请输入编码.\n");
for(e=sstr;;e++)
{
scanf("%c",*e);
if(*e=='\0') break;
}
compare(sstr,str2,HC,n);
print(str2,HT);
}
#include<stdio.h>
#include<string.h>
#include<iostream.h>
#include<malloc.h>
typedef struct
{
char ch;
unsigned int weight;
unsigned int parent,lchild,rchild;
}NTNode,*HuffmanTree;
typedef char ** Huffmancode;
void select(HuffmanTree &HT,int l,int *s1,int *s2)
{
HuffmanTree p=&HT[2],q=&HT[1];
for(p,q;q<=&HT[l]&&p<=&HT[l];)
{
if(q->parent==0&&p->parent==0)
{
if(q->weight<=p->weight) p++;
else
{
q=p;
p++;
}
}
else
{
if(q->parent!=0) q++;
if(p->parent!=0) p++;
}
}
HT[*s1]=*q;
for(p=&HT[2],q=&HT[1];p<=&HT[l];)
{
if(q==&HT[*s1]) q++;
if(p==&HT[*s1]) p++;
if(q->parent==0&&p->parent==0)
{
if(q->weight<=p->weight) p++;
else
{
q=p;
p++;
}
}
else
{
if(q->parent!=0) q++;
if(p->parent!=0) p++;
}
}
HT[*s2]=*q;//?
}
int Huffmancoding(HuffmanTree &HT,Huffmancode &HC,int n,int m)
{
int *s1,*s2;
char * cd;
int i;
printf("请以“符号,权值”的格式输入n组符号及其权值.\n");
for(i=1;i<=n;i++)
{
scanf("%c,%d",&HT[i].ch,&HT[i].weight);
}
for(i=n+1;i<=m;++i)
{
select(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;
}
HC=(Huffmancode)malloc((n+1)*sizeof(char*));
cd=(char*)malloc(n*sizeof(char));
if(cd==NULL) return 0;
else{
cd[n-1]='\0';
for(i=1;i<=n;++i)
{
unsigned int start,c,f;
start=n-1;
for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
if(HT[f].lchild==c) cd[--start]='0';
else cd[--start]='1';
HC[i]=(char*)malloc((n-start)*sizeof(char));
strcpy(HC[i],&cd[start]);
}
}
free(cd);
return n;
}
int *compare(char *sstr,int *str2,Huffmancode &HC,int n)
{
int n1=-1,k=0,/*j=0,*/i,*m;
char *str1,*p,*q;
p=sstr;
m=str2;
q=&HC[1][0];//HC[1][0]的地址?
for(i=1;i<=n;i++)
{
n1=strlen(HC[i]);
str1=(char*)malloc(30*sizeof(char));
str2=(int*)malloc(30*sizeof(int));
for(q=&HC[i][0],k=0;k<=n1;q++,p++,k++)
{
if(*p==*q);
else
{
p=p-k;
break;
}
}
if(k==n1)
{
*(++str2)=i;
i--;
}
}
return m;
}
void print(int *str,HuffmanTree &HT)
{
int x;
for(str;*str!=0;str++)
{
x=*str;
printf("%c\n",HT[x].ch);
}
}
void main()
{
int n=0,*str2;
char *sstr,*e;
printf("请输入所需符号的个数n.\n");
scanf("%d",&n);
int m;
m=2*n-1;
HuffmanTree HT;
HT=(HuffmanTree)malloc((m+1)*sizeof(NTNode));
Huffmancode HC;
Huffmancoding(HT,HC,n,m);
sstr=(char*)malloc(100*sizeof(char));
printf("请输入编码.\n");
for(e=sstr;;e++)
{
scanf("%c",*e);
if(*e=='\0') break;
}
compare(sstr,str2,HC,n);
print(str2,HT);
}