def huff_code(txt, ab, freq):
l = len(ab)
Q = [Node(i, freq[i]) for i in range(l)]
heapq.heapify(Q)
#for node in Q:
# print(node)
for i in range(l-1):
n1 = heapq.heappop(Q)
n2 = heapq.heappop(Q)
n = Node(-1, n1.freq+n2.freq, n1, n2)
heapq.heappush(Q,n)
return heapq.heappop(Q)
评论列表
文章目录