哈夫曼树与哈夫曼代码

一、收获

树中结点常被赋予一个代表某种意义的数值,那个数值称为该结点的权
从树的根到任意结点的路径长度(经过的边数)与该结点上权值的乘积,称为该结点的带。

树中所有叶结点的带权路径长度之和称为该树的带权路径长度,记为WPL。带权路径长度最小的二叉树称为哈夫曼树,也称最优二叉树。

给定n个权值分别为w1, W2, ... wn,的结点,构造哈夫曼树的算法描述如下:


(1)将这n个结点分别作为n棵仅含一个结点的二叉树,构成森林F。
(2)构造一个新结点,从F中选取两棵根结点权值最小的树作为新结点的左、右子树,并且将新结点的权值置为左、右子树上根结点的权值之和。
(3)从F中删除刚才选出的两棵树,同时将新得到的树加入F中。
(4)重复步骤(2)和(3), 直至F中只剩下一棵树为止。

 

注意:

  1. 满二叉树不一定是哈夫曼树
  2. 哈夫曼树中权越大的叶子离根越近
  3. 具有相同带权结点的哈夫曼树不唯一

二、摘抄代码

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

#define MaxSize 1024
#define OK 1
#define ERROR 0
typedef int Status;

typedef struct wordcnt{
	char ch;
	int cnt = 0;
}Count;

typedef struct NumCount{
	Count count[MaxSize];
	int length = 0;
}NumCount;

typedef struct HTree{
	char data;
	int weight;
	int parent,lchild,rchild;
}HTNode,*HuffmanTree;

typedef struct HCode{
	char data;
	char* str;
}HuffmanCode;


Status ReadData(char *source);
Status WordCount(char *data,NumCount *paraCnt);
Status Show(NumCount *paraCnt);
Status CreateHuffmanTree(HuffmanTree &Ht, int length,NumCount cntarray);
Status select(HuffmanTree HT,int top, int *s1,int *s2);
Status CreateHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int length);
Status Encode(char *data,HuffmanCode HC,int length);
Statue Decode(HuffmanTree HT,int length);

int main(int argc, char** argv) {
	char data[MaxSize];
	NumCount Cntarray;
	ReadData(data);
	WordCount(data,&Cntarray);
	// Show(&Cntarray);
	HuffmanTree tree;
	CreateHuffmanTree(tree,Cntarray.length,Cntarray);
	HuffmanCode code;
	CreateHuffmanTree(tree,code,Cntarray.length);
	Encode(data,code,Cntarray.length);
	Decode(tree,Cntarray.length);
	cout<<"Please view the generated TXT file to check the result"<<endl;
	return 0;
}

Status ReadData(char *source)
{
	//打开文件读入数据
	ifstream infile;
	infile.open("in.txt");
	cout<<"Reading..."<<endl;
	cout<<"the input file is:"<<end1;
	infile.getline(sourse,MaxSize);
	cout<<source<<endl;
	infile.close();
	cout<<endl;
	return OK;
}

Status WordCount(char *data,NumCount *paraCnt)
{
	int flag;
	int len = strlen(data);
	for(int i = 0;i < len;++1)
	{
		flag = 0;
		for(int j =0;j < paraCnt->length;++j)
		{
			if(paraCnt->count[j].ch == data[i])
			{
				++paraCnt->count[j].cnt;
				flag = 1;
				break;
			}
			
		}
		if(!flag)
		{
			paraCnt->count[paraCnt->length].ch = data[i];
			++paraCnt->count[paraCnt->length].cnt;
			++paraCnt->length;
		}
	}
	return OK;
}
	
Status Show(NumCount *paraCnt)
{
	cout<<"the length is "<<paraCnt->length<<end1;
	for(int i = 0;i < paraCnt->length;++i)
	{
		cout<<"The character "<<paraCnt->count[i].ch<<" sppears "<<paraCnt->count[i].cnt<<end1;
	}
	cout<<endl;
	return OK;
}

Status CreateHuffmanTree(HuffmanTree &HT,int length,NumCountcntarray
{
	if(length <= 1) return ERROR;
	int s1,s2;
	int m = length*2-1;
	HT = new HTNode[m+1];
	for(int 1 = 1;i <= m;++i)
	{
		HT[i].parent = 0;
		HT[i].lchild = 0;
		HT[i].rchild = 0;
	}
	
	for(int i =1;i <= length;++i)
	{
	HT[i].data = cntarray.count[i-1].ch;
	HT[i].weight = cntarray.count[i-1].ch;	
	}
	
	for(int 1 = length +1;i <=m;++i)
	{
		select(HT,i-1,&s1,&s2);
		HT[s1].parent = i;
		HT[s2].parent = i;
		HT[i].lchild = s1;
		HT[i].lchild = s2;
		HT[i].weight = HT[s1].weight + HT[s2].weight;
	}
	return OK;
	
	
	uss select(HuffmanTree HT,int top,int *si,int *s2)
	
	int min = INT_MAX;
	for(int i = 1;i <= top;++i)
	{
		if(HT[i].weight < min && HT[i].parent == 0)
		{
			min = HT[i].weight;
			*s1 = i;
		}
	 } 
	 min = INT_MAX;
	 for(int 1 = 1;i <= top;++i)
	 {
	 	if(HT[i].weight < min && i != *s1 && HT[i].parent  == 0)
	 	{
	 		min = HT[i].weight;
	 		*s2 = i;
		 }
	 }
}
return OK;


Status CreateHuffmanCode(HuffmanTree HT,HuffmanCode &HC,int length)
{
	HC = new HCode[length+1];
	char *cd = new char[length];
	cd[length-1] = '\0';  
	int c,f,start;
	for(int i = 1;i <= length;++i)
	{
		start = length-1;  
		c = i;
		f = HT[c].parent;
		while(f != 0)
		{
			--start;
			if(HT[f].lchild == c)
			cd[start] = '0';
			else
			cd[start] = '1';
			c =f;
			f = HT[c].parent;
		}
		HC[i].str = new char[length-start];
		HC[i].data = HT[i].data;
		strcpy(HC[i],str,&cd[start]);
}
delete cd;
}

Status Encode(char *data,HuffmanCode HC,int length)
{
	odstream outfile;
	outfile.open("code.txt");
	for(int i = 0;i < strlen(data);++j);
	{
		for(int j = 1;j <= length;++j)
		{
			if(data[i] == HC[j].data)
			{
				outfile<<HC[j].str;
			}
		}
	}
	outfile.close();
	cout<<"the code txt has been written"<<endl;
	cout<<end1;
	return OK;
	}
	
	Status Decode(HuddmanTree HT,int length)
	{
		char codetxt[MaxSize*length];
		ifstream infile;
		infile.open("code.txt");
		infile. getline(codetxt,MaxSize*length);
		infile.clode();
		
		ofstream outfile;
		outfile.open("out.txt");
		
		int root = 2*length-1;
		for(int 1 = 0;i < strlen(codetxt);++i)
		{
			if(codetxt[i] == '0') root = HT[root].lchild;
			else if(codetxt[i] == 'i') root = HT[root].rchild;
			if(HT[root].lchild == 0 && HT[root].rchild == 0)
		{
			outfile<<HT[root].data;
			root = 2*length-1;
		}
		}
		outfile.close();
		cout<<"the output txt has been writtten"<<endl;
		cout<<endl;
		return OK;
	}

 三、运行结果

Reading...
the input file is:


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值