哈弗曼树的实现





#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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值