huffman.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:tryalgo 作者: jilljenn 项目源码 文件源码
def huffman(freq):
    """Huffman code

    :param freq: dictionary with frequencies for each item
    :returns: dictionary with binary code string for each item
    :complexity: O(n log n)
    """
    h = []
    for a in freq:
        heappush(h, (freq[a], a))
    while len(h) > 1:
        (fl, l) = heappop(h)
        (fr, r) = heappop(h)
        heappush(h, (fl + fr, [l, r]))
    code = {}
    extract(code, h[0][1])
    return code
评论列表


问题


面经


文章

微信
公众号

扫码关注公众号