哈夫曼编码(数据结构实验)

前言

哈夫曼编码又称最优树,是一种典型的贪心算法,这种编码方式最大的优点就是用最少的字符包含最多的信息。
哈夫曼编码是一种前缀编码,或者称非前缀编码,这种编码的特点是没有任何字是其他码的前缀。

步骤

1、创建一个优先级队列
当然不一定要用优先级队列,也可以用普通数组代替,相比优先队列,普通数组在程序中要每次都比较节点权重的大小

2、构建哈夫曼树
对于给定的有各自权值的 n 个结点;

  1. 在 n 个权值中选出两个最小的权值,对应的两个结点组成一个新的二叉树,且新二叉树的根结点的权值为左右孩子权值的和;
  2. 在原有的 n 个权值中删除那两个最小的权值,同时将新的权值加入到 n–2 个权值的行列中,以此类推;
  3. 重复 1 和 2 ,直到所以的结点构建成了一棵二叉树为止,这棵树就是哈夫曼树。

3、哈夫曼编码
使用程序求哈夫曼编码有两种方法:

  1. 从叶子结点一直找到根结点,逆向记录途中经过的标记。例如,图 1 中字符 c 的哈夫曼编码从结点 c 开始一直找到根结点,结果为:0 1 1 ,所以字符 c 的哈夫曼编码为:1 1 0(逆序输出)。
  2. 从根结点出发,一直到叶子结点,记录途中经过的标记。例如,求图 1 中字符 c 的哈夫曼编码,就从根结点开始,依次为:1 1 0。

4、代码(可运行)
题目:构造哈夫曼树和哈夫曼编码的算法实现
统计下面一段英文的不同字符个数和每个字符的出现频率,利用统计数据构造构造哈夫曼树和哈夫曼编码
The Chinese official said he viewed the Trump Presidency not as an aberration but as the product of a failing political system. This jibes with other accounts. The Chinese leadership believes that the United States, and Western democracies in general, haven’t risen to the challenge of a globalized economy, which necessitates big changes in production patterns, as well as major upgrades in education and public infrastructure. In Trump and Trumpism, the Chinese see an inevitable backlash to this failure.

#include <iostream>
#include<map>
#include<string>
#include<iomanip>
#include<vector>
using namespace std;
struct Huffmantree {
   
	int weight;
	int parent;
	int lchild;
	int rchild;
};
void select(Huffmantree*& ht, int i, int& s1, int& s2)//找出哈夫曼树表最小的两个数
{
   
	int j = 1;
	int k = 1;
	while (ht[k].parent != 0) k++;//找出双亲为0的标号
	s1 = k;
	for (j = 1; j <= i; j++)
	{
   //找出最小值
		if (ht[j].parent == 0 && ht[j].weight <=</
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
哈夫曼编码是一种用于数据压缩的编码方法,它通过将出现频率较高的字符用较短的编码表示,而将出现频率较低的字符用较长的编码表示,从而实现对数据的压缩。下面是一个演示哈夫曼编码的例子: 假设有一个文本文件SourceFile.txt,其中包含一段文本。首先,需要统计每个字符在文本中出现的频率,并根据频率构建哈夫曼树。然后,根据哈夫曼树生成每个字符的编码表。最后,根据编码表将文本中的字符转换成相应的编码,并将编码结果保存到另一个文件ResultFile.txt中。 以下是一个Python实现的示例代码: ```python import heapq import os class HuffmanNode: def __init__(self, char, freq): self.char = char self.freq = freq self.left = None self.right = None def __lt__(self, other): return self.freq < other.freq def build_frequency_table(text): frequency_table = {} for char in text: if char in frequency_table: frequency_table[char] += 1 else: frequency_table[char] = 1 return frequency_table def build_huffman_tree(frequency_table): heap = [] for char, freq in frequency_table.items(): node = HuffmanNode(char, freq) heapq.heappush(heap, node) while len(heap) > 1: node1 = heapq.heappop(heap) node2 = heapq.heappop(heap) merged_node = HuffmanNode(None, node1.freq + node2.freq) merged_node.left = node1 merged_node.right = node2 heapq.heappush(heap, merged_node) return heap[0] def build_encoding_table(huffman_tree): encoding_table = {} def build_encoding_table_helper(node, code): if node is None: return if node.char is not None: encoding_table[node.char] = code build_encoding_table_helper(node.left, code + "0") build_encoding_table_helper(node.right, code + "1") build_encoding_table_helper(huffman_tree, "") return encoding_table def encode_text(text, encoding_table): encoded_text = "" for char in text: encoded_text += encoding_table[char] return encoded_text def decode_text(encoded_text, huffman_tree): decoded_text = "" current_node = huffman_tree for bit in encoded_text: if bit == "0": current_node = current_node.left else: current_node = current_node.right if current_node.char is not None: decoded_text += current_node.char current_node = huffman_tree return decoded_text # 读取源文件 with open("SourceFile.txt", "r") as file: text = file.read() # 构建频率表 frequency_table = build_frequency_table(text) # 构建哈夫曼树 huffman_tree = build_huffman_tree(frequency_table) # 构建编码表 encoding_table = build_encoding_table(huffman_tree) # 编码文本 encoded_text = encode_text(text, encoding_table) # 将编码结果保存到文件 with open("ResultFile.txt", "w") as file: file.write(encoded_text) # 解码文本 decoded_text = decode_text(encoded_text, huffman_tree) print("Huffman编码完成!") ``` 请注意,上述代码中的SourceFile.txt是输入文件,ResultFile.txt是输出文件。你可以根据实际情况修改这两个文件的路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值