上次写的哈夫曼,这星期需要写哈夫曼的课程设计,就将之前写的哈夫曼树先优化了一下。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int n;
int m;
char h[100];//输入的字符串
char whole_str[100];
struct tree//树的结构
{
char a;
int weight;
int parent;
int Lchild;
int Rchild;
}ht[];
struct coder//叶子编码
{
char s;
int data;
char code[10];
}hc[];
void select(struct tree ht[],int k,int *s1,int *s2)
{
int i;
for(i=1; i<=k ; ++i)
{
if(ht[i].parent == 0)
break;
}
*s1 = i;
for(i=1; i<=k; ++i)
{
if(ht[i].parent==0 && ht[i].weight<ht[*s1].weight)
*s1 = i;
}
for(i=1; i<=k; ++i)
{
if(ht[i].parent==0 && i!=*s1)
break;
}
*s2 = i;