哈夫曼编/译码器(C++)

自定义优先队列,使其按元素的权重(结构体中wt)从小到大进行排列:

ps:关于自定义优先队列的使用方法,可参考文章:priority_queue 的 自定义排序(C++ 简明版)-优快云博客

typedef struct hfm {
	char ch; // 字符
	int idx; // 在HT数组中的索引
	double wt; // 权值
	int prt; // 父节点索引
	int lc, rc; // 左右孩子节点索引
}HFM;
//cmp用于自定义优先队列排列依据
struct cmp {
	bool operator()(const HFM& a, const HFM& b)
	{
		return a.wt > b.wt;
	}
};
priority_queue<HFM, vector<HFM>, cmp>pq;

利用优先队列构造哈夫曼树:

ps:关于二叉树的基本建立方法,可参考文章:二叉树: 构造 & 三种遍历(C++)-优快云博客

void build_hfm(HFM*& HT, int n)//总体上用表HT的结构存储数据和数据间关系
{
	// 构建哈夫曼树
	int m = 2 * n - 1;
	HT = (HFM*)calloc(m + 1, sizeof(HFM));
	HFM cur1, cur2, parent;
	for (int i = 1; i <= n; i++)
	{
		cur1 = pq.top(); pq.pop();
		HT[i].wt = cur1.wt;
		HT[i].idx = i;
		HT[i].ch = cur1.ch;
	}
	for (int i = 1; i <= n; i++)
	{
		pq.push(HT[i]);
	}
	for (int i = n + 1; i <= m; i++)
	{
		cur1 = pq.top(); pq.pop();
		cur2 = pq.top(); pq.pop();

		parent.wt = cur1.wt + cur2.wt;
		parent.idx = i;
		parent.lc = cur1.idx; parent.rc = cur2.idx;
		parent.ch = '/';

		HT[i] = parent;
		HT[cur1.idx].prt = i; HT[cur2.idx].prt = i;
		pq.push(parent);
	}
	HT[m].prt = 0;
}

为了给接下来构造哈夫曼编码做好准备,上面构建哈夫曼树时特别建立了各结点的父节点的信息,便于回溯。

构建哈夫曼编码:

void hfm_code(HFM*& HT, char**& HC, int n)
{
	// 哈夫曼编码
	HC = (char**)malloc(sizeof(char*) * (n + 1));
	char* hp = (char*)malloc(sizeof(char) * n);
	hp[n - 1] = '\0';
	for (int j = 1; j <= n; j++)
	{
		int st = n - 1;
		int c = j;
		int pt = HT[c].prt;
		while (pt)
		{
			if (c == HT[pt].lc) hp[--st] = '0'; // 左孩子为0
			else hp[--st] = '1'; // 右孩子为1
			c = pt;
			pt = HT[c].prt;
		}
		HC[j] = (char*)malloc(sizeof(char) * (n - st));
		strcpy(HC[j], &hp[st]);
	}
	free(hp);
}

完整代码如下,可实现题目要求的功能:

1. 编码         2. 读入文本并转化为编码形式存储与“ code ”中       

3. 将code中编码转译为文本,并与原文本比较

#include<bits/stdc++.h>
using namespace std;

typedef struct hfm {
	char ch; // 字符
	int idx; // 在HT数组中的索引
	double wt; // 权值
	int prt; // 父节点索引
	int lc, rc; // 左右孩子节点索引
}HFM;
//cmp用于自定义优先队列排列依据
struct cmp {
	bool operator()(const HFM& a, const HFM& b)
	{
		return a.wt > b.wt;
	}
};
priority_queue<HFM, vector<HFM>, cmp>pq;

void build_hfm(HFM*& HT, int n)//总体上用表HT的结构存储数据和数据间关系
{
	// 构建哈夫曼树
	int m = 2 * n - 1;
	HT = (HFM*)calloc(m + 1, sizeof(HFM));
	HFM cur1, cur2, parent;
	for (int i = 1; i <= n; i++)
	{
		cur1 = pq.top(); pq.pop();
		HT[i].wt = cur1.wt;
		HT[i].idx = i;
		HT[i].ch = cur1.ch;
	}
	for (int i = 1; i <= n; i++)
	{
		pq.push(HT[i]);
	}
	for (int i = n + 1; i <= m; i++)
	{
		cur1 = pq.top(); pq.pop();
		cur2 = pq.top(); pq.pop();

		parent.wt = cur1.wt + cur2.wt;
		parent.idx = i;
		parent.lc = cur1.idx; parent.rc = cur2.idx;
		parent.ch = '/';

		HT[i] = parent;
		HT[cur1.idx].prt = i; HT[cur2.idx].prt = i;
		pq.push(parent);
	}
	HT[m].prt = 0;
}

void hfm_code(HFM*& HT, char**& HC, int n)
{
	// 哈夫曼编码
	HC = (char**)malloc(sizeof(char*) * (n + 1));
	char* hp = (char*)malloc(sizeof(char) * n);
	hp[n - 1] = '\0';
	for (int j = 1; j <= n; j++)
	{
		int st = n - 1;
		int c = j;
		int pt = HT[c].prt;
		while (pt)
		{
			if (c == HT[pt].lc) hp[--st] = '0'; // 左孩子为0
			else hp[--st] = '1'; // 右孩子为1
			c = pt;
			pt = HT[c].prt;
		}
		HC[j] = (char*)malloc(sizeof(char) * (n - st));
		strcpy(HC[j], &hp[st]);
	}
	free(hp);
}

int main()
{
	// 主函数
	int n; cout << "the number of elements> ";
	cin >> n;
	HFM cur;
	for (int i = 1; i <= n; i++)
	{
		cin >> cur.ch >> cur.wt; // 输入字符和权值
		pq.push(cur);
	}
	HFM* HT;
	build_hfm(HT, n);
	char** HC;
	hfm_code(HT, HC, n); // 哈夫曼编码存储于HC中

	//可以输出编码看看
//	for(int i=1; i<=n; i++)
//	{
//		cout<<HT[i].ch<<" "<<HC[i]<<endl;
//	}

	cout << "the article waiting for stockpiling>";
	string atc;
	cin >> atc;
	int lth = atc.size();
	string code; // 待存文本的编码
	for (int i = 0; i < lth; i++)
	{
		int j;
		for (j = 1; j <= n; j++)
		{
			if (atc[i] == HT[j].ch)
			{
				code.append(HC[j]); break;
			}
		}
		if (j == n + 1) cout << " word not found " << endl;
	}
	cout << "HFM_code>	" << code << endl;

	// 转译文本,存于tr_atc中
	string tr_atc;
	int idx = 2 * n - 1;
	for (int i = 0; ;)
	{
		if (HT[idx].ch != '/')
		{
			tr_atc.push_back(HT[idx].ch); idx = 2 * n - 1; continue;
		}
		char cur = code[i];
		if (cur == '0') idx = HT[idx].lc;
		else idx = HT[idx].rc;
		if (!code[i]) break;
		i++;
	}
	cout << "tranalated article>	" << tr_atc << endl;

	// 匹配性检验
	if (!atc.compare(tr_atc)) cout << "SUCCUSSFULLY TRANSLATED! WOOLHOOL!" << endl;
	else cout << "FAILED! BUGS EXISTED!" << endl;

	// 释放调用内存
	free(HT);
	for (int i = 0; i <= n; i++)
	{
		free(HC[i]);
	}
	free(HC);
}

如果还是有困惑的话,推荐文章拿捏-哈夫曼树构建及编码生成(建议收藏)_构建哈夫曼树,并输出哈夫曼编码-优快云博客

~ 希望对你有帮助 ~

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值