-
Notifications
You must be signed in to change notification settings - Fork 0
/
Huffman.py
58 lines (51 loc) · 1.33 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
class Node():
def __init__(self, i, h):
self.i = i
self.l = None
self.r = None
self.h = h
class H():
def __init__(self):
self.H = list()
self.C = dict()
def insert(self, x):
self.H.append(x)
def extract(self):
min_h, position = float('inf'), 0
for i in range(len(self.H)):
if self.H[i].h < min_h:
min_h, position = self.H[i].h, i
return self.H.pop(position)
@staticmethod
def huffman(s, lst):
for i in lst:
H.insert(s, Node(i[0], i[1]))
while len(s.H) > 1:
x = Node(None, 0)
x.r = H.extract(s)
x.l = H.extract(s)
x.h = (x.r.h + x.l.h)
s.insert(x)
def code(self, x, c=''):
if x.l is None and x.r is None:
self.C[x.i] = c if c else '0'
else:
self.code(x.l, c+'1')
self.code(x.r, c+'0')
def main():
s = input()
k = dict()
for i in s:
k[i] = 1 if i not in k else k[i] + 1
node = H()
H.huffman(node, k.items())
node.code(node.H[0])
m = ''
for i in s:
m += node.C[i]
print(len(k), len(m))
for i in sorted(node.C.items(), key= lambda x: x[1]):
print(f'{i[0]}: {i[1]}')
print(m)
if __name__ == '__main__':
main()