#include<iomanip.h>
#include<iostream.h>
#include<string.h>
#define MAX 99
char cha[MAX];
char hc[MAX-1][MAX];
int s1,s2; //设置全局变量,以便在方法(函数)select中返回两个变量
typedef struct //huffman树存储结构
{
unsigned int weight;//权值
int lchild,rchild,parent;
}huftree;
void select(huftree tree[],int k) //找寻parent为0,权最小的两个节点
{
int i;
for (i=1;i<=k && tree[i].parent!=0 ;i++); s1=i;//初始化s1
for (i=1;i<=k;i++)
if (tree[i].parent==0 && tree[i].weight<tree[s1].weight) s1=i;//把最小值赋给s1
for (i=1; i<=k ; i++)
if (tree[i].parent==0 && i!=s1) break; s2=i;//初始化s2
for (i=1;i<=k;i++)
if ( tree[i].parent==0 && i!=s1 && tree[i].weight<tree[s2].weight) s2=i;//把最小值赋给s2
}
void huffman(huftree tree[],int *w,int n) //生成huffman树
{ int m,i;
if (n<=1) return;
m=2*n-1;
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余4页未读,立即下载