huff_encode.h

来自「数据结构的一个作业」· C头文件 代码 · 共 41 行

H
41
字号
using namespace std;


/***************************************
*                  蔡敏
***************************************/


HaffCode* huff_encode(char_record* w,int n, HaffNode *ht)//由哈夫曼树生成哈夫曼编码
{
	int i,j,child,parent;;
	HaffCode *hc= new HaffCode[n];

	HaffCode cd;
	for(i=0;i<n;i++) {
		cd.start  = n-1;
		cd.weight = ht[i].weight; 
		child     = i;
		parent    = ht[child].parent;
		while(parent != 0) {
			if(ht[parent].lchild == child) {
				cd.bit[cd.start] = 0;
			}
			else {
				cd.bit[cd.start]=1;//右孩子,编码为1 
			}
			cd.start--;
			child  = parent;
			parent = ht[child].parent;
		}
		// cd.bit[start+1..n-1] 存的是编码
		for(j = cd.start+1;j<n;j++)
			hc[i].bit[j] = cd.bit[j];

		hc[i].start  = cd.start;
		hc[i].weight = cd.weight;
		hc[i].ascii_code = ht[i].ascii_code;
	}
	return hc;
}        

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?