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

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

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

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++)-CSDN博客

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);
}

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

~ 希望对你有帮助 ~

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个用C++实现的哈夫曼译码器的示例: ```cpp #include <iostream> #include <queue> #include <unordered_map> using namespace std; // 哈夫曼树节点 struct Node { char data; // 字符 int freq; // 频度 Node* left; Node* right; }; // 比较函数,用于优先队列的排序 struct Compare { bool operator()(Node* a, Node* b) { return a->freq > b->freq; } }; // 创建哈夫曼树 Node* createHuffmanTree(unordered_map<char, int>& freqMap) { priority_queue<Node*, vector<Node*>, Compare> pq; // 将字符和频度转化为节点,并加入优先队列 for (auto& pair : freqMap) { Node* newNode = new Node(); newNode->data = pair.first; newNode->freq = pair.second; newNode->left = nullptr; newNode->right = nullptr; pq.push(newNode); } // 构建哈夫曼树 while (pq.size() > 1) { Node* left = pq.top(); pq.pop(); Node* right = pq.top(); pq.pop(); Node* parent = new Node(); parent->data = '\0'; parent->freq = left->freq + right->freq; parent->left = left; parent->right = right; pq.push(parent); } return pq.top(); } // 生成哈夫曼码 void generateHuffmanCodes(Node* root, string code, unordered_map<char, string>& codes) { if (root == nullptr) { return; } if (root->left == nullptr && root->right == nullptr) { codes[root->data] = code; } generateHuffmanCodes(root->left, code + "0", codes); generateHuffmanCodes(root->right, code + "1", codes); } // 哈夫曼码 string encode(string text, unordered_map<char, string>& codes) { string encodedText = ""; for (char c : text) { encodedText += codes[c]; } return encodedText; } // 哈夫曼译码 string decode(string encodedText, Node* root) { string decodedText = ""; Node* curr = root; for (char c : encodedText) { if (c == '0') { curr = curr->left; } else { curr = curr->right; } if (curr->left == nullptr && curr->right == nullptr) { decodedText += curr->data; curr = root; } } return decodedText; } int main() { // 字符集及频度 unordered_map<char, int> freqMap = { {' ', 44}, {'A', 64}, {'B', 13}, {'C', 42}, {'D', 32}, {'E', 103}, {'F', 21}, {'G', 15}, {'H', 37}, {'I', 57}, {'J', 1}, {'K', 5}, {'L', 52}, {'M', 20}, {'N', 57}, {'O', 77}, {'P', 15}, {'Q', 1}, {'R', 84}, {'S', 51}, {'T', 80}, {'U', 23}, {'V', 8}, {'W', 18}, {'X', 10}, {'Y', 16}, {'Z', 1}, {'.', 1} }; // 创建哈夫曼树 Node* root = createHuffmanTree(freqMap); // 生成哈夫曼码 unordered_map<char, string> codes; generateHuffmanCodes(root, "", codes); // 哈夫曼码示例 string text = "HELLO WORLD"; string encodedText = encode(text, codes); cout << "Encoded Text: " << encodedText << endl; // 哈夫曼译码示例 string decodedText = decode(encodedText, root); cout << "Decoded Text: " << decodedText << endl; return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值