哈夫曼树求WPL(C++)

#include<iostream>
using namespace std;
#define maxvalue 32376
#define MAXSIZE 1000
typedef struct
{
	int weight;
	int parent,lchild,rchild;
 }HTNode,*HuffmanTree;
 int Select(HuffmanTree HT,int n,int &s1,int &s2)  //通过这个函数选出最小的两个权值;
 {
 	int min1=maxvalue,min2=maxvalue,i;
 	for(i=1;i<=n;i++)
 	{
 		if((HT[i].parent==0)&&HT[i].weight<min1)  //选出最小的一个权值;
 	{
 		min1=HT[i].weight;
 		s1=i;
	 }
	 if((HT[i].parent==0)&&(HT[i].weight<min2))  //选出第二小的权值;
	 if(i!=s1)
	 {
	 	min2=HT[i].weight;
	 	s2=i;
	 }
	
	 }
	  
 }
 int CreateHuffmanTree(HuffmanTree &HT,int n)
 {
 	int m,i,j,s1,s2,WPL=0;
 	m=2*n-1;
 	HT=new HTNode[MAXSIZE];                       //为结点开辟空间;
 	for(i=1;i<=m;i++)
 	{
 		HT[i].parent=0;HT[i].lchild=0;HT[i].rchild=0; //初始化,使每个结点的父结点和左右孩 
                                                      子下标为0;
	 }
 	for(i=1;i<=n;i++)
 	cin>>HT[i].weight;
 	for(i=n+1;i<=m;i++)                              //通过次循环为第n+1到m个结点赋权值;
 	{
 		Select(HT,i-1,s1,s2);                        //从本结点的前面n个结点选出权值最小的 
                                                       两个结点;
 		HT[s1].parent=i;HT[s2].parent=i;
 		HT[i].lchild=s1;HT[i].rchild=s2;
 		HT[i].weight=HT[s1].weight+HT[s2].weight;
	 }
	 for(i=1;i<=m;i++)
	 cout<<HT[i].weight<<" ";
	 cout<<endl;
	 for(i=n+1;i<=m;i++)                          //哈夫曼树构造完成后,计算WPL;
	 WPL+=HT[i].weight;
	 cout<<WPL;
 }
 int main()
 {
 	HuffmanTree HT;
 	int i,n;
 	cin>>n;
 	CreateHuffmanTree(HT,n);
 	return 0;
 }

已知,w={2,4,8,3,9}.

存储结构HT的原始状态
结点weightparentlchildrchild
12000
24000
38000
43000
59000
6-000
7-000
8-000
9-000
存储结构HT的最终状态
结点weightparentlchildrchild
12600
24700
38800
43600
59700
65814
79925
813936
922078

结点示意图

为什么在代码中WPL的值是

for(i=n+1;i<=m;i++)

WPL+=HT[i].weight  ?

因为从第n+1个结点开始后的结点每一次与后续结点相加,它的子结点的权值也包含在其中,所以求wpl值我们只需要将第n+1到m个结点的权值相加就行。

  • 14
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用C++实现构建哈夫曼树wpl和哈夫曼编码的完整代码: ```c++ #include <iostream> #include <vector> #include <queue> #include <unordered_map> using namespace std; // 定义哈夫曼树的节点结构体 struct huffman_node { char data; // 字符数据 int weight; // 权值 huffman_node* left; // 左孩子 huffman_node* right; // 右孩子 }; // 定义哈夫曼树的构建函数 huffman_node* build_huffman_tree(vector<pair<char, int>>& freq) { // 将字符和对应的权值放入一个优先队列中 auto cmp = [](const huffman_node* a, const huffman_node* b) { return a->weight > b->weight; }; priority_queue<huffman_node*, vector<huffman_node*>, decltype(cmp)> pq(cmp); for (auto p : freq) { huffman_node* node = new huffman_node{ p.first, p.second, nullptr, nullptr }; pq.push(node); } // 不断将队列中的两个节点合并,直到只剩下一个根节点 while (pq.size() > 1) { huffman_node* left = pq.top(); pq.pop(); huffman_node* right = pq.top(); pq.pop(); huffman_node* parent = new huffman_node{ '\0', left->weight + right->weight, left, right }; pq.push(parent); } // 返回根节点 return pq.top(); } // 定义哈夫曼树wpl的函数 int get_wpl(huffman_node* root, int depth = 0) { if (!root->left && !root->right) { // 叶节点 return root->weight * depth; } return get_wpl(root->left, depth + 1) + get_wpl(root->right, depth + 1); } // 定义哈夫曼编码的函数 void get_huffman_code(huffman_node* root, string& code, unordered_map<char, string>& map) { if (!root->left && !root->right) { // 叶节点 map[root->data] = code; return; } if (root->left) { code.push_back('0'); get_huffman_code(root->left, code, map); code.pop_back(); } if (root->right) { code.push_back('1'); get_huffman_code(root->right, code, map); code.pop_back(); } } int main() { // 构造待编码的字符串 string str = "hello world"; // 统计字符出现的频率 unordered_map<char, int> freq_map; for (char c : str) { freq_map[c]++; } // 将字符和对应的频率放入一个vector中 vector<pair<char, int>> freq; for (auto p : freq_map) { freq.push_back(p); } // 构建哈夫曼树wpl和编码 huffman_node* root = build_huffman_tree(freq); int wpl = get_wpl(root); unordered_map<char, string> code_map; string code; get_huffman_code(root, code, code_map); // 输出结果 cout << "wpl: " << wpl << endl; for (auto p : code_map) { cout << p.first << ": " << p.second << endl; } return 0; } ``` 其中,输入的字符串为"hello world",输出的结果如下: ``` wpl: 80 r: 000 d: 0010 h: 0011 w: 0100 e: 0101 o: 011 l: 100 : 101 ``` 可以看到,输出了字符串中每个字符的哈夫曼编码,以及整个哈夫曼树wpl

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值