哈夫曼树的证明

简述:在学习慕课《数据结构》时,关于哈夫曼树仅给出了算法描述,并没有给出哈夫曼树就是最优树的证明,查阅教材也没发现相关信息,通过上网浏览博客解决了该问题。

参考博客:1)https://blog.csdn.net/xiyanxiyan10/article/details/17580599

        2)https://blog.csdn.net/BigLeo/article/details/41243779

        3)http://blog.sina.com.cn/s/blog_cf5c8eae0102uz75.html

  

  首先需要明确代价函数,即cost function:cost of tree = Σ freq(i) * depth(i)。该式表明代价函数等于每一个叶节点的权重与该节点深度的乘积和。在 algorithm 中,作者给出了另一种描述:即除根节点外所有节点的权重之和。原文为:There is another way to write this cost function that is very helpful. Although we are only given frequencies for the leaves, we can define the frequency of any internal node to be the sum of the frequencies of its descendant leaves; this is, after all, the number of times the internal node is visited during encoding or decoding…

  不失一般性,设叶节点(V1,V2,V3...Vn)的权重分别为(W1,W2,W3...Wn),有(W1<W2<W3<...<Wn)。由哈夫曼树的构造易得V1和V2对应的结点是兄弟结点,且这两结点在二叉树中的深度不小于其它任何一个叶结点的深度(参见博客2))。

  数学归纳法证明见博客2):n = 2 时成立, 假设 n = k时成立{W1+W2,W3,...Wk+1}。n = k + 1时 cost(T) = cost(T') + W1+W2。最优成立。

  调整法见博客3),顺推理解哈夫曼树的构造过程:整体上最优必定把V1,V2放在二叉树的底部且为兄弟节点(有待商榷,具体讨论见博客1)),由cost(T) = cost(T') + W1+W2,整体最优的条件下,去掉权重最小的两个节点将其合并为一个节点的二叉树也要最小,即也需要满组最小的在整棵树的最底层。以此类推可理解哈夫曼树的构造过程。

  综上,关键在于对于cost(T) = cost(T') + W1+W2式子的理解与类推的思想。

  补充:1)哈夫曼树不唯一

     2)哈夫曼树是最优树之一不是全部

 

原文地址:https://www.cnblogs.com/jinjin-2018/p/8729423.html