def create_huffman_tree(txt):
q = [TreeNode(c, cnt) for c, cnt in collections.Counter(txt).items()]
heapq.heapify(q)
while len(q) > 1:
a, b = heapq.heappop(q), heapq.heappop(q)
heapq.heappush(q, TreeNode('', a.cnt + b.cnt, a, b))
return q.pop()
评论列表
文章目录