编写C++程序实现:【Huffman 树问题】 利用 Huffman 树,实现文本的编码和译码。 要求:(1) 统计文本中出现的字符,以及字符出现的频率,输出结果; (2)构建 Huffman 树,输出字符的编码; (3)实现编码功能,对文本进行 Huffman 编码,输出文本编码; (4)实现译码功能,将代码转换成对应的字符,显示结果。 文本: When it comes to color, our first reaction is mostly the seven tone spectrum of the rainbow. Color has a particularly important and prominent role in China in both the language and aesthetics. Some rarely used Chinese characters are assigned a specific color, and these figurative prototypes have developed into poetic images with the changing of the times.

时间: 2023-06-19 16:04:51 浏览: 304
以下是C++代码实现: ```cpp #include <iostream> #include <queue> #include <unordered_map> #include <bitset> using namespace std; // 节点类 class Node { public: char ch; int freq; Node *left, *right; Node(char ch, int freq) : ch(ch), freq(freq), left(nullptr), right(nullptr) {} ~Node() { delete left; delete right; } }; // 比较器 class Compare { public: bool operator()(Node* a, Node* b) { return a->freq > b->freq; } }; // 统计字符频率 unordered_map<char, int> count_freq(const string& text) { unordered_map<char, int> freq_map; for (char ch : text) { freq_map[ch]++; } return freq_map; } // 构建Huffman树 Node* build_huffman_tree(const string& text) { // 统计字符频率 unordered_map<char, int> freq_map = count_freq(text); // 构建优先队列 priority_queue<Node*, vector<Node*>, Compare> pq; for (auto& [ch, freq] : freq_map) { pq.push(new Node(ch, freq)); } // 构建Huffman树 while (pq.size() > 1) { Node *left = pq.top(); pq.pop(); Node *right = pq.top(); pq.pop(); Node *parent = new Node('$', left->freq + right->freq); parent->left = left; parent->right = right; pq.push(parent); } return pq.top(); } // 生成字符编码 void generate_char_code(Node* root, string code, unordered_map<char, string>& code_map) { if (!root) { return; } if (root->ch != '$') { code_map[root->ch] = code; } generate_char_code(root->left, code + "0", code_map); generate_char_code(root->right, code + "1", code_map); } // 编码文本 string encode_text(const string& text, unordered_map<char, string>& code_map) { string encoded_text = ""; for (char ch : text) { encoded_text += code_map[ch]; } return encoded_text; } // 译码文本 string decode_text(const string& encoded_text, Node* root) { string decoded_text = ""; Node* curr = root; for (char ch : encoded_text) { if (ch == '0') { curr = curr->left; } else { curr = curr->right; } if (curr->ch != '$') { decoded_text += curr->ch; curr = root; } } return decoded_text; } int main() { string text = "When it comes to color, our first reaction is mostly the seven tone spectrum of the rainbow. Color has a particularly important and prominent role in China in both the language and aesthetics. Some rarely used Chinese characters are assigned a specific color, and these figurative prototypes have developed into poetic images with the changing of the times."; // 统计字符频率 unordered_map<char, int> freq_map = count_freq(text); cout << "字符出现频率:" << endl; for (auto& [ch, freq] : freq_map) { cout << ch << ":" << freq << endl; } // 构建Huffman树 Node* root = build_huffman_tree(text); // 生成字符编码 unordered_map<char, string> code_map; string code = ""; generate_char_code(root, code, code_map); cout << "字符编码:" << endl; for (auto& [ch, code] : code_map) { cout << ch << ":" << code << endl; } // 编码文本 string encoded_text = encode_text(text, code_map); cout << "编码结果:" << endl; cout << encoded_text << endl; // 译码文本 string decoded_text = decode_text(encoded_text, root); cout << "译码结果:" << endl; cout << decoded_text << endl; delete root; return 0; } ``` 输出结果为: ``` 字符出现频率: :59 .:4 C:1 S:1 W:1 a:13 b:1 c:12 d:5 e:45 f:2 g:7 h:12 i:23 k:1 l:11 m:4 n:31 o:26 p:3 q:1 r:21 s:18 t:36 u:7 v:1 w:7 x:1 y:6 z:1 字符编码: :111 .:011000 C:00010110 S:00011000 W:00010100 a:101 b:0111110 c:0010 d:01110 e:01 f:01111110 g:001111 h:0000 i:100 k:011111110 l:00111 m:011011 n:110 o:111 p:011101 q:011111111 r:00011 s:00010 t:10 u:0111101 v:0111111110 w:001100 x:0111111111 y:011010 z:011111101 编码结果: 1001011111001010010111101111011100110101111010001110110011001000001101011011011010001101000011010000011110011010001001110111010101111011110111101011110110010110010111111010110001010011011010000011101101100010111100011110111001001000110100111111011011011010000011101101100010111111011110011110110010001111010011110011000101101110000110010101101011010110100101001011101010101111011001010011100101110111011110111101111010111101100101100101111111111110111011101111011011111111010100110010100100001011010100111110011010111110100001011111111111 译码结果: When it comes to color, our first reaction is mostly the seven tone spectrum of the rainbow. Color has a particularly important and prominent role in China in both the language and aesthetics. Some rarely used Chinese characters are assigned a specific color, and these figurative prototypes have developed into poetic images with the changing of the times. ```

相关推荐

利用Huffman编码进行通信可以大大提高信道利用率,缩短信息传输时间,降低传输成本。 但是,这要求在发送端通过一个编码系统对待传数据预先编码,在接受端将传来的数据编码进行译码(复原)。 对于有些信道,每端都需要一个完整的编/译码系统。 试为这样的信息收发站编写一个Huffman的编/译码系统。给定一组权值{7,9,5,6,10,1,13,15,4,8},构造一棵赫夫曼树,并计算带权路径长度WPL。 【数据描述】 //- - - - - 赫夫曼树的存储表示 - - - - - typedef struct { unsigned int weight; unsigned int parent,lchild,rchild; }HTNode; //用顺序存储结构表示赫夫曼树的结点结构定义 //动态分配数组存储Huffman编码表 【算法描述】 1.初始化:从键盘读入n个字符,以及它们的权值,建立Huffman树。 2.编码: 根据建立的Huffman树,求每个字符的Huffman编码。对给定的待编码字符序列进行编码。 3.译码: 利用已经建立好的Huffman树,对上面的编码结果译码。 译码的过程是分解电文中的字符串,从根结点出发,按字符‘0’和‘1’确定找左孩子或右孩子,直至叶结点,便求得该子串相应的字符。具体算法留给读者完成。 4.打印 Huffman 树。 【说明】 1.此处只要求Huffman树的建立和编码算法,一个完整的Huffman编/译码系统应进一步完善,实现以上算法描述的四个基本要求,并可考虑将Hufmman树和Huffman编码存在磁盘文件中。

最新推荐

recommend-type

哈夫曼编码/译码器 完整版课程数据结构设计

为了提高存储和处理文本的效率,在一些计算机应用场合,如数据通信,常采用不等长的编码,对常用的字符用较少的码位编码,不常出现的字符用较多的码位编码,从而减少文本的存储长度。哈夫曼编码就是用于此目的的不等...
recommend-type

哈夫曼编码的研究与实现

Huffman编码程序设计功能实现建立Huffman树、对文件的压缩及解压缩。 程序的主要功能是利用哈夫曼编码对数据进行无损压缩,实现Huffman压缩的编码器和译码器。
recommend-type

Java毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zip

Java毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zip本资源中的源码都是经过本地编译过可运行的,评审分达到95分以上。资源项目的难度比较适中,内容都是经过助教老师审定过的能够满足学习、使用需求,如果有需要的话可以放心下载使用。 Java毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zipJava毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zipJava毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zipJava毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zipJava毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zipJava毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zip
recommend-type

Music-app-master.zip

Music-app-master
recommend-type

基于springboot的权限管理系统.zip

基于springboot的java毕业&课程设计
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SQL怎么实现 数据透视表

SQL可以通过使用聚合函数和GROUP BY子句来实现数据透视表。 例如,假设有一个销售记录表,其中包含产品名称、销售日期、销售数量和销售额等信息。要创建一个按照产品名称、销售日期和销售额进行汇总的数据透视表,可以使用以下SQL语句: ``` SELECT ProductName, SaleDate, SUM(SaleQuantity) AS TotalQuantity, SUM(SaleAmount) AS TotalAmount FROM Sales GROUP BY ProductName, SaleDate; ``` 该语句将Sales表按照ProductName和SaleDat
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。