-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHuffman.py
80 lines (43 loc) · 1.32 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from heapq import heappush, heappop
from collections import Counter
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def __eq__(self, other): # Ignore Heap comparisons past freq
return True
def _add_codes(node, codes, prefix=""):
if node:
if node.value != '*':
codes.update({node.value: prefix})
_add_codes(node.left, codes, prefix + "0")
_add_codes(node.right, codes, prefix + "1")
def get_huffman_codes(msg):
freqs = Counter(msg)
heap = []
for char, freq in freqs.items():
heappush(heap, (freq, Node(char)))
while len(heap) > 1:
left, right = heappop(heap), heappop(heap)
combined = Node('*')
combined.left = left[1]
combined.right = right[1]
heappush(heap, (left[0] + right[0], combined))
codes = {}
_add_codes(heap[0][1], codes)
return codes
def encode(msg, codes):
compressed = ""
for c in msg:
compressed += codes[c]
return compressed
def main():
msg = "This is a test of huffman encoding............"
print(msg)
codes = get_huffman_codes(msg)
for char, code in codes.items():
print(f'{char} {code}')
print(encode(msg, codes))
if __name__ == '__main__':
main()