一、题目描述

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的更多相关文章

  1. 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 ...

  2. [IR] Huffman Coding

    为了保证:Block中,所有的叶子在所有的中间结点的前面.Static: Huffman coding Dynamic: Adaptive Huffman 一些概念 压缩指标 • Compress a ...

  3. 霍夫曼编码(Huffman Coding)

    霍夫曼编码(Huffman Coding)是一种编码方法,霍夫曼编码是可变字长编码(VLC)的一种. 霍夫曼编码使用变长编码表对源符号(如文件中的一个字母)进行编码,其中变长编码表是通过一种评估来源符 ...

  4. 哈夫曼编码(Huffman coding)的那些事,(编码技术介绍和程序实现)

    前言 哈夫曼编码(Huffman coding)是一种可变长的前缀码.哈夫曼编码使用的算法是David A. Huffman还是在MIT的学生时提出的,并且在1952年发表了名为<A Metho ...

  5. 【CodeForces】700 D. Huffman Coding on Segment 哈夫曼树+莫队+分块

    [题目]D. Huffman Coding on Segment [题意]给定n个数字,m次询问区间[l,r]的数字的哈夫曼编码总长.1<=n,m,ai<=10^5. [算法]哈夫曼树+莫 ...

  6. 哈夫曼编码的理解(Huffman Coding)

    哈夫曼编码(Huffman Coding),又称霍夫曼编码,是一种编码方式,可变字长编码(VLC)的一种.Huffman于1952年提出一种编码方法,该方法完全依据字符出现概率来构造异字头的平均长度最 ...

  7. Huffman coding & Huffman tree

    Huffman coding & Huffman tree Huffman coding 哈夫曼编码 / 最优二元前缀码 Huffman tree 哈夫曼树 / 最优二叉树 https://w ...

  8. jpeg huffman coding table

    亮度DC系数的取值范围及序号:                                                               序号(size) 取值范围 0 0  1 - ...

  9. Huffman Coding

    哈夫曼树 霍夫曼编码是一种无前缀编码.解码时不会混淆.其主要应用在数据压缩,加密解密等场合. 1. 由给定结点构造哈夫曼树 (1)先从小到大排序(nlogn) (2)先用最小的两个点构造一个节点,父节 ...

随机推荐

  1. war包放入tomcat

    1.找到tomcat的安装路径(如:D:\example\download\set\apache-tomcat-7.0.23) 2.D:\example\download\set\apache-tom ...

  2. JAVA设计模式之【模板方法模式】

    模板方法模式 提高代码的复用性 把常用的基本方法放入父类中 强调一种流程步骤 角色 抽象类 抽象方法 具体方法 钩子方法 空方法 通过bool控制 具体类 看例子 1.银行模板类 package Te ...

  3. BZOJ 2096 单调队列

    思路: 偷懒用的STL //By SiriusRen #include <deque> #include <cstdio> using namespace std; struc ...

  4. 深入C#类的方法

    构造函数 example1: static void Main(string [] args) { SE engineer=new SE(); engineer.Age=; enginner.Name ...

  5. Android APP 调试过程中遇到的问题。

    调试过过程中APP安装完启动后有的时候会异常退出,报这个错误.有的时候可以直接启动.查找不到原因.网上说把commit方法替换成commitAllowingStateLoss() 也无效. Andro ...

  6. img图片在ie上有有空隙

    图片在ie下会有空隙 首先在全局样式中设置img标签的边距为0 img { border:0;} 一般有两个方法1,img{float:left}2,img{display:block}

  7. Volatile variables

    Volatile variables apply another type of memory constraint to individual variables. The compiler oft ...

  8. [洛谷P3939]数颜色

    题目大意:有n个物品,每个物品有一个颜色.现在有两种操作:1.查询l-r内有多少颜色为c的物品并输出.2.将第x个物品和第x+1个交换.现在让你实现这些操作. 解题思路:首先一共有300000种颜色, ...

  9. springboot ajax返回html

    因为拦截器 或者是 shiro  拦截登陆接口

  10. php7 memcache和memcached.so扩展

    php7安装memcache和memcached扩展 https://github.com/websupport-sk/pecl-memcache https://github.com/php-mem ...