forked from ghallak/jpeg-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman.py
87 lines (65 loc) · 2.58 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
from queue import PriorityQueue
class HuffmanTree:
class __Node:
def __init__(self, value, freq, left_child, right_child):
self.value = value
self.freq = freq
self.left_child = left_child
self.right_child = right_child
@classmethod
def init_leaf(self, value, freq):
return self(value, freq, None, None)
@classmethod
def init_node(self, left_child, right_child):
freq = left_child.freq + right_child.freq
return self(None, freq, left_child, right_child)
def is_leaf(self):
return self.value is not None
def __eq__(self, other):
stup = self.value, self.freq, self.left_child, self.right_child
otup = other.value, other.freq, other.left_child, other.right_child
return stup == otup
def __nq__(self, other):
return not (self == other)
def __lt__(self, other):
return self.freq < other.freq
def __le__(self, other):
return self.freq < other.freq or self.freq == other.freq
def __gt__(self, other):
return not (self <= other)
def __ge__(self, other):
return not (self < other)
def __init__(self, arr):
q = PriorityQueue()
# calculate frequencies and insert them into a priority queue
for val, freq in self.__calc_freq(arr).items():
q.put(self.__Node.init_leaf(val, freq))
while q.qsize() >= 2:
u = q.get()
v = q.get()
q.put(self.__Node.init_node(u, v))
self.__root = q.get()
# dictionaries to store huffman table
self.__value_to_bitstring = dict()
def value_to_bitstring_table(self):
if len(self.__value_to_bitstring.keys()) == 0:
self.__create_huffman_table()
return self.__value_to_bitstring
def __create_huffman_table(self):
def tree_traverse(current_node, bitstring=''):
if current_node is None:
return
if current_node.is_leaf():
self.__value_to_bitstring[current_node.value] = bitstring
return
tree_traverse(current_node.left_child, bitstring + '0')
tree_traverse(current_node.right_child, bitstring + '1')
tree_traverse(self.__root)
def __calc_freq(self, arr):
freq_dict = dict()
for elem in arr:
if elem in freq_dict:
freq_dict[elem] += 1
else:
freq_dict[elem] = 1
return freq_dict