求哈夫曼编码时程序运行到一半就终止了,编译无错
这是一个求哈夫曼树和哈夫曼编码的程序:1.count函数统输入字符串的频数2.构建哈夫曼树3.求哈夫曼编码。但是求哈夫曼编码是,那个子函数出问题了,程序运行到那个函数时终止了,求各位大牛指导,代码如下:#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MaxSize 52
struct TreeNode
{
char ch;
int weight;
int parent;
int lchild;
int rchild;
};
typedef struct TreeNode HFTreeNode;
typedef HFTreeNode HuffmanTree;
struct CodeNode
{
char ch;
char bits[6];
};
typedef struct CodeNode CodeNode;
typedef CodeNode HuffmanCode[6];
void Count()
{
char ch;char a[52]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G',
'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int count[52]={0};int i; int j;
for(j=0;j<52;j++)
count[j]=0;
printf("请输入一段英文字符:\n");
ch=getchar();
for(i=0;(i<100)&&(ch!='\n');i++)
{
for(j=0;j<52;j++)
{
if(ch==a[j])
count[j]++;
}
ch=getchar();
}
printf("出现的字符和次数如下:\n");
for(j=0;j<52;j++)
{
if(count[j]!=0)
{
printf("%c",a[j]);
printf(" %d\n",count[j]);
}
}
}
void SelectMin(HuffmanTree *HT,int g,int &s1,int &s2);
void CreateHuffmanTree(HuffmanTree *T,int n)
{
int i,m,w,p1,p2;
char a;
if(n<1)
{
printf("不能构建哈弗曼树!");
exit(1);
}
m=2*n;
for(i=1;i<m;i++)
{
T[i].parent=0;
T[i].lchild=0;
T[i].rchild=0;
T[i].weight=0;
}
printf("请输入叶子节点的权值:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&w);
T[i].weight=w;
printf(" %d\n",w);
}
for(i=n+1;i<=m-1;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;
}
for(i=1;i<=m-1;i++)
{
printf("%d",T[i].weight);
printf(" %d",T[i].parent);
printf(" %d",T[i].lchild);
printf(" %d\n",T[i].rchild);
}
}
void SelectMin(HuffmanTree *HT,int g,int &s1,int &s2)
{
int i,j,k,m,n;
for(k=1;k<=g;k++)
{
if(HT[k].parent==0)
{
s1=k;
break;
}
}
for(j=i;j<=g;j++)
{
if((HT[j].weight<=HT[k].weight)&&(HT[j].parent==0))
s1=j;
}
for(m=1;m<=g;m++)
{
if((HT[m].parent==0)&&(m!=s1))
{
s2=m;
break;
}
}
for(n=1;n<=g;n++)
{
if((HT[n].weight<=HT[m].weight)&&(HT[n].parent==0)&&(n!=s1))
s2=n;
}
}
void CharSetHuffmanEncoding(HuffmanTree *T,HuffmanCode *H)
{
int c,p,i,n;
char a,cd[6];
int start;
cd[5]='\n';
printf("请输入字符的种类数:"); //问题所在处
scanf("%d",&n);
printf("%d",n);
for(i=1;i<=n;i++)
{
printf("请输入对应权值的字符:");
a=getchar();
H[i]->ch='a';
start=5;
c=i;
while(p=T[c].parent>0)
{
if(T[p].lchild==c)
{
cd[--start]='0';
}
else{
cd[--start]='1';
}
c=p;
}
strcpy(H[i]->bits,cd);
}
}
int main()
{
HuffmanTree T[MaxSize];
HuffmanCode H[6];
int leaf_num;
int i,s1,s2;
Count();
printf("请输入叶子数:");
scanf("%d",&leaf_num);
CreateHuffmanTree(T,leaf_num);
CharSetHuffmanEncoding(T,H);
for(i=1;i<=leaf_num;i++)
{
printf("%c",H[i]->ch);
printf("%s",H[i]->bits);
}
return 0;
}