哈弗曼树的实现
来源:互联网 发布:那个软件可以制作菜单 编辑:程序博客网 时间:2024/06/05 01:55
#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);
}
- 哈弗曼树的实现
- 哈弗曼树的java实现
- 哈弗曼树的实现
- 哈弗曼树的实现
- 三子棋的实现的实现的实现
- 数据结构学习之哈弗曼树的实现
- JAVA实现的时钟实现
- 模态框的的实现
- 哈弗曼树实现
- 杀毒软件的简单实现的简单实现
- android的实现电话号码的实现
- 音频的实现音乐声音的实现
- malloc的实现、内存池的实现
- 异形窗体的实现
- 个性化的分页实现
- ASP数据库连接的实现
- Struts单选框的实现
- 梅西迭代算法的实现
- Struts2学习之旅(4)
- 七大查找算法
- 虚拟机软件
- 第7章:项目成本管理习题总结
- sysbench使用及自定义oltp测试lua脚本
- 哈弗曼树的实现
- Markdown 语法
- linux命令screen
- vue spa project build
- 各种排序
- Swift-数组
- BZOJ2002-弹飞绵羊 LCT
- TCP/IP模型的各层的作用
- 范式的理解