<Sicily>Huffman coding
一、题目描述
In computer science and information theory, a Huffman code is an optimal prefix code algorithm.
In this exercise, please use Huffman coding to encode a given data.
You should output the number of bits, denoted as B(T), to encode the data:
B(T)=∑f(c)dT(c),
where f(c) is the frequency of character c, and dT(c) is be the depth of character c’s leaf in the tree T.
二、输入
The first line is the number of characters n.
The following n lines, each line gives the character c and its frequency f(c).
三、输出
Output a single number B(T).
例如:
输入:
5
0 5
1 4
2 6
3 2
4 3
输出:
45
四、解题思路
1、所有的节点存放在一个数组中
2、对数组按每个节点的权值从小到大进行排序
本文使用快速排序
3、取出权值最小的两个节点,生成新的节点,原来的两个节点分别为新的节点的左右子树,新节点的权值为原来两个节点的权值之和
4、把新的节点按照权值插入到已经有序的数组中
5、重复步骤3、4,直到只剩根节点
五、代码
#include<iostream>
#include<string>
#include <vector>
using namespace std;
//节点结构
struct Node{
char ch;
int weight;
Node *leftChild, *rightChild;
};
//插入排序
void insertSort(Node** nodeArray, int low, int high)
{
for(int i = low + 1; i < high; i++)
{
Node* temp = nodeArray[i];
int j = i - 1;
while(j >= low && nodeArray[j]->weight > temp->weight)
{
nodeArray[j+1] = nodeArray[j];
j--;
}
nodeArray[j+1] = temp;
}
}
//建Hffman树
void createHuffman(Node** nodeArray, int n)
{
insertSort(nodeArray, 0, n); //先对节点按weight排序
int p = 0;
int res = 0;
while(p < n - 1)
{
Node* minNode1 = nodeArray[p]; //找出weight值最小的两个
Node* minNode2 = nodeArray[++p];
Node* newNode = new Node;
newNode->weight = minNode1->weight + minNode2->weight; //合并生成新的节点
res += newNode->weight;
newNode->leftChild = minNode1;
newNode->rightChild = minNode2;
nodeArray[p] = newNode;
insertSort(nodeArray, p, n);
}
cout << res <<endl;
}
int main()
{
int n;
cin >> n;
char ch;
int weight;
Node* nodeArray[n];
for(int i = 0; i < n; i++)
{
Node* node = new Node;
cin >> ch >> weight;
node->ch = ch;
node->weight = weight;
nodeArray[i] = node;
}
createHuffman(nodeArray, n);
return 0;
}
<Sicily>Huffman coding的更多相关文章
- hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏
huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...
- [IR] Huffman Coding
为了保证:Block中,所有的叶子在所有的中间结点的前面.Static: Huffman coding Dynamic: Adaptive Huffman 一些概念 压缩指标 • Compress a ...
- 霍夫曼编码(Huffman Coding)
霍夫曼编码(Huffman Coding)是一种编码方法,霍夫曼编码是可变字长编码(VLC)的一种. 霍夫曼编码使用变长编码表对源符号(如文件中的一个字母)进行编码,其中变长编码表是通过一种评估来源符 ...
- 哈夫曼编码(Huffman coding)的那些事,(编码技术介绍和程序实现)
前言 哈夫曼编码(Huffman coding)是一种可变长的前缀码.哈夫曼编码使用的算法是David A. Huffman还是在MIT的学生时提出的,并且在1952年发表了名为<A Metho ...
- 【CodeForces】700 D. Huffman Coding on Segment 哈夫曼树+莫队+分块
[题目]D. Huffman Coding on Segment [题意]给定n个数字,m次询问区间[l,r]的数字的哈夫曼编码总长.1<=n,m,ai<=10^5. [算法]哈夫曼树+莫 ...
- 哈夫曼编码的理解(Huffman Coding)
哈夫曼编码(Huffman Coding),又称霍夫曼编码,是一种编码方式,可变字长编码(VLC)的一种.Huffman于1952年提出一种编码方法,该方法完全依据字符出现概率来构造异字头的平均长度最 ...
- Huffman coding & Huffman tree
Huffman coding & Huffman tree Huffman coding 哈夫曼编码 / 最优二元前缀码 Huffman tree 哈夫曼树 / 最优二叉树 https://w ...
- jpeg huffman coding table
亮度DC系数的取值范围及序号: 序号(size) 取值范围 0 0 1 - ...
- Huffman Coding
哈夫曼树 霍夫曼编码是一种无前缀编码.解码时不会混淆.其主要应用在数据压缩,加密解密等场合. 1. 由给定结点构造哈夫曼树 (1)先从小到大排序(nlogn) (2)先用最小的两个点构造一个节点,父节 ...
随机推荐
- 对Shell几个冷知识的总结(IFS,数组,替换,分割,查找)
IFS: 对IFS的用处直接进行说明,详细IFS是干什么的...自行谷歌 首先创建一个 "a a",和"a"的文件: 然后我们 ls查看一下: --> l ...
- iOS对象方法和类方法的区别与调用方式
作为一个iOS程序员初学者,会搞不清楚对象方法和类方法的区别 -(void)duixiangfangfa ; +(void)leifangfa; - 代表实例方法,它在类的一个具体实例范围内执行,也就 ...
- 移动端video播放时不弹出页面层
移动端视频在播放时会主动弹出页面,有的浏览器不会.对那些会的浏览器进行处理: 直接加上下面三个属性即可,兼容方面就不说了,微信上是很ok的. <video x5-playsinline=&quo ...
- ibatis annotations 注解方式返回刚插入的自增长主键ID的值--转
原文地址:http://www.blogs8.cn/posts/WWpt35l mybatis提供了注解方式编写sql,省去了配置并编写xml mapper文件的麻烦,今天遇到了获取自增长主键返回值的 ...
- 接口、索引器、Foreach的本质(学习笔记)
接口 什么是接口? 接口代表一种能力,和抽象类类似但比抽象类的抽象程度更高! 接口的定义: public interface IEat//定义一个接口 { void Eat(string food); ...
- hiho 1564 - 简单dfs + 宏的危害!!!
题目链接 H公司有 N 台服务器,编号1~N,组成了一个树形结构.其中中央服务器处于根节点,终端服务器处于叶子节点. 中央服务器会向终端服务器发送消息.一条消息会通过中间节点,到达所有的终端服务器.消 ...
- SLAM概念学习之随机SLAM算法
这一节,在熟悉了Featue maps相关概念之后,我们将开始学习基于EKF的特征图SLAM算法. 1. 机器人,图和增强的状态向量 随机SLAM算法一般存储机器人位姿和图中的地标在单个状态向量中,然 ...
- swift语言点评四-Closure
总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...
- jQuery获取单选框(复选框)选中的状态
jQuery 获取单选框(复选框)选中的状态 <input type="checkbox" name="" id="choose"/& ...
- 详解JavaScript中的原型和继承-转自颜海镜大大
本文将会介绍面向对象,继承,原型等相关知识,涉及的知识点如下: 面向对象与继承 CEOC OLOO 臃肿的对象 原型与原型链 修改原型的方式 面向对象与继承 最近学习了下python,还写了篇博文&l ...