赫夫曼树输出赫夫曼编码不知道运行总是中断?
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define M 10
#define N 100
typedef struct{
char data;
int weight;
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char * *HuffmanCode;
void Select(HuffmanTree HT,int j,int &s1,int &s2){
int k;
int min1=100;
int min2=100;
for( k=1;k<=j;k++)
if (HT[k].weight<min1&&HT[k].parent==0)
{
min1=HT[k].weight;
s1=k;
}
for(k=1;k<=j;k++)
if (HT[k].weight<min2&&HT[k].parent==0&&k!=s1)
{
min2=HT[k].weight;
s2=k;
}
}
void Huffman(HuffmanTree &HT,HuffmanCode &HC,char a[M],int w[M],int n){
int i,m,f,s1,s2;
int c,start;
char cd[N];
HuffmanTree p;
if(n<=1)
return;
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for (i=1,p=HT+1;i<=n;++i,++p)
{
HT[i].data=a[i-1];
HT[i].weight=w[i-1];
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
for (;i<=m;++i,++p)
{
HT[i].weight=0;
HT[i].parent=0;
HT[i].lchild=0;
HT[i].rchild=0;
}
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[n-1]='\0';
for (i=1;i<=n;++i)
{
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);
}
void main(){
HuffmanTree Ht;
HuffmanCode Hc;
int n=10;
char a[M]={'a','b','c','d','e','f','g','h','i','j'};
int w[M]={1,2,3,4,5,6,7,8,9,10};
Huffman(Ht,Hc,a,w,n);
for (int i=1;i<=n;i++)
{
printf("%c :",Ht[i].data);
printf("%c",Hc[i]);
printf("\n");
}
}
[解决办法]
重点检查数组越界没有,设置断点,单步调试
[解决办法]
free(cd);
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]
srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
b[(a=rand()%11)]=0;
Sleep(100);
b[(a=rand()%11)]=1;
Sleep(100);
b[(a=rand()%11)]=2;
Sleep(100);
b[(a=rand()%11)]=3;
Sleep(100);
b[(a=rand()%11)]=4;
Sleep(100);
}
return 0;
}