-
Notifications
You must be signed in to change notification settings - Fork 1
/
huffman_coding.py
82 lines (63 loc) · 2.28 KB
/
huffman_coding.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
#!/usr/bin/python
import heapq
import heap
import copy
class HuffmanCoding:
def __init__(self, frequency):
self.minHeap = []
self.frequency = frequency
self.codes = {}
self.reverse_mapping = {}
#self.make_heap(frequency)
def make_minheap(self, text):
for key in text:
heap_node = heap.HeapNode(key, self.frequency[key])
#print("heap_node.get_char()", heap_node.get_char())
heapq.heappush(self.minHeap, heap_node)
def write_code(self, root, current_code):
if root is None:
return
if root.get_char() != 'None':
self.codes[root.get_char()] = current_code
self.reverse_mapping[current_code] = root.get_char()
return
self.write_code(root.get_left(), current_code + "0")
self.write_code(root.get_right(), current_code + "1")
def check_input(self, text):
if text.isalpha():
for c in text:
if not c.islower():
return False
return True
else:
return False
def encode(self, text):
if self.check_input(text) == False:
print("invalid input")
return None
self.make_minheap(text)
while len(self.minHeap) > 1:
node_left = heapq.heappop(self.minHeap)
#print("node_left.get_freq()", node_left.get_freq())
node_right = heapq.heappop(self.minHeap)
merged = heap.HeapNode('None', node_left.get_freq() + node_right.get_freq())
merged.set_left(node_left)
merged.set_right(node_right)
heapq.heappush(self.minHeap, merged)
root = heapq.heappop(self.minHeap)
current_code = ""
self.write_code(root, current_code)
encoded = ""
for character in text:
encoded += self.codes[character]
return encoded
def decode_text(self, encoded_text):
current_code = ""
decoded_text = ""
for bit in encoded_text:
current_code += bit
if current_code in self.reverse_mapping:
character = self.reverse_mapping[current_code]
decoded_text += character
current_code = ""
return decoded_text