【数据结构_树_Haffman_0986】Huffman Code



#include <stdio.h> 
#include <stdlib.h>
#include <iostream> 
using namespace std; 
const int maxvalue=100; 
const int maxbit=100; 
const int maxn=100; 
struct haffnode 
{ 
	char ch; //节点值
	int weight; //权重
	int flag; //确定当前点有没有放入二叉树而使用的标记;
	int parent; //双亲结点;
	int leftchild; //左孩子
	int rightchild; //右孩子
}; 
struct code 
{ 
	int bit[maxn]; 
	int start; 
	int weight; 
	char ch; //存放结点值的霍夫曼编码
}; 

void haffman(int weight[],char text[],int n,haffnode hafftree[]) //hafftree属于还在建立的霍夫曼树;
{ 
	int j,m1,m2,x1,x2,i; 
	
	for(i=0;i< 2*n-1;i++) //对霍夫曼树进行传值,初始化;
	{ 
		if(i < n) //只有n个结点;
		{ 
			hafftree[i].weight=weight[i]; //weight[],text[]在main函数中有定义;
			hafftree[i].ch=text[i]; 
		} 
		else 
		{ 
			hafftree[i].weight=0;//没有定义的结点赋值为0; 
			hafftree[i].ch='#'; //没有对应的字符就置为'#'
		} 
		hafftree[i].parent=0; 
		hafftree[i].flag=0; 
		hafftree[i].leftchild=-1; 
		hafftree[i].rightchild=-1; 
		
	} 
	for(i=0;i<n-1;i++) 
	{ 
		m1=m2=maxvalue; 
		x1=x2=0; //x1,x2相当于左右结点;
		for(j=0;j<n+i;j++) 
		{ 
			if(hafftree[j].weight< m1 && hafftree[j].flag==0)//用flag来判断当前节点有没有放进霍夫曼树; 
			{ 
				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; //最顶层结点,2n-1;
		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; 
	} 
	
} 


void haffmancode(haffnode hafftree[],int n,code haffcode[]) 
{ 
	code cd; 
	int i,j; 
	int child,parent; 
	for(i=0;i<n;i++) 
	{ 
		cd.start=n-1; 
		cd.weight=hafftree[i].weight; 
		cd.ch=hafftree[i].ch; 
		child=i; 
		parent=hafftree[child].parent; 
		while(parent!=0) 
		{ 
			if(hafftree[parent].leftchild==child) 
				cd.bit[cd.start]=0; 
			else 
				cd.bit[cd.start]=1; 
			cd.start--; 
			child=parent; 
			parent=hafftree[child].parent; 
		} 
		for(j=cd.start+1;j< n;j++) 
			haffcode[i].bit[j]=cd.bit[j]; 
		haffcode[i].start=cd.start; 
		haffcode[i].weight=cd.weight; 
		haffcode[i].ch=cd.ch; 
	} 	
} 

void ccode(haffnode hafftree[],int n) 
{ 
	char a[1005];
	cin>>a;
	int i,j=0;
	int m=2*n-2;
	while(a[j]!='\0')
	{
		i=m;
		while(hafftree[i].leftchild!=-1 && hafftree[i].rightchild!=-1)
		{
			if(a[j]=='0')
			{
				i=hafftree[i].leftchild;
			}
			else if(a[j]=='1')
			{
				i=hafftree[i].rightchild;
			}
			j++;
		}
		cout<<hafftree[i].ch;
	}
} 

int main( ) 
{ 
	int n=8; 
	int weight[]={5,29,7,8,14,23,3,11}; 
	char text[]={'a','b','c','d','e','f','g','h'}; 
	haffnode myhafftree[maxvalue]; 
	code myhaffcode[maxvalue]; 
	haffman(weight,text,n,myhafftree); 
	haffmancode(myhafftree,n,myhaffcode); 
	ccode(myhafftree,n); 
	return 0; 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值