哈夫曼树学习笔记
既然我们要学习赫夫曼树,那么我们首先就要知道什么叫赫夫曼树。
那么什么叫赫夫曼树呢?
一、什么叫赫夫曼树?
书上说:“赫夫曼(Huffman)树又称最优树,是一类带权路径长度最短的树,但是我们仅学习最优二叉树。”
看到这个还是不明白什么意思,因此在学习之前我们要结合这个图了解几个基本概念。
路 径:由一结点到另一结点间的分支所构成。如:a->b a->b->e
路径长度:路径上的分支数目,如:a→e的路径长度=2 a->c的路径长度=1
树的路径长度:从树根到每一结点的路径长度之和。如:树的路径长度=10=2+2+2+2+1+1
带权路径长度:树中所有叶子结点的带权路径长度之和。
树的带权路径长度:树中所有叶子结点的带权路径长度之和。
赫 夫 曼 树:带权路径长度最小的二叉树。
了解这些基本概念后,可能还不是很理解什么是赫夫曼树,那么我们再看这样一个例子。
已经a的权值为7,b的权值为5,c的权值为2,d的权值为4,判断下列哪一个是赫夫曼树?
如图所示,
WPL=36 WPL=46 WPL=35
赫 夫 曼 树:带权路径长度最小的二叉树。
树的带权路径长度如何计算?
(a)计算方法:WPL=7*2+5*2+2*2+4*2=36
(b)计算方法:WPL=4*2+7*3+5*3+2*1=46
(c)计算方法:WPL=7*1+5*2+2*3+4*3=35
赫夫曼树则是:WPL 最小的二叉树。
第三个带权路径长度最短,因此第三个就是赫夫曼树,即最优的二叉树。
二、如何构造一个赫夫曼树?
知道什么是赫夫曼树后,那么如何才能够构造一个赫夫曼树呢?
书上给出了如下方法:
(1)由给定的n 个权值{w1, w2, …, wn}构成n棵二叉树的集合(即森林)F = { T1, T2, …, Tn },其中每棵二叉树Ti 中只有一个带权为wi 的根结点,其左右子树均空。(2)在F 中选取两棵根结点的权值最小的树作为左右子树构造一棵新的二叉树,且置新的二叉树的根结点的权值为其左右子树上根结点的权值之和。(3)在F 中删去这两棵树,同时将新得到的二叉树加入F中。(4)重复(2)和(3), 直到F 只含一棵树为止。这棵树便是赫夫曼树。
看到这个方法后,可能还是看不懂,所以我们再看一个例子。
例1: 已知权值 W={ 5, 6, 2, 9, 7 },求构造赫夫曼树,并求其赫夫曼编码。
1.首先根据权值构成集合F={ ![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEQAAAA7CAIAAABNBbvxAAAF50lEQVRogdWaP0zbWBjAPSEWJCQW2ikTYjmpaheuU6ajQ5FQWVCnLInQhSGiGciCB4YyBFGQLhXKkAwNFF2P6rxESJHC0KQSsmwCOZCPkL+1HEWuQiKsKFYc3/CcFxOc/8/msL6BmOh73y/vve/73vc+TEb9VLjLMhMuM+F8aIslcJbAr7ZfJTzz4O/C8Ufw3yqfQj40hkRLTSj+PNnPBJbOXSbShvUo5y5TJrBUpL/WhCISM4aCEbL0j8PVi/VnvQO0k8uNGZbAh5yuAWGELJ3wzGv92E8zgQWWsOZDK2Vmt8zsVrgv9TpZr5NCdg+84YLLLGFN++fOXU/va0j7LUKWNgimzISvtl+ph6cdYwmPuXDsgnb3LhXuSz60kvCYKfuIWmfCMz8AUh8wYoljNs0t88BH8H4B2gkfwVvmKuGZF0scepifJ/u0YxwOc7E+VaTdqDBakOJrTS8Sc07exIPIYGpCMe23GIChliLtViNlAkuSWBkW5vb6e3xtGir9cWiRxKjeJEAkMZoJLMCh42vTXXdRJxghS8OlFXNOlJldYzDUchPfiTknGp5mvDNPWxg1ScJjrglh40mAiKWjq+2XMM52iEXaMFU+BWN52j/3UBhqSXpnu/JowKhJkt7ZB8cAIolROD8X6880M6BWGEmswB1/tf3SsO3eL8/lxkx3GC74Hnyb2XzxvyIBUhPCF+tTwMJ8aKsTTJVPUfZR0oZR9pEqTzy46ZoiZPdA7kM7xlvygzsw/35Qki4uuIxk4F0M6yoDqM0dWhpbelEb5ufJfiM8mZAsMJ1I6nVSEqOnjeCjTnaaMGerigdDFRz1g6nXySLthp6tFabMhGF8NIxkGJh6nbz68CuwucJd3oHJBJYas7aDFgaJNk3hv+HA5txfq00YSaxQjnHShp06J5DvFv1gJDFK/T5C2rCzVVMTpkh/VTLtT28ey7QASXp/a+zzsAKT9C6C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LF0wh5IDemYBBphBy6IEZVD9cMPw/5iaMlGW1PXNkwgvTfxKgBybz7gXQvyqmCLkkgDcp7zOdMAPx4IKB6WYjzlinaQvBuR7qh+nJc/dbPST1Ov2vc562ENHXcxoMOMnEHPexwHTSuIvomYu13aMtxMXWYw0Gnpbxnvu76I0LRsq+1wpJfyxrMIWTfbxH/4E49YwDg4wY9mow0AfENw1jJtEJAxIzxjoN7giIlrpMRfjrZzFLmTtoqdFoMDAP0B9txmYWGGFggUaDUeQKcNBn9lm5dPzjk8ilY3CSYWwz8B6qWWrKHdowRs+eMDqHSri121WeItvUzRS5En09h6t0NlISWDQ73zCg14PflWfxFjVHRNKpnNkKo46g3IyXpI7s+5ZCcxsYeBGg/4RTx7rjgQiBdaAbc+cKoA2MirhpPTzYMepICZOxTrdtDmh/p4ny9F+wxZtBdiK5u1V6wKA8jHUKY0/JcFI4cTQXS9jbSedufQA81fwz0r7nE7k9r0khGFK6k/SAUZErTuCv9dRvh5Dbay/saOhJ0htGVdUyF4ra5+CSEwLrY7i4VeQIT5mR/3Hh9vpLT1X76mqqSUW0VS5qn80HX41o1dWkEE+ZUYOkfaY++x4H6DcrnOzDEERbCNZ2j6fMGLNSuXScO3wJjsGNKWZ6Lq0hYVRVrYqptM+EIjHWqaRnSQyTQ1NVRUoMk0nPEtoMyFinc4e2gTrnBoYBj1wScoc2tJcOeojc4cub+F73PFWRI2XuoMi6Mv4VtKkMWoOnyEExhocBT00q8hSJdqPdFcY6xW0/gtLll7SFuHAu5oM7etqCMfQ1yyVBDHuTnlXo9PqXqH0u7TOJYe9wpsAPgz5SlhUCb3iKzPjXuG0jt22EDVIxhwF8kvGv8RSZD+7AK29cz/8Hjf7VGEkuZAAAAABJRU5ErkJggg==)
}
2.在该集合中选取根两个最小的
和
作为左右子树构造一棵新的二叉树,且置新的二叉树的根节点的权值为其左右子树上根节点的权值之和。
3.在该结果中删去这两棵树,同时将新得到的二叉树加入F中。
即该集合现在F={![](data:image/png;base64,iVBORw0K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xgEukst8dgzwsA4jcuSCBvxgR/sN90yYEbCFMIuaJ0R6GQKYdfAkYgiTv9jbcBwWVLeM0eWQYTJfH4P9K+yKYQvMeBHyvduEGFguFn3M/Zh3IZQ7lcDRyKK+L/rE7gNiX0Yl2HASebC9WIQYUjHCG5DLjemZRh4Wu7rub8fJFx2X04k/TEnwxROdg1wNf2AgU6GjfhkGGgD4mumwYIBgRlhHwY1AqQpL1Nh/hoUkjK115SjkWFgHNAnb9MPGOhhYIJGhhH4CjDQZ84xvnT888PwpWNwkiEco7AO1Ug15Q4dffKe/ZiWhEeurtIY2iJvJvCV2Idx7akzA2Bg0ux81aQsDz5Iz+qS1Ow3zFPpzGYYSad0c1+nBe77pkRzCxhYCNDrhKMvDBNcBroRj0oALWAkhZnWhUdHGJjCJOzDLZsDWtc0lTxGJmy7IXm8VTrAKHkI+5COPSXqpHDiaiyWiO8pndv1AdBY42Ok/bPPUj2vcWHoUtqTdICRFCVOYK+15G9VyP2ND3Y0dCTpDCNJUpkKx5zjcMkxwWUDCrcCH6Uxq+I7Tt7ffO+oalddTTWuqGyViznH8qGVPq26GhemMatyQtJ+S5d9jz30mxVOdqELwm0I6RihMauOUSlfOs4dLoJjcH2I0Y5LSyWMJElVNpX2W5RIhH0o6Z1hI6hqqiqLsRE06Z1RNgMS9uHcoaOnzrmeYcDFl5jcoUPZSwctRO5w8S6+0z5OFfhomdorku5MYF7ZVAZng8bQXjHUw4CrxhVpDFV2oz0Wwj5Ebb6G0uZJ3IZcrk/lQ1ta2oJ16GvmSwwb8SW9C9DodS8x53jab2EjPnVToT+M8uKyJBP8SGNoJrBEbZqpTTNskLpwmcCdTGCJxtB8aAuWvPW6/gdDPX+5NMWGfQAAAABJRU5ErkJggg==)
}
然后现在集合中最小的两个为
和
或
,因此这时候便会出现两种形态的赫夫曼树。
或者
重复(2)和(3), 直到F 只含一棵树为止。这棵树便是赫夫曼树。
4.因此根据左0右1的规则,最终结果也将有两种,这两种都是赫夫曼树,都是最优二叉树。
其一:
WPL=6*2+7*2+2*3+5*3+9*2=65
6的编码为:00
7的编码:01
2的编码:100
5的编码:101
9的编码:11
或者
其二:
WPL=2*3+5*3+6*2+9*2+7*2=65
6的编码为:01
7的编码:000
2的编码:000
5的编码:001
9的编码:10********************例子一结束**********************************
做完这个例子后,应该差不多明白如何构造一个赫夫曼树了,那么我们再看一个下面例子练练手:
操作要点1:对权值的合并、删除与替换
——在权值集合{7,5,2,4}中,总是合并当前值最小的两个权值
注:方框表示外结点(叶子,字符对应的权值),圆框表示内结点(合并后的权值)。
操作要点2:按左0右1对Huffman树的所有分支编号!
Huffman编码结果:
d=0, i=10, a=110, n=111
WPL=1bit×7+2bit×5+3bit(2+4)=35
*********************
交流即分享,分享才能进步!
不对之处,还请各位前辈多多指教。
by 星云
********************