博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
超好理解的哈夫曼树(最优二叉树)与例题
阅读量:282 次
发布时间:2019-03-01

本文共 4192 字,大约阅读时间需要 13 分钟。

对于比较官方的说法就是给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman Tree)。哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近。

凡是涉及带权路径最短长度的问题,也就是解决一个问题需要分成几步并且这几步需要的代价很明确,我们要求最小的代价的时候,看起来像贪心的题目,我们都可以用哈夫曼树来解决,如小明决定把所有的水果合成一堆。每一次合并,小明可以把两堆水果合并到一起,消耗的体力等于两堆水果的重量之和一块木板分割成N块,输入每块的长度,切割木板的长度即为花费的钱,求最小花费。

这样说可能看不明白,用一个例子模拟就能很明白的看懂了,需要注意的就是在构建哈夫曼树的时候,每形成一个父亲结点的时候都要重新进行一次排序。

下面我们以【5、8、4、11、9、13】为例来画出哈夫曼树(数字大小代表权重大小,越大的权重越大)

第一步:按从小到大排序。

【5、8、4、11、9、13】→【4、5、8、9、11、13】

第二步:选最小两个数画出一个树,最小数为4和5。

给定的4、5、8、9、11、13为白色, 红色的9为4+5,与给定的白9无关,新序列为:【红9(含子节点4、5)、8、9、11、13】

图示

之后一直重复第一、第二步:排序然后取两个最小值。实际就是一个递归过程

排序:

图示

取两个最小数8和9:

图示

排序:

图示

取两个最小数9和11:

图示

排序,然后取两个最小数13和17:

图示

取两个最小数20和30:

图示

上例子,摘自百度经验:和

这样整个哈夫曼树的构造原理就显得很清楚明白了,下面附上例题来看看哈夫曼树到底是如何构建的

核心思想就是每次生成父亲结点的时候都进行一次从小到大的排列,然后让权值最小的两个结点生成一个新的父亲结点,该父亲结点的权值就为他两个儿子的权值的和,递归这个过程,最后哈夫曼树的解也就是最小的带权值路径值就为各个父亲结点值的和

例一、哈夫曼树

题目描述:

哈夫曼树,第一行输入一个数n,表示叶结点的个数。需要用这些叶结点生成哈夫曼树,根据哈夫曼树的概念,这些结点有权值,即weight,题目需要输出所有结点的值与权值的乘积之和。

输入:

输入有多组数据。

每组第一行输入一个数n,接着输入n个叶节点(叶节点权值不超过100,2<=n<=1000)。

输出:

输出权值。

样例输入:

5  1 2 2 5 9

样例输出:

37

来源:

例一我们用不是建树的方法,只用数组来模拟上面的哈夫曼树构造过程

 

//不建树的方式#include 
#include
using namespace std;int huffman[100010];int main(){ int n; cin>>n; for(int i=1;i<=n;i++) { cin>>huffman[i]; } int i=2; int sum=0; while(i<=n) { sort(huffman+1,huffman+1+n); //根据哈夫曼树原理每次生成新的节点都要重新排序 int temp=huffman[i]+huffman[i-1]; //就算新的父节点的权值 sum+=temp; //求和器计算最后的哈夫曼权值 huffman[i]=temp; /* //优化 huffman[i]+=huffman[i-1]; sum+=huffman[i]; */ i++; } cout<
<

例二、

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the “kerf”, the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn’t own a saw with which to cut the wood, so he mosies over to Farmer Don’s Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn’t lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer N, the number of planks
Lines 2… N+1: Each line contains a single integer describing the length of a needed plank
Output
Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

Sample Input

3
8
5
8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).

题意:一块木板分割成N块,输入每块的长度,切割木板的长度即为花费的钱,求最小花费。

其实对于一般的哈夫曼树的问题我们一般也不采用递归建树和遍历树的方式来实现。因为每一次生成父亲结点的时候我们都会重新进行一次从小到大的排列,也就是这些结点的排列就有序的,这就可以让我们联想到一种新的数据结构,堆!!!也就是优先队列,让队列中的数从小到大排列,每次让最小的两个相加,让相加的两个数出队列,得到的和放入队尾,然后让和叠加,重复操作,得出结果。

#include 
#include
#include
using namespace std;priority_queue
,greater
> q;//最后两个 > 要分开,否则就是右移符号//greater的作用是使进入队列的数从小到大排列//less从大到小 priority_queue
q;默认less;int main(){ int n,m; cin>>n; while(n--) { cin>>m; q.push(m); } int sum=0; while(q.size()>=2) { //最小的两个元素出队列形成新的父亲结点重新压入栈 int a=q.top(); q.pop(); int b=q.top(); q.pop(); int c=a+b; sum+=c; q.push(c); } cout<
<

 

你可能感兴趣的文章