源码:
/**
*哈夫曼树
*@author 菜鸟
*@version 2014.7.16
*/
#include <iostream>
#include <windows.h>
#include <malloc.h>
#define Max 100
#define MaxValue 10000
using namespace std;
//创建节点结构
typedef struct{
int weight;//表示权值
int flag;//用来记录是否已经插入到哈夫曼树中
int parent;//双亲节点的下标
int leftChild;
int rightChild;
}HaffNode;
//创建哈夫曼编码结构
typedef struct{
int bit[Max];//记录的数组
int start;//编码的起始下标
int weight;//权值
}Code;
/**
*哈夫曼树的初始化
*@param int weight[] 用来接收带权值的元素数组
*@param int n 表示要建立n个叶子节点
*@param HaffNode haffTree[]用来接收申请哈夫曼树的地址
*@return 无
*/
void HaffManTreeInitiate(int weight[],int n,HaffNode haffTree[]){
for(int i = 0;i <2*n-1;i++){
if(i < n ){
haffTree[i].weight = weight[i];
}else{
haffTree[i].weight = 0;
}
haffTree[i].parent = -1;
haffTree[i].flag = 0;
haffTree[i].leftChild = -1;
haffTree[i].rightChild = -1;
}
cout<<"初始化完毕!"<<endl;
}
//哈夫曼编码分为两个部分,一就是创建带权值的节点的树结构,一个就是记录其哈夫曼编码的路径即二进制
/**
*哈夫曼树的创建
*@param int weight[] 用来接收带权值的元素数组
*@param int n 表示要建立n个叶子节点
*@param HaffNode haffTree[]用来接收申请哈夫曼树的地址
*@return 无
*/
void HaffManCreate(int weight[],int n,HaffNode haffTree[]){
int m1 = 0,m2 =0;//记录权值
int x1 =0,x2 = 0;
int i = 0, j = 0;
//构造n-1个哈夫曼树的非叶子节点
for( i = 0; i < n-1; i++){
m1 = m2 = MaxValue;//表示权值的最大值
for( j = 0;j < n +i;j++){
//找出两个权值最小的两个
if(haffTree[j].weight <m1 &&haffTree[j].flag == 0){
m2 = m1;
x2 = x1;
m1 = haffTree[j].weight;
x1 = j;
}else if(haffTree[j].weight < m2&&haffTree[j].flag == 0){
m2 = haffTree[j].weight;
x2 = j;
}
}
//将权值最小的两个构成一棵子树
haffTree[x1].parent = n+ i;
haffTree[x2].parent = n +i;
haffTree[x1].flag = 1;
haffTree[x2].flag = 1;
haffTree[n+i].weight = haffTree[x1].weight +haffTree[x2].weight;
haffTree[n +i].leftChild = x1;
haffTree[n+i].rightChild = x2;
}
cout<<"哈夫曼树创建完毕!"<<endl;
/* for(int i = 0;i < 2*n-1;i++){
cout<<"haffTree["<<i<<"].parent:"<<haffTree[i].parent<<" weight"<<haffTree[i].weight<<endl;
}*/
}
/**
*哈夫曼树的编码的创建
*@param HaffNode haffTree[] 用来接收哈夫曼树地址
*@param int n 表示哈夫曼树结构的n个节点
*@param Code haffCode[] 接收哈夫曼编码
*return 无
*/
void HaffManCode(HaffNode haffTree[],int n ,Code haffCode[]){
Code *c = (Code *)malloc(sizeof(Code));
int child= 0,parent = 0;
int i = 0;
//求n个叶节点的哈夫曼编码
for(i = 0;i < n ;i++){
c->start =0;
c->weight = haffTree[i].weight;//获取对应的权值
child = i;
parent = haffTree[child].parent;
while(parent != -1){
if(haffTree[parent].leftChild ==child){
c->bit[c->start] = 0;
}else{
c->bit[c->start] = 1;
}
c->start++;
child = parent;
parent = haffTree[child].parent;
}
for(int j = c->start-1;j >=0 ;j--){
haffCode[i].bit[j] = c->bit[j];
}
haffCode[i].start = c->start;
haffCode[i].weight = c->weight;
}
cout<<"哈夫曼树创建完毕!"<<endl;
}
//输出
void Out_put(Code myHaffCode[],int n){
cout<<"输出:"<<endl;
for(int i = 0;i < n ;i++){
cout<<"weight="<<myHaffCode[i].weight;
cout<<" code=";
for(int j = myHaffCode[i].start;j >0;j--){
cout<<myHaffCode[i].bit[j-1];
}
cout<<endl;
}
cout<<"输出完毕!"<<endl;
}
int main(){
int n = 4;
int weight[4] = {1,3,5,7};
HaffNode *myHaffTree = (HaffNode *)malloc(sizeof(HaffNode)*(2*n-1));
Code *myHaffCode = (Code*)malloc(sizeof(Code)*n);
if(n>Max){
cout<<"给出的n值出了界!"<<endl;
return 0;
}
cout<<"哈夫曼树的初始化!"<<endl;
HaffManTreeInitiate(weight,n,myHaffTree);
cout<<"哈夫曼树的创建:"<<endl;
HaffManCreate(weight,n,myHaffTree);
cout<<"哈夫曼编码的创建:"<<endl;
HaffManCode(myHaffTree,n,myHaffCode);
Out_put(myHaffCode,n);
getchar();
system("PAUSE");
return 0;
}