简单的C语言赫夫曼树实现代码
2014-10-05 11:08
411 查看
结果:
#include <stdio.h>
#include <MALLOC.H>
#include <ASSERT.H>
#include <STDLIB.H>
#define NODEMAXSIZE 30
typedef int ElemType;
typedef struct Node{
struct Node *left,*rigth,*parent;
ElemType e;
int weight;
}HfmNode;
struct HFM{
HfmNode *nodeArr[NODEMAXSIZE];
int nodeNum;
};
//获取一个节点
HfmNode* getNode()
{
HfmNode *temp;
temp = (HfmNode *)malloc(sizeof(HfmNode));
while(!temp)
{
temp = (HfmNode *)malloc(sizeof(HfmNode));
}
temp->left=temp->rigth=temp->parent = NULL;
temp->weight = 0 ;
return temp;
}
//增加节点
void addNode(HFM *hfm,ElemType e,int weight)
{
assert(hfm->nodeNum < NODEMAXSIZE);
HfmNode *temp;
temp = getNode();
temp->e = e;
temp->weight = weight;
hfm->nodeArr[hfm->nodeNum] = temp;
hfm->nodeNum++;
}
//初始化
void initHfmTree(HFM *hfm,int *elemArr,int length)
{
assert(hfm != NULL);
int i=0;
hfm->nodeNum = 0;
while (i<length)
{
addNode(hfm,i,elemArr[i++]);
}
}
//建立哈夫曼树
void establishHfmTree(HFM *hfm)
{
assert(hfm != NULL);
HfmNode *lchild,*rchild,*partent;
int untreatedNode;//未处理的结点
int tempWeight;
int i;
untreatedNode = hfm->nodeNum;
while(untreatedNode>1)
{
tempWeight = 100000;
i=0;
lchild = rchild = NULL;
while(i<hfm->nodeNum)
{
if (hfm->nodeArr[i]->parent == NULL && tempWeight >= hfm->nodeArr[i]->weight)
{
lchild = hfm->nodeArr[i];
tempWeight = hfm->nodeArr[i]->weight;
}
++i;
}
tempWeight = 100000;
i=0;
while(i<hfm->nodeNum)
{
if (hfm->nodeArr[i]->parent == NULL &&
tempWeight >= hfm->nodeArr[i]->weight && hfm->nodeArr[i] != lchild)
{
rchild = hfm->nodeArr[i];
tempWeight = hfm->nodeArr[i]->weight;
}
++i;
}
partent = getNode();
partent->e = hfm->nodeNum;
partent->left = lchild;
partent->rigth = rchild;
if(lchild != NULL)
{
lchild->parent = partent;
}
if(rchild != NULL)
{
rchild->parent = partent;
}
partent->weight = lchild->weight + rchild->weight;
hfm->nodeArr[hfm->nodeNum] = partent;
hfm->nodeNum++;
--untreatedNode;
}
}
//打印编码
void printCode(int *encode,int encodeLength)
{
int i=0;
while(i<encodeLength)
{
printf("%d",encode[i++]);
}
printf("\n");
}
//获取编码
void getEncode(HfmNode *hfmNode,int *encode,int encodeLength,
void (*op)(int *encode,int encodeLength))
{
if(hfmNode->left == NULL && hfmNode->rigth == NULL)
{
printf("字母:%d\t 权值:%d \t编码:",hfmNode->e,hfmNode->weight);
op(encode,encodeLength);
return ;
}
if(hfmNode->left != NULL)
{
encode[encodeLength] = 0;
getEncode(hfmNode->left,encode,encodeLength+1,op);
}
if(hfmNode->rigth != NULL)
{
encode[encodeLength] = 1;
getEncode(hfmNode->rigth,encode,encodeLength+1,op);
}
}
//销毁哈夫曼树
void destoryHfmTree(HFM *hfm)
{
int i=0;
while (i<hfm->nodeNum)
{
free(hfm->nodeArr[i++]);
}
}
int main(void)
{
//在此,数组的下标就是要编码的字母,数组的内容就是权值
//如果需要转换成其他字符,只需要对数组下标和字符进行一个映射即可
//如果需要帮助:lwj4616#foxmail.com
ElemType elemArr[]={4,3,96,2,53,22,66,12,43,2};
int length = sizeof(elemArr)/sizeof(elemArr[0]);
int encode[30];
HFM hfm;
printf("\n\n------------赫夫曼树------------\n\n");
initHfmTree(&hfm,elemArr,length);
establishHfmTree(&hfm);
getEncode(hfm.nodeArr[hfm.nodeNum-1],encode,0,&printCode);
destoryHfmTree(&hfm);
printf("\n树已销毁...\n");
return 0;
}
#include <stdio.h>
#include <MALLOC.H>
#include <ASSERT.H>
#include <STDLIB.H>
#define NODEMAXSIZE 30
typedef int ElemType;
typedef struct Node{
struct Node *left,*rigth,*parent;
ElemType e;
int weight;
}HfmNode;
struct HFM{
HfmNode *nodeArr[NODEMAXSIZE];
int nodeNum;
};
//获取一个节点
HfmNode* getNode()
{
HfmNode *temp;
temp = (HfmNode *)malloc(sizeof(HfmNode));
while(!temp)
{
temp = (HfmNode *)malloc(sizeof(HfmNode));
}
temp->left=temp->rigth=temp->parent = NULL;
temp->weight = 0 ;
return temp;
}
//增加节点
void addNode(HFM *hfm,ElemType e,int weight)
{
assert(hfm->nodeNum < NODEMAXSIZE);
HfmNode *temp;
temp = getNode();
temp->e = e;
temp->weight = weight;
hfm->nodeArr[hfm->nodeNum] = temp;
hfm->nodeNum++;
}
//初始化
void initHfmTree(HFM *hfm,int *elemArr,int length)
{
assert(hfm != NULL);
int i=0;
hfm->nodeNum = 0;
while (i<length)
{
addNode(hfm,i,elemArr[i++]);
}
}
//建立哈夫曼树
void establishHfmTree(HFM *hfm)
{
assert(hfm != NULL);
HfmNode *lchild,*rchild,*partent;
int untreatedNode;//未处理的结点
int tempWeight;
int i;
untreatedNode = hfm->nodeNum;
while(untreatedNode>1)
{
tempWeight = 100000;
i=0;
lchild = rchild = NULL;
while(i<hfm->nodeNum)
{
if (hfm->nodeArr[i]->parent == NULL && tempWeight >= hfm->nodeArr[i]->weight)
{
lchild = hfm->nodeArr[i];
tempWeight = hfm->nodeArr[i]->weight;
}
++i;
}
tempWeight = 100000;
i=0;
while(i<hfm->nodeNum)
{
if (hfm->nodeArr[i]->parent == NULL &&
tempWeight >= hfm->nodeArr[i]->weight && hfm->nodeArr[i] != lchild)
{
rchild = hfm->nodeArr[i];
tempWeight = hfm->nodeArr[i]->weight;
}
++i;
}
partent = getNode();
partent->e = hfm->nodeNum;
partent->left = lchild;
partent->rigth = rchild;
if(lchild != NULL)
{
lchild->parent = partent;
}
if(rchild != NULL)
{
rchild->parent = partent;
}
partent->weight = lchild->weight + rchild->weight;
hfm->nodeArr[hfm->nodeNum] = partent;
hfm->nodeNum++;
--untreatedNode;
}
}
//打印编码
void printCode(int *encode,int encodeLength)
{
int i=0;
while(i<encodeLength)
{
printf("%d",encode[i++]);
}
printf("\n");
}
//获取编码
void getEncode(HfmNode *hfmNode,int *encode,int encodeLength,
void (*op)(int *encode,int encodeLength))
{
if(hfmNode->left == NULL && hfmNode->rigth == NULL)
{
printf("字母:%d\t 权值:%d \t编码:",hfmNode->e,hfmNode->weight);
op(encode,encodeLength);
return ;
}
if(hfmNode->left != NULL)
{
encode[encodeLength] = 0;
getEncode(hfmNode->left,encode,encodeLength+1,op);
}
if(hfmNode->rigth != NULL)
{
encode[encodeLength] = 1;
getEncode(hfmNode->rigth,encode,encodeLength+1,op);
}
}
//销毁哈夫曼树
void destoryHfmTree(HFM *hfm)
{
int i=0;
while (i<hfm->nodeNum)
{
free(hfm->nodeArr[i++]);
}
}
int main(void)
{
//在此,数组的下标就是要编码的字母,数组的内容就是权值
//如果需要转换成其他字符,只需要对数组下标和字符进行一个映射即可
//如果需要帮助:lwj4616#foxmail.com
ElemType elemArr[]={4,3,96,2,53,22,66,12,43,2};
int length = sizeof(elemArr)/sizeof(elemArr[0]);
int encode[30];
HFM hfm;
printf("\n\n------------赫夫曼树------------\n\n");
initHfmTree(&hfm,elemArr,length);
establishHfmTree(&hfm);
getEncode(hfm.nodeArr[hfm.nodeNum-1],encode,0,&printCode);
destoryHfmTree(&hfm);
printf("\n树已销毁...\n");
return 0;
}
相关文章推荐
- C语言33行代码简单实现Shell
- 简单的C语言归并排序实现代码
- 数据结构 --静态队列的一个简单的C语言代码实现
- <C语言>如何一步一步根据简单的代码联想到更多的功能?(实现输入一个整数,输出比它小包括它本身的所有素数。)
- 用C语言实现一个简单的计算器代码
- C语言字符串库函数的实现也是笔试题常考的题目,以下代码没有严格测试,只是简单的实现:
- 简单的C语言插入排序实现代码
- 线性结构 -- 连续存储(数组), 1个简单的c语言代码实现.
- 简单的C语言快速排序实现代码
- 线性结构 -- 连续存储(数组), 1个简单的c语言代码实现.
- 简单几行C语言代码实现高斯分布
- 简单易懂的C语言实现双向链表代码
- 简单的C语言二叉树的实现代码
- 简单的C语言栈的实现代码
- 简单的C语言选择排序实现代码
- C语言简单实现sizeof功能代码
- 简单代码实现LCD菜单(C语言的实现方法---加强版 有视频教程)
- 数据结构 - 队列简介 及 1个简单的c语言链式队列代码实现
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- 一个简单用C语言实现的日志函数