构造哈夫曼树和哈夫曼编码的算法实现

本文介绍了如何统计英文字符频率并利用这些数据构建哈夫曼树和哈夫曼编码。实验内容包括分析一段英文文本,识别不同字符及其出现频率,然后详细阐述了实现这一过程的代码步骤。
摘要由CSDN通过智能技术生成

1、实验内容:

统计下面一段英文的不同字符个数和每个字符的出现频率,利用统计数据构造构造哈夫曼树和哈夫曼编码
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.

2、实现代码:

#include <iostream>
#include<cstring>
#include<string.h>

using namespace std;
struct Tag
{
   
	char ch;  //字符
	int value;  //频率
}T[100];
typedef struct
{
   
	int weight;
	int parent, lchild, rchild;
}HTNode, *HuffmanTree;
typedef char **HuffmanCode;

bool Judge(char temp, int j)//判断是否重复
{
   
	for (int i = 0; i < j; i++)
		if (T[i].ch == temp) return true; //重复
	return false;
}
//统计下面一段英文的不同字符个数和每个字符的出现频率
int GetCount(char *str)//写入每种字符及其出现频率
{
   
	int j = 0;
	for (int i = 0; i < 1000; i++)
	{
   
		char temp= str[i];
		if (!Judge(temp, j))
		{
   
			T[j].ch = temp;
			T[j].value 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值