-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffman.py
55 lines (48 loc) · 1.21 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 12 13:16:21 2018
@author: Abdallah-Elshamy
"""
from heapq import heappush as push, heappop as pop , heapify as make
import sys
import resource
sys.setrecursionlimit(10 ** 6)
resource.setrlimit(resource.RLIMIT_STACK, (2 ** 29, 2 ** 30))
tree = {}
def expand(x, y):
global tree
a = x[1]
b = y[1]
for node in a.split():
try:
tree[node] += "0"
except KeyError:
tree[node] = "0"
for node in b.split():
try:
tree[node] += "1"
except KeyError:
tree[node] = "1"
def huffman(freq):
global tree
if len(freq) == 2 :
expand(freq[0] , freq[1])
else:
a = pop(freq)
b = pop(freq)
push(freq , (a[0]+b[0] , a[1]+" "+b[1]) )
huffman(freq)
expand(a , b)
freq = []
with open('huffman.txt') as f:
n = int(f.readline())
data = f.readlines()
i = 1
for line in data:
freq.append( (int(line[:-1]) , str(i)) )
i += 1
make(freq)
huffman(freq)
binary_code = sorted(list(tree.values()), key = lambda x : len(x))
print(len(binary_code[n-1]) , len(binary_code[0]))