-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffman.py
396 lines (350 loc) · 9.91 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
from algopy import bintree
from algopy import heap
## COMPRESSION
def __frequency(dataIN, c):
"""
:param dataIN: string
:param c: character
:return: frequency of the character c in the string dataIN
"""
count = 0
for i in range(len(dataIN)):
if dataIN[i] == c:
count += 1
return count
def __inthelist(L, c):
"""
:param L: list of Tuple(frequency, char)
:param c: char
:return: True if the character c is in the list L or False if not
"""
for i in range(len(L)):
if c == L[i][1]:
return True
return False
def buildfrequencylist(dataIN):
"""
Builds a tuple list of the character frequencies in the input.
"""
L = []
for i in range(len(dataIN)):
if not __inthelist(L, dataIN[i]):
frequency = __frequency(dataIN, dataIN[i])
L.append((frequency, dataIN[i]))
return L
def __listtree(inputList):
"""
:param inputList: list of Tuple(frequency, char)
:return: a bintree list where each bintree is a leaf and have a Tuple(frequency, char) as key
"""
for i in range(len(inputList)):
inputList[i] = bintree.BinTree((inputList[i][0], inputList[i][1]), None, None)
return inputList
def __2minlist(inputList):
"""
:param inputList: bintree list (result of __listtree(inputList))
:return: input list but with a new bintree created from the 2 smallest frequency of the list
"""
# first min
mini1 = inputList[0]
mini1freq = mini1.key[0]
length = len(inputList)
index = 0
for i in range(length):
if inputList[i].key[0] < mini1freq:
mini1 = inputList[i]
mini1freq = mini1.key[0]
index = i
(inputList[index], inputList[length - 1]) = (inputList[length - 1], inputList[index])
mini1 = inputList.pop()
# second min
mini2 = inputList[0]
mini2freq = mini2.key[0]
index = 0
length = len(inputList)
for i in range(length):
if inputList[i].key[0] < mini2freq:
mini2 = inputList[i]
mini2freq = mini2.key[0]
index = i
(inputList[index], inputList[length - 1]) = (inputList[length - 1], inputList[index])
mini2 = inputList.pop()
inputList.append(bintree.BinTree((mini1freq + mini2freq, None), mini1, mini2))
return inputList
def __changeKey(B):
"""
:param B: an huffman tree
:return: the same huffman tree but the key as changed,
"""
if B != None:
if B.key[1] == '_':
B.key = ' '
else:
B.key = B.key[1]
__changeKey(B.left)
__changeKey(B.right)
return B
def buildHuffmantree(inputList):
"""
Processes the frequency list into a Huffman tree according to the algorithm.
"""
inputList = __listtree(inputList)
while len(inputList) > 1:
inputList = __2minlist(inputList)
B = __changeKey(inputList[0])
return B
def __occurences_list(B, L=[], s=""):
"""
:param B: huffman tree
:param L: an empty list to fill
:param s: the occurence string
:return: a tuple list where a tuple is (B.key, occurence)
"""
if B != None:
if B.key != None:
L.append((B.key, s))
if B.left != None:
__occurences_list(B.left, L, s + "0")
if B.right != None:
__occurences_list(B.right, L, s + "1")
return L
def __charinlist(L, c):
"""
:param L: a list of tuples (char or None, occurence)
:param c: a char
:return: True if c is in the list, else False
"""
length = len(L)
for i in range(length):
if L[i][0] == '_' and c == ' ':
return i, True
if L[i][0] == c:
return i, True
return -1, False
def encodedata(huffmanTree, dataIN):
"""
Encodes the input string to its binary string representation.
"""
s = ""
L = __occurences_list(huffmanTree, [], "")
length = len(dataIN)
for i in range(length):
(index, valid) = __charinlist(L, dataIN[i])
if valid:
s += L[index][1]
return s
def __chartobin(char):
"""
:param char: a character
:return: the binary representation of the ascii code of the character
"""
ascii = ord(char)
s = ""
while ascii > 0:
s += str(ascii % 2)
ascii = ascii // 2
while len(s) < 8:
s += '0'
res = ""
index = len(s) - 1
while index >= 0:
res += s[index]
index -= 1
return res
def __encodetree2(B, s=""):
"""
:param B: a binarytree
:param s: s string that represents the tree with 1 and 0
:return: the binary representation of the tree B
"""
if B:
if B.key:
return '1' + __chartobin(B.key) + __encodetree2(B.left, s) + __encodetree2(B.right, s)
else:
return '0' + __encodetree2(B.left, s) + __encodetree2(B.right, s)
return s
def encodetree(huffmanTree):
"""
Encodes a huffman tree to its binary representation using a preOrder traversal:
* each leaf key is encoded into its binary representation on 8 bits preceded by '1'
* each time we go left we add a '0' to the result
"""
return __encodetree2(huffmanTree)
def tobinary(dataIN):
"""
Compresses a string containing binary code to its real binary value.
"""
length = len(dataIN) - 1
full = length // 8
res = ""
nbChar = 0
index = 0
while nbChar < full:
s = ""
index2 = 0
while index2 < 8:
s += dataIN[index]
index2 += 1
index += 1
res += __binarytochar(s)
nbChar += 1
reste = length % 8 + 1
nb0 = 8 - reste
s = ""
while nb0 > 0:
s += '0'
nb0 -= 1
while index <= length:
s += dataIN[index]
index += 1
res += __binarytochar(s)
return res, 8 - reste
def compress(dataIn):
"""
The main function that makes the whole compression process.
"""
freqList = buildfrequencylist(dataIn)
huffmanTree = buildHuffmantree(freqList)
dataencoded = encodedata(huffmanTree, dataIn)
treeencoded = encodetree(huffmanTree)
return tobinary(dataencoded), tobinary(treeencoded)
## DECOMPRESSION
def __charintree(B, dataIn, index):
valid = True
res = ""
while valid:
if B.key != None:
valid = False
if B.key == '_':
res += ' '
else:
res = B.key
elif dataIn[index] == '0':
B = B.left
else:
B = B.right
index += 1
return res, index - 1
def decodedata(huffmanTree, dataIN):
"""
Decode a string using the corresponding huffman tree into something more readable.
"""
res = ""
length = len(dataIN)
index = 0
resbis, index = __charintree(huffmanTree, dataIN, index)
res += resbis
while index < length:
resbis, index = __charintree(huffmanTree, dataIN, index)
res += resbis
return res
def __binarytochar(binarystring):
"""
:param binarystring: a binary ascii code
:return: the character of the binary ascii code
"""
pow = 7
i = 0
res = 0
while i < len(binarystring):
if binarystring[i] == '1':
res += 2**pow
pow -= 1
i += 1
return chr(res)
def __reverseList(L):
"""
:param L: a list
:return: the list reverse
"""
Lres = []
indexL = len(L) - 1
while indexL >= 0:
Lres.append(L[indexL])
indexL -= 1
L = Lres
return L
def __charlist(dataIN):
"""
:param dataIN: the encoded tree
:return: a tuple (list of char, structure string)
"""
L = []
index = 0
structure = []
while index < len(dataIN):
s = ""
if dataIN[index] == '1':
structure.append(dataIN[index])
index += 1
i = index
while i < index + 8:
s += dataIN[i]
i += 1
index += 8
L.append(__binarytochar(s))
else:
structure.append(dataIN[index])
index += 1
L = __reverseList(L)
structure = __reverseList(structure)
return L, structure
def __decodetree(charlist, structure):
"""
:param dataIN: the encoded data
:param B: the new tree
:param index: the index to go through the data
:return: the tree
"""
if len(structure) <= 0:
return None
index_struct = len(structure) - 1
if structure[index_struct] == '1':
structure.pop()
index = len(charlist) - 1
key = charlist[index]
charlist.pop()
B = bintree.BinTree(key, None, None)
return B
elif structure[index_struct] == '0':
structure.pop()
B = bintree.BinTree(None, None, None)
B.left = __decodetree(charlist, structure)
B.right = __decodetree(charlist, structure)
return B
return None
def decodetree(dataIN):
"""
Decodes a huffman tree from its binary representation:
* a '0' means we add a new internal node and go to its left node
* a '1' means the next 8 values are the encoded character of the current leaf
"""
(charlist, structure) = __charlist(dataIN)
return __decodetree(charlist, structure)
def frombinary(dataIN, align):
"""
Retrieve a string containing binary code from its real binary value (inverse of :func:`toBinary`).
"""
index = 0
res = ""
while index < len(dataIN) - 1:
char = __chartobin(dataIN[index])
res += char
index += 1
s = __chartobin(dataIN[index])
i = 0
while i < len(s):
if i < align:
i += 1
continue
res += s[i]
i += 1
return res
def decompress(data, dataAlign, tree, treeAlign):
"""
The whole decompression process.
"""
fromBdata = frombinary(data, dataAlign)
fromBtree = frombinary(tree, treeAlign)
decodedtree = decodetree(fromBtree)
return decodedata(decodedtree, fromBdata)