每日一题之 哈夫曼编码

描述:

给一个字符串,如 abbcccddd 输出各个字符的哈夫曼编码

input:
abbcccdddd

output :
a 0
b 10
c 110
d 111

思路

哈夫曼编码模板题

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e3+5;

struct huffman_node
{
	char c;
	int weight;
	char code[maxn];
	huffman_node* left;
	huffman_node* right;
	huffman_node(char cc, int v):c(cc),weight(v),left(nullptr),right(nullptr){}
	huffman_node(){}

};


struct cmp{
    //weight小的优先
	bool operator()(huffman_node* x,huffman_node* y){  //自定义优先级 重载()
		return x->weight > y->weight; //表示weight小的优先
	}
};

void huffmanTreeCreat(huffman_node* &root, map<char,int>&word)
{

	priority_queue<huffman_node*,vector<huffman_node*>,cmp> treeNode;
	//
	//priority_queue<node,vector<node>,cmp>

	map<char,int>::iterator it;
	for (it = word.begin(); it != word.end(); ++it) {
		huffman_node* node = new huffman_node(it->first,it->second);
		treeNode.push(node);
	}

	//开始从叶节点构建Huffman树

	//cout << treeNode.top()->c << " "<< treeNode.top()->weight<< endl;

	while(!treeNode.empty()) {

		//只有一个根节点
		if (treeNode.size() == 1) {
			auto node1 = treeNode.top();
			treeNode.pop();
			root = node1;
		}
		else {
			auto node1 = treeNode.top();
			treeNode.pop();
			auto node2 = treeNode.top();
			treeNode.pop();
			huffman_node* newNode  = new huffman_node;
			newNode->weight = node1->weight + node2->weight;
			if (node1->weight <= node2->weight) {  //小的权重在左边 大的在有边 左0右1
				newNode->left = node1;
				newNode->right = node2;
			}
			else {
				newNode->left = node2;
				newNode->right = node1;
			}

			treeNode.push(newNode);
		}


	}


}


void getHuffmanCode(huffman_node* &root)  //层次遍历实现
{
	if (root == nullptr) return;

	huffman_node* p = root;
	queue<huffman_node*> q;
	q.push(p);
	while(!q.empty()) {
		p = q.front();
		q.pop();
		if (p->left != nullptr) {
			q.push(p->left);
			strcpy((p->left)->code,p->code);  //拷贝上级的编码
			char* ptr = (p->left)->code;
			while(*ptr != '\0') ++ptr;
			*ptr = '0';
		}

		if (p->right != nullptr) {
			q.push(p->right);
			strcpy((p->right)->code,p->code);
			char* ptr = (p->right)->code;
			while(*ptr != '\0') ++ptr;
			*ptr = '1';
		}
	}

}

void print(huffman_node*  root) 
{
	if (root == nullptr) return;
	print(root->left);
	if (root->left == nullptr && root->right == nullptr) {
		cout << root->c << " " << root->code << endl;
	} 
	print(root->right);
}




void solve(string s) 
{
	map<char,int>word;
	int len = s.length();
	for (int i = 0; i < len; ++i) {
		if (word.find(s[i]) == word.end()) 
			word[s[i]] = 1;
		else
			word[s[i]]++;
	}


	huffman_node* root = nullptr;
	root = new huffman_node;
	huffmanTreeCreat(root,word);
	getHuffmanCode(root);
	print(root);

}

int main()
{
	string s = "abbcccdddd";
	//cin >> s;
	solve(s);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值