程序如下:
#include""
#include""
#include""
#include""
int maxline=0;
char xx[50][80];
int l,L;
typedef struct /*定义结构体*/
{ int weight;
int parent;
int lchild,rchild;
}tree;
tree b[57];
int ReadDat(void)
{ FILE *fp;
int i=0;
char *p;
if((fp=fopen("","r"))==NULL)
return 1;
while(fgets(xx[i],80,fp)!=NULL)
{ p=strchr(xx[i],'\n');
if(p)
*p=0;
i++;
}
maxline=i;
fclose(fp);
return 0;
}
int pinlv(int a[])
{int i,j;
int L;
for(i=0;i<maxline;i++)
{L=strlen(xx[i]);
for(j=0;j<L;j++)
if(xx[i][j]>=97&&xx[i][j]<=122)
a[xx[i][j]-97]++;
else
if(xx[i][j]==32)
a[26]++;
else
if(xx[i][j]==44)
a[27]++;
else
if(xx[i][j]==46)
a[28]++;
}
}
smax(int a[],int low,int high,int max[])
{ int mid,M[2],N[2];
mid=(high+low)/2;
if(high-low==1)
{if(a[low]<a[high])
{ max[0]=low;
max[1]=high;
}
else
{ max[0]=high;
max[1]=low;
}
}
else
if(high-low==0)
{max[0]=high;
max[1]=57;
}
else
{ smax(a,low,mid,max);
M[0]=max[0];
M[1]=max[1];
smax(a,mid+1,high,max);
N[0]=max[0];
N[1]=max[1];
if(a[M[0]]<=a[N[0]]&&a[M[1]]<=a[N[0]])
{max[0]=M[0];
max[1]=M[1];
}
else
if(a[M[0]]<=a[N[0]]&&a[M[1]]>a[N[0]])
{max[0]=M[0];
max[1]=N[0];
}
else
if(a[M[0]]>a[N[0]]&&a[M[0]]<=a[N[1]])
{max[0]=N[0];
max[1]=M[0];
}
else
if(a[M[0]]>a[N[0]]&&a[M[0]]>a[N[1]])
{max[0]=N[0];
max[1]=N[1];
}
}
}
bhtree(int a[])
{ int i,j;
哈夫曼树及哈夫曼编码译码的实现 来自淘豆网m.daumloan.com转载请标明出处.