Contest100000617 - 《算法笔记》9.8小节——图算法专题->哈夫曼树

题目链接

A 算法6-12:自底向上的赫夫曼编码

  1. 这个题卡了一下午,头疼。就是构建哈夫曼树,求哈夫曼编码。本题求序列中权值最小的两棵树时貌似不能用优先级队列(这里卡了好一会儿),这个比较麻烦;另外,如果有结点权值相同,最后的编码该如何对应起来呢?这里采用结点内部编号的方式让编码与结点一一对应(这里也想了半天)。这里是自底向上编码,参考代码如下。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int d[110];
struct node {
	int weight;
	int index;
	node* parent = NULL;
	node* lchild = NULL;
	node* rchild = NULL;
	string code;
};
vector <node*> ans;
bool cmp(node* n1, node* n2) { return n1->index < n2->index; }

node* huffman_tree(int n) {
	vector <node*> v;
	for (int i = 0; i < n; i++) {
		node* temp = new node;
		temp->weight = d[i];
		temp->index = i;
		v.push_back(temp);
	}
	for (int i = 1; i < n; i++) {
		int min1 = 0, min2 = 1;
		if (v[0]->weight > v[1]->weight) { min1 = 1, min2 = 0; }
		for (int i = 2; i < v.size(); i++)
			if (v[i]->weight < v[min1]->weight) {
				min2 = min1;
				min1 = i;
			}
			else if (v[i]->weight < v[min2]->weight) {
				min2 = i;
			}
		if (min1 > min2) swap(min1, min2);
		node* t1 = v[min1];	node* t2 = v[min2];
		v.erase(v.begin() + min2); v.erase(v.begin() + min1);
		node* temp = new node;
		temp->weight = t1->weight + t2->weight;
		temp->lchild = t1; t1->parent = temp;
		temp->rchild = t2; t2->parent = temp;
		v.push_back(temp);
	}
	return v[0];
}
void huffman_code(node* root) {
	if (root->lchild == NULL && root->rchild == NULL) {
		string temp_code;
		node* temp = root;
		while (root->parent) {
			if (root == root->parent->lchild) 
				temp_code = "0" + temp_code;
			else if (root == root->parent->rchild) 
				temp_code = "1" + temp_code;
			root = root->parent;
		}
		temp->code = temp_code;
		ans.push_back(temp);
		return;
	}
	else {
		huffman_code(root->lchild);
		huffman_code(root->rchild);
	}
}

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> d[i];
	if (n == 1)
		cout << "0" << endl;
	else {
		node* root = huffman_tree(n);
		huffman_code(root);
		sort(ans.begin(), ans.end(), cmp);
		for (int i = 0; i < n; i++)
			cout << ans[i]->code << endl;
	}
	return 0;
}

B 算法6-13:自顶向下的赫夫曼编码

  1. 求自顶向下编码,结点内部就不需要定义父节点了,注意全局变量 temp_code 的用法。参考代码如下。
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int d[110];
string temp_code;
struct node {
	int weight;
	int index;
	node* lchild = NULL;
	node* rchild = NULL;
	string code;
};
vector <node*> ans;
bool cmp(node* n1, node* n2) { return n1->index < n2->index; }

node* huffman_tree(int n) {
	vector <node*> v;
	for (int i = 0; i < n; i++) {
		node* temp = new node;
		temp->weight = d[i];
		temp->index = i;
		v.push_back(temp);
	}
	for (int i = 1; i < n; i++) {
		int min1 = 0, min2 = 1;
		if (v[0]->weight > v[1]->weight) { min1 = 1, min2 = 0; }
		for (int i = 2; i < v.size(); i++)
			if (v[i]->weight < v[min1]->weight) {
				min2 = min1;
				min1 = i;
			}
			else if (v[i]->weight < v[min2]->weight) {
				min2 = i;
			}
		if (min1 > min2) swap(min1, min2);
		node* t1 = v[min1];	node* t2 = v[min2];
		v.erase(v.begin() + min2); v.erase(v.begin() + min1);
		node* temp = new node;
		temp->weight = t1->weight + t2->weight;
		temp->lchild = t1; temp->rchild = t2;
		v.push_back(temp);
	}
	return v[0];
}
void huffman_code(node* root) {
	if (root->lchild == NULL && root->rchild == NULL) {
		root->code = temp_code;
		ans.push_back(root);
		return;
	}
	else {
		if (root->lchild) {
			temp_code.push_back('0');
			huffman_code(root->lchild);
			temp_code.pop_back();
		}
		if (root->rchild) {
			temp_code.push_back('1');
			huffman_code(root->rchild);
			temp_code.pop_back();
		}
	}
}

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> d[i];
	if (n == 1)
		cout << "0" << endl;
	else {
		node* root = huffman_tree(n);
		huffman_code(root);
		sort(ans.begin(), ans.end(), cmp);
		for (int i = 0; i < n; i++)
			cout << ans[i]->code << endl;
	}
	return 0;
}

C 哈夫曼树

  1. 构建哈夫曼树,计算最小带权路径。最小带权路径为哈夫曼树所有内部节点的权值之和,利用这个结论可以简化代码。在构建哈夫曼树过程中,这里利用优先级队列每次取出权值最小的两棵树。参考代码如下。
#include<iostream>
#include<queue>
const int maxn = 1010;

using namespace std;

int v[maxn];
int total_length;
struct node {
	int weight;
	int path_length;
	node* lchild = NULL;
	node* rchild = NULL;
};
struct cmp { bool operator()(node* n1, node* n2) { return n1->weight > n2->weight; } };

node* huffman_tree(int n) {
	priority_queue <node*, vector<node*>, cmp> pq;
	for (int i = 0; i < n; i++) {
		node* temp = new node;
		temp->weight = v[i];
		pq.push(temp);
	}
	for (int i = 1; i < n; i++) {
		node* t1 = pq.top(); pq.pop();
		node* t2 = pq.top(); pq.pop();
		node* temp = new node;
		temp->weight = t1->weight + t2->weight;
		temp->lchild = t1; temp->rchild = t2;
		pq.push(temp);
	}
	return pq.top();
}
void huffman_length(node* root) {
	if (root == NULL)
		return;
	if (root->lchild || root->rchild)
		total_length += root->weight;
	huffman_length(root->lchild);
	huffman_length(root->rchild);
}

int main() {
	int n;
	while (cin >> n) {
		total_length = 0;
		for (int i = 0; i < n; i++)
			cin >> v[i];
		node* root = huffman_tree(n);
		huffman_length(root);
		cout << total_length << endl;
	}
	return 0;
}

D Haffman编码

  1. 有了前几道题的铺垫,这道题就简单很多了。参考代码如下。
#include<iostream>
#include<string>
#include<queue>
const int maxn = 128;

using namespace std;

char v[maxn];
int w[maxn];
string code[maxn];
string temp_code;
struct node {
	char data;
	int weight;
	node* lchild = NULL;
	node* rchild = NULL;
};
struct cmp {
	bool operator () (node* n1, node* n2) {
		if (n1->weight == n2->weight)
			return n1->data > n2->data;
		else return n1->weight > n2->weight;
	}
};

node* huffman_tree(int n) {
	priority_queue<node*, vector<node*>, cmp> pq;
	for (int i = 0; i < n; i++) {
		node* temp = new node;
		temp->data = v[i];
		temp->weight = w[i];
		pq.push(temp);
	}
	for (int i = 1; i < n; i++) {
		node* t1 = pq.top(); pq.pop();
		node* t2 = pq.top(); pq.pop();
		node* temp = new node;
		temp->weight = t1->weight + t2->weight;
		temp->lchild = t1; temp->rchild = t2;
		pq.push(temp);
	}
	return pq.top();
}
void huffman_code(node* root) {
	if (root->lchild == NULL && root->rchild == NULL) {
		code[root->data] = temp_code;
		return;
	}
	if (root->lchild) {
		temp_code.push_back('0');
		huffman_code(root->lchild);
		temp_code.pop_back();
	}
	if (root->rchild) {
		temp_code.push_back('1');
		huffman_code(root->rchild);
		temp_code.pop_back();
	}
}

int main() {
	int n;
	while (cin >> n) {
		for (int i = 0; i < n; i++)
			cin >> v[i] >> w[i];
		node* tree = huffman_tree(n);
		huffman_code(tree);
		for (int i = 0; i < n; i++)
			cout << v[i] << ':' << code[v[i]] << endl;
	}
	return 0;
}

E 合并果子-NOIP2004TGT2

  1. 之前用堆做过一次了,这里用优先级队列实现一次。参考代码如下。
#include<iostream>
#include<vector>
#include<queue>

using namespace std;

priority_queue< int, vector<int>, greater<int> >pq;

int main() {
	int n, t, sum = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> t;
		pq.push(t);
	}
	for (int i = 1; i < n; i++) {
		int a = pq.top();
		pq.pop();
		int b = pq.top();
		pq.pop();
		sum += a + b;
		pq.push(a + b);
	}
	cout << sum << endl;
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值