求助大侠们哈弗曼树问题(内存不足)
程序代码:
#include<string.h> #include<stdlib.h> #include<stdio.h> #include<iostream.h> #define MAXLEN 100 typedef struct{ char ch; int weight; int lchild; int rchild; int parent; }HTNode; typedef HTNode HFMT[MAXLEN]; typedef char **HfCode; int n; void CreateHT(HTNode ht[],int n) { int i,k,lnode,rnode; int min1,min2; for(i=0;i<2*n-1;i++) { ht[i].parent=-1; ht[i].lchild=-1; ht[i].rchild=-1; ht[i].weight=0; } for(i=0;i<n;i++) { printf("输入结点的第%d个权值:",i+1); scanf("%d",&ht[i].weight); } for(i=n;i<2*n-1;i++) { min1=min2=32767; lnode=rnode=0; for(k=0;k<=i-1;k++) if(ht[k].parent==-1) { if(ht[k].weight<min1) { min2=min1; rnode=lnode; min1=ht[k].weight; lnode=k; } else if(ht[k].weight<min2) { min2=ht[k].weight; rnode=k; } } ht[lnode].parent=i; ht[rnode].parent=i; ht[i].weight=ht[lnode].weight+ht[rnode].weight; ht[i].lchild=lnode; ht[i].rchild=rnode; } } //====================================================================================================== void PrintHFMT(HFMT T) { int i,k=0; for(i=0;i<2*n-1;i++) while(T[i].lchild!=-1) { if(!(k%2)) { printf("\n"); } printf("\t\t(%d%d),(%d%d)",T[i].weight,T[i].lchild,T[i].weight,T[i].rchild); k++; break; } } void printHfCode(HfCode hc) { for(int i=0;i<n;i++) { printf("%s",hc[i]); } } HfCode hfEnCoding(HFMT T) { int start; HfCode hc=new char* [(n+1)*sizeof(char*)]; char* cd=new char [n*sizeof(char)]; cd[n-1]='\0'; int c; int f; for(int i=0;i<n;i++) { start=n-1; for(c=i,f=T[i].parent;f!=-1;c=f,f=T[i].parent) { if(T[f].lchild==c) { cd[--start]='0'; } else { cd[--start]='1'; } } hc[i]=new char[(n-start)*sizeof(char)]; strcpy(hc[i],&cd[start]); printf("\n%c:%s",T[i].ch,hc[i]); } return hc; } void print_HuffmanTree(HFMT HT,int t,int i) { if(HT[t].rchild!=-1) { print_HuffmanTree(HT,HT[t].rchild,i+1); } for(int j=1;j<3*i;j++) { printf(""); } if(HT[t].lchild!=-1||HT[t].rchild!=-1) { printf("%d\n",HT[t].weight); } else { printf("%c(%d)\n",HT[t].ch,HT[t].weight); } if(HT[t].lchild!=-1) { print_HuffmanTree(HT,HT[t].lchild,i+1); } } //============================================================================================================== void Encoder(char *original,char *codeFile,HfCode hc,HFMT HT) { char *str; int i=0; char ch; int k=0; FILE *fin; FILE *fout; if((fin=fopen("C:\\original","r"))!=NULL) { fscanf(fin,"%c",&ch); while(!feof(fin)) { k++; fscanf(fin,"%c",&ch); } } fclose(fin); str=new char[k+1]; k=0; if((fin=fopen("C:\\original","r"))!=NULL) { fscanf(fin,"%c",&ch); while(!feof(fin)) { str[k++]=ch; fscanf(fin,"%c",&ch); } } fclose(fin); str[k]='\0'; printf("要编码的数据是:\n"); printf("%s\n",str); k=0; if((fout=fopen("C:\\codeFile","w"))!=NULL) { while(str[k]!='\0') { for(i=0;i<n;i++) { if(str[k]==HT[i].ch) { fprintf(fout,"%s",hc[i]); break; } } k++; } printf("已编码!且存到文件codeFile.dat中!\n\n"); } fclose(fout); } void Decoder(char *codeFile,char *textFile,HFMT HT) { int i=0,k=0; int j=n*2-1-1; char *bitStr; FILE *fin; FILE *fout; printf("经译码的内容为:\n"); char ch; if((fin=fopen("C:\\codeFile","r"))!=NULL) { fscanf(fin,"%c",&ch); while(!feof(fin)) { k++; fscanf(fin,"%c",ch); } } fclose(fin); bitStr=new char[k+1]; k=0; if((fin=fopen("C:\\codeFile","r"))!=NULL) { fscanf(fin,"%c",&ch); while(!feof(fin)) { bitStr[k++]=ch; fscanf(fin,"%c",&ch); } } fclose(fin); bitStr[k]='\0'; if(HT==NULL) { printf("请先编码!\n"); return; } if((fout=fopen("C:\\textFile","w"))!=NULL) while(bitStr[i]=='\0') { if(bitStr[i]=='0') j=HT[j].lchild; else j=HT[j].rchild; if(HT[j].rchild==-1) { ch=HT[j].ch; fprintf(fout,"%c",ch); j=n*2-1-1; } i++; } fclose(fout); printf("\n译码成功且已存到文件textFile.text\n\n"); } void main() { HFMT HT; int n; printf("输入权值个数:\n"); scanf("%d",&n); CreateHT(HT,n); PrintHFMT(HT); HfCode hc=hfEnCoding(HT); printf("\n哈弗曼树形态为:\n"); print_HuffmanTree(HT,2*n-2,0); Encoder("original.text","codefile.text",hc,HT); Decoder("codeFile.text","textfile.text",HT); printf("\n"); }运行时总报内存不足,百度了半天还是不知道怎么回事。(ps:本人基础较差,请大家见谅~)