-
Notifications
You must be signed in to change notification settings - Fork 80
/
HuffmanDecoding.py
80 lines (63 loc) · 1.85 KB
/
HuffmanDecoding.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
import queue as Queue
cntr = 0
class Node:
def __init__(self, freq, data):
self.freq = freq
self.data = data
self.left = None
self.right = None
global cntr
self._count = cntr
cntr = cntr + 1
def __lt__(self, other):
if self.freq != other.freq:
return self.freq < other.freq
return self._count < other._count
def huffman_hidden():#builds the tree and returns root
q = Queue.PriorityQueue()
for key in freq:
q.put((freq[key], key, Node(freq[key], key) ))
while q.qsize() != 1:
a = q.get()
b = q.get()
obj = Node(a[0] + b[0], '\0' )
obj.left = a[2]
obj.right = b[2]
q.put((obj.freq, obj.data, obj ))
root = q.get()
root = root[2]#contains root object
return root
def dfs_hidden(obj, already):
if(obj == None):
return
elif(obj.data != '\0'):
code_hidden[obj.data] = already
dfs_hidden(obj.right, already + "1")
dfs_hidden(obj.left, already + "0")
"""class Node:
def __init__(self, freq,data):
self.freq= freq
self.data=data
self.left = None
self.right = None
"""
def is_last(node):
return not node.left and not node.right
def go_right(node):
return node.right
def go_left(node):
return node.left
# Enter your code here. Read input from STDIN. Print output to STDOUT
def decodeHuff(root , s):
result = []
node = root
for char in s:
if char == '1':
node = go_right(node)
elif char == '0':
node = go_left(node)
if is_last(node):
result.append(node.data)
node = root
print(''.join(result))
ip = input()