#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define Max 1000
typedef struct
{
int weight;
int parent;
int lchild;
int rchild;
int flag;
}HaffTree;
void Create_HaffTree(HaffTree *pHaffTree,int *weight,int n)
{
int i,j;
int min1,min2;
int x1,x2;
//初始化
for(i=0;i<2*n-1;i++)
{
if(i<n)
{
pHaffTree[i].weight = weight[i];
}
else
{
pHaffTree[i].weight = 0;
}
pHaffTree[i].parent = -1;
pHaffTree[i].lchild = -1;
pHaffTree[i].rchild = -1;
pHaffTree[i].flag = 0;
}
for(i=0;i<n-1;i++)
{
min1 = min2 = Max;
x1 = x2 = -1;
//找最小和次小值
for(j=0;j<n+i;j++)
{
if(pHaffTree[j].flag==0 && pHaffTree[j].weight<min1)
{
min2 = min1;
min1 = pHaffTree[j].weight;
x2 = x1;
x1 = j;
}
else if(pHaffTree[j].flag==0 && pHaffTree[j].weight<min2)
{
min2 = pHaffTree[j].weight;
x2 = j;
}
}
//修改对应数组值
pHaffTree[n+i].weight = min1+min2;
pHaffTree[n+i].lchild = x1;
pHaffTree[n+i].rchild = x2;
pHaffTree[x1].flag = 1;
pHaffTree[x2].flag = 1;
pHaffTree[x1].parent = n+i;
pHaffTree[x2].parent = n+i;
}
}
void HaffCode(HaffTree *pHaffTree,int n)
{
int i,start;
char ch[10] = "";
//申请二位数组,存储字符串
char **code = (char**) malloc(sizeof(char*)*n);
for(i=0;i<n;i++)
{
code[i] = (char*)malloc(sizeof(char)*n);
}
int parent,child;
for(i=0;i<n;i++)
{
parent = pHaffTree[i].parent;
child = i;
start = n;
while(parent!=-1)
{
if(child==pHaffTree[parent].lchild)
{
ch[start--] = '0';
}
else
{
ch[start--] = '1';
}
child = parent;
parent = pHaffTree[parent].parent;
}
strcpy(code[i],ch+start+1);
}
for(i=0;i<n;i++)
{
printf("%s\n",code[i]);
}
}
void main()
{
int weight[] = {7,5,2,4};
int n = sizeof(weight)/sizeof(weight[0]);
HaffTree *pHaffTree = (HaffTree*)malloc(sizeof(HaffTree)*(2*n-1));
Create_HaffTree(pHaffTree,weight,n);
for(int i=0;i<2*n-1;i++)
{
printf("%d ",pHaffTree[i].weight);
}
printf("\n");
HaffCode(pHaffTree,n);
}
#endif
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define Max 1000
typedef struct
{
int weight;
int parent;
int lchild;
int rchild;
int flag;
}HaffTree;
void Create_HaffTree(HaffTree *pHaffTree,int *weight,int n)
{
int i,j;
int min1,min2;
int x1,x2;
//初始化
for(i=0;i<2*n-1;i++)
{
if(i<n)
{
pHaffTree[i].weight = weight[i];
}
else
{
pHaffTree[i].weight = 0;
}
pHaffTree[i].parent = -1;
pHaffTree[i].lchild = -1;
pHaffTree[i].rchild = -1;
pHaffTree[i].flag = 0;
}
for(i=0;i<n-1;i++)
{
min1 = min2 = Max;
x1 = x2 = -1;
//找最小和次小值
for(j=0;j<n+i;j++)
{
if(pHaffTree[j].flag==0 && pHaffTree[j].weight<min1)
{
min2 = min1;
min1 = pHaffTree[j].weight;
x2 = x1;
x1 = j;
}
else if(pHaffTree[j].flag==0 && pHaffTree[j].weight<min2)
{
min2 = pHaffTree[j].weight;
x2 = j;
}
}
//修改对应数组值
pHaffTree[n+i].weight = min1+min2;
pHaffTree[n+i].lchild = x1;
pHaffTree[n+i].rchild = x2;
pHaffTree[x1].flag = 1;
pHaffTree[x2].flag = 1;
pHaffTree[x1].parent = n+i;
pHaffTree[x2].parent = n+i;
}
}
void HaffCode(HaffTree *pHaffTree,int n)
{
int i,start;
char ch[10] = "";
//申请二位数组,存储字符串
char **code = (char**) malloc(sizeof(char*)*n);
for(i=0;i<n;i++)
{
code[i] = (char*)malloc(sizeof(char)*n);
}
int parent,child;
for(i=0;i<n;i++)
{
parent = pHaffTree[i].parent;
child = i;
start = n;
while(parent!=-1)
{
if(child==pHaffTree[parent].lchild)
{
ch[start--] = '0';
}
else
{
ch[start--] = '1';
}
child = parent;
parent = pHaffTree[parent].parent;
}
strcpy(code[i],ch+start+1);
}
for(i=0;i<n;i++)
{
printf("%s\n",code[i]);
}
}
void main()
{
int weight[] = {7,5,2,4};
int n = sizeof(weight)/sizeof(weight[0]);
HaffTree *pHaffTree = (HaffTree*)malloc(sizeof(HaffTree)*(2*n-1));
Create_HaffTree(pHaffTree,weight,n);
for(int i=0;i<2*n-1;i++)
{
printf("%d ",pHaffTree[i].weight);
}
printf("\n");
HaffCode(pHaffTree,n);
}