Huffman Tree哈夫曼树权值路径长度WPL计算,binarytree ,Python

50 篇文章 5 订阅

Huffman Tree哈夫曼树(霍夫曼树、赫夫曼树)权值路径长度WPL计算,binarytree ,Python

计算定义:把构建成功的哈夫曼树的每一个边缘节点(叶子)值乘以该节点到根的路径长度,最后求合。

import random

from binarytree import Node, get_parent


def app():
    data = []
    # 生成随机测试数据。
    for i in range(5):
        d = random.randint(1, 50)
        data.append(d)
    random.shuffle(data)
    print(data)
    huffman_tree = build_huffman_tree(data)
    print('-----')
    print('最终的Huffman树')
    root = huffman_tree.pop()
    print(root)
    print('---')
    print(data)
    print('计算WPL')
    wpl(root)


def build_huffman_tree(data):
    nodes = []
    for i in data:
        nodes.append(Node(i))
    print('nodes', nodes)
    print('开始构建Huffman树')

    while True:
        nodes = sorted(nodes, key=lambda x: x.value)
        # print('nodes', nodes)

        if len(nodes) == 1:
            # print('仅剩一个节点,建树结束')
            break

        left = nodes.pop(0)
        right = nodes.pop(0)
        # print('选取节点', left.value, right.value)

        root = Node(left.value + right.value)
        root.left = left
        root.right = right
        nodes.append(root)

    return nodes


# 计算带权值路径长度WPL
def wpl(root):
    leaves = root.leaves
    sum = 0
    for l in leaves:
        path = count_path(root, l)
        print(l.value, '->', root.value, '长度 = ', path)
        sum = sum + path * l.value

    print('带权值路径长度WPL =', sum)


def count_path(root, node):
    root_value = root.value
    count = 0
    while True:
        p = get_parent(root, node)
        count = count + 1
        if p.value == root_value:
            break
        else:
            node = p
    return count


if __name__ == '__main__':
    app()

输出:

[18, 35, 40, 3, 9]
nodes [Node(18), Node(35), Node(40), Node(3), Node(9)]
开始构建Huffman树
-----
最终的Huffman树

  _105_____________
 /                 \
40              ____65
               /      \
           ___30       35
          /     \
         12      18
        /  \
       3    9

---
[18, 35, 40, 3, 9]
计算WPL
40 -> 105 长度 =  1
35 -> 105 长度 =  2
18 -> 105 长度 =  3
3 -> 105 长度 =  4
9 -> 105 长度 =  4
带权值路径长度WPL = 212

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值