-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHuffman.py
38 lines (32 loc) · 997 Bytes
/
Huffman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'''
Author: Dwipam Katariya
Email: ddkatari@iu.edu
'''
from heapq import heappush, heappop, heapify
from collections import defaultdict
def encode(symb2freq):
heap = [[wt, [sym, ""]] for sym, wt in symb2freq.items()]
heapify(heap)
print(heap)
while len(heap) > 1:
lo = heappop(heap)
hi = heappop(heap)
for pair in lo[1:]:
pair[1] = '0' + pair[1]
#print(pair[1])
for pair in hi[1:]:
pair[1] = '1' + pair[1]
#print(pair[1])
heappush(heap, [lo[0] + hi[0]] + lo[1:] + hi[1:])
print(heap)
return sorted(heappop(heap)[1:], key=lambda p: (len(p[-1]), p))
txt = "aaaabbbccd"
symb2freq = defaultdict(int)
for ch in txt:
symb2freq[ch] += 1
print(symb2freq)
# symb2freq = collections.Counter(txt)
huff = encode(symb2freq)
print("Symbol\tWeight\tHuffman Code")
for p in huff:
print("%s\t%s\t%s" % (p[0], symb2freq[p[0]], p[1]))