-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffman-coding.py
166 lines (131 loc) · 4.81 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
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
from node_class import Node
from tree_class import Tree
from tkinter import *
"""
Pseudocode:
1. Take a string and determine the relevant frequencies of the characters
2. Build and sort a list of tuples from lowest to highest frequencies
3. Build the Huffman Tree by assigning a binary code to each letter, using shorter codes for the more frequent letters
4. Trim the Huffman Tree (remove the frequencies from the previously built tree)
5. Encode the text into its compressed form
"""
def return_frequency(data):
# Take a string and determine the relevant frequencies of the characters
frequency = {}
for char in data:
if char in frequency:
frequency[char] += 1
else:
frequency[char] = 1
lst = [(v, k) for k, v in frequency.items()]
# Build and sort a list of tuples from lowest to highest frequencies
lst.sort(reverse=True)
return lst
# A helper function to the build_tree()
def sort_values(nodes_list, node):
node_value, char1 = node.value
index = 0
max_index = len(nodes_list)
while True:
if index == max_index:
nodes_list.append(node)
return
current_val, char2 = nodes_list[index].value
if current_val <= node_value:
nodes_list.insert(index, node)
return
index += 1
# Build a Huffman Tree: nodes are stored in list with their values (frequencies) in descending order.
# Two nodes with the lowest frequencies form a tree node. That node gets pushed back into the list and the process repeats
def build_tree(data):
lst = return_frequency(data)
nodes_list = []
for node_value in lst:
node = Node(node_value)
nodes_list.append(node)
while len(nodes_list) != 1:
first_node = nodes_list.pop()
second_node = nodes_list.pop()
val1, char1 = first_node.value
val2, char2 = second_node.value
node = Node((val1 + val2, char1 + char2))
node.set_left_child(second_node)
node.set_right_child(first_node)
sort_values(nodes_list, node)
root = nodes_list[0]
tree = Tree()
tree.root = root
return tree
# the function traverses over the huffman tree and returns a dictionary with letter as keys and binary value and value.
# function get_codes() is for encoding purposes
def get_codes(root):
if root is None:
return {}
frequency, characters = root.value
char_dict = dict([(i, '') for i in list(characters)])
left_branch = get_codes(root.get_left_child())
for key, value in left_branch.items():
char_dict[key] += '0' + left_branch[key]
right_branch = get_codes(root.get_right_child())
for key, value in right_branch.items():
char_dict[key] += '1' + right_branch[key]
return char_dict
# when we've got the dictionary of binary values and huffman tree, tree encoding is simple
def huffman_encoding_func(data):
if data == '':
return None, ''
tree = build_tree(data)
dict = get_codes(tree.root)
codes = ''
for char in data:
codes += dict[char]
return tree, codes
# The function traverses over the encoded data and checks if a certain piece of binary code could actually be a letter
def huffman_decoding_func(data, tree):
if data == '':
return ''
dict = get_codes(tree.root)
reversed_dict = {}
for value, key in dict.items():
reversed_dict[key] = value
start_index = 0
end_index = 1
max_index = len(data)
s = ''
while start_index != max_index:
if data[start_index : end_index] in reversed_dict:
s += reversed_dict[data[start_index : end_index]]
start_index = end_index
end_index += 1
return s
window = Tk()
window.title("Huffman Encoding and Decoding")
window.geometry('300x250')
def enClicked():
global tree, encoded_data
sentence = enArea.get(1.0, END)
tree, encoded_data = huffman_encoding_func(sentence)
enArea1.delete(1.0, END)
enArea1.insert(END, encoded_data)
def deClicked():
decoded_data = huffman_decoding_func(encoded_data, tree)
deArea.delete(1.0, END)
deArea.insert(END, decoded_data)
#Components
enLabel = Label(window, text="Enter Txt to Encode:")
enLabel.pack()
enArea = Text(window, width=30, height=2)
enArea.pack()
enButton = Button(window, text="Encode", command=enClicked)
enButton.pack()
enLabel1 = Label(window, text="The Encoded Text:")
enLabel1.pack()
enArea1 = Text(window, width=30, height=2)
enArea1.pack()
deButton = Button(window, text="Decode", command=deClicked)
deButton.pack()
deLabel = Label(window, text="The Decoded Text:")
deLabel.pack()
deArea = Text(window, width=30, height=2)
deArea.pack()
window.mainloop()