-
Notifications
You must be signed in to change notification settings - Fork 1
/
Huffman.py
157 lines (119 loc) · 2.56 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
class Tree:
def __init__(self, cargo, left=None, right=None):
self.cargo = cargo
self.left = left
self.right = right
def __str__(self):
return str(self.cargo)
def delMin(H):
global currentSize
retval = H[1]
H[1] = H[currentSize]
# print 'this is H1',H
# print
# print 'currentSize in delMin1', currentSize
# print
currentSize = currentSize - 1
# print 'currentSize in delMin2', currentSize
# print
H.pop()
# print 'this is H2 after pop',H
# print
percDown(1)
return retval
def percDown(i):
while (i * 2) <= currentSize:
# print 'this is currentSize',currentSize
# print
mc = minChild(i)
# print 'this is mc',mc
# print
if H[i][1] > H[mc][1]:
tmp = H[i]
H[i] = H[mc]
H[mc] = tmp
i = mc
def minChild(i):
if i * 2 + 1 > currentSize:
return i * 2
else:
# print 'this is i in minChild', i
# print
if H[i*2][1] < H[i*2+1][1]:
return i * 2
else:
return i * 2 + 1
def percUp(i):
while i // 2 > 0:
if H[i][1] < H[i // 2][1]:
tmp = H[i // 2]
H[i // 2] = H[i]
H[i] = tmp
i = i // 2
def insert(k):
global currentSize
H.append(k)
# print 'this is H in insert',H
# print
currentSize = currentSize + 1
percUp(currentSize)
def stringToFreq(stringInput):
global S, f, n
S = list(stringInput)
S = dict.fromkeys(stringInput).keys()
S.sort()
n = len(S)
f = []
for w in S:
f.append(stringInput.count(w))
a = 0
f = [a] + f
S = [a] + S
return S, f
inputString="AAABBBBCCDEIIz"
print 'these are my S and f arrays',stringToFreq(inputString)
print
################################
## begin the Huffman tree here##
################################
def huffmanEncode(S,f):
global H
H = []
n = len(f)
# this for loop makes the min heap
for i in range(0,n):
H.append([S[i],f[i]])
H = sorted(H, key=lambda H: H[1])
return H
H = huffmanEncode(S,f)
currentSize = len(H) - 1
# print 'this is my H after huffmanEncode',H
# print
# i = delMin(H)
# print 'this is i', i
# print
# print 'H after delMin called for i',H
# print
# j = delMin(H)
# print 'this is j',j
# print
# print 'H after delMin called for j',H
# print
# k = [i[0]+j[0],i[1]+j[1]]
# print 'this is k',k
# insert(k)
# print 'this is H after K insert',H
# print
while currentSize != 1:
print 'this is H:',H
print
i = delMin(H)
j = delMin(H)
k = [i[0]+j[0],i[1]+j[1]]
# insert(k)
left = i
right = j
tree = Tree(k,left,right)
insert(tree)
print 'this is final H:',H
print