huffman decoder
This commit is contained in:
parent
cdd9cf296f
commit
901cc818bc
31
370/homework/huff.py
Normal file
31
370/homework/huff.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
class Node():
|
||||||
|
def __init__(self, c, weight):
|
||||||
|
self.c = c
|
||||||
|
self.freq = weight
|
||||||
|
self.left = None
|
||||||
|
self.right = None
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'{self.c}|{self.freq}'
|
||||||
|
|
||||||
|
class Huffman:
|
||||||
|
def __init__(self, string):
|
||||||
|
self.heap = []
|
||||||
|
self.charMap = self.charFrequencies(string)
|
||||||
|
|
||||||
|
def charFrequencies(self, string):
|
||||||
|
ret = []
|
||||||
|
for i in string:
|
||||||
|
if i in ret:
|
||||||
|
ret[i] += 1
|
||||||
|
else:
|
||||||
|
ret[i] = 1
|
||||||
|
return ret.sort()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
text = input()
|
||||||
|
binary = input()
|
||||||
|
|
||||||
|
# encode the first given string to build a tree from it
|
||||||
|
huff = Huffman(text)
|
||||||
|
print(huff.charMap)
|
@ -2,12 +2,12 @@ import queue
|
|||||||
class Node():
|
class Node():
|
||||||
def __init__(self, c, weight):
|
def __init__(self, c, weight):
|
||||||
self.c = c
|
self.c = c
|
||||||
self.weight = weight
|
self.freq = weight
|
||||||
self.left = None
|
self.left = None
|
||||||
self.right = None
|
self.right = None
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'{self.c}|{self.weight}'
|
return f'{self.c}|{self.freq}'
|
||||||
|
|
||||||
class MinHeap():
|
class MinHeap():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -58,12 +58,16 @@ class MinHeap():
|
|||||||
self.__heapifyDown(mini)
|
self.__heapifyDown(mini)
|
||||||
|
|
||||||
def printChar(self, c):
|
def printChar(self, c):
|
||||||
# Traverse through the tree until we get to a leaf then back out
|
# TODO: this method
|
||||||
tmp = self.data[0]
|
curr = self.data[0]
|
||||||
# keep going until we hit a leaf
|
for i in bin:
|
||||||
while tmp.left is not None or tmp.right is not None:
|
if i == '1' and curr.right is not None:
|
||||||
# 0 if left : 1 if right
|
curr = curr.right
|
||||||
continue
|
if i == '0' and curr.left is not None:
|
||||||
|
curr = curr.left
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -97,10 +101,10 @@ def frequencyMap(string):
|
|||||||
# otherwise increment the frequency of that node
|
# otherwise increment the frequency of that node
|
||||||
for k in ret:
|
for k in ret:
|
||||||
if k.c == i:
|
if k.c == i:
|
||||||
k.weight += 1
|
k.freq += 1
|
||||||
|
|
||||||
# Sort the charmap based on the frequencies
|
# Sort the charmap based on the frequencies
|
||||||
ret.sort(key=lambda x: x.weight)
|
ret.sort(key=lambda x: x.freq)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
@ -110,9 +114,15 @@ def buildMinHeap(freqs):
|
|||||||
while _queue.qsize() >= 2:
|
while _queue.qsize() >= 2:
|
||||||
left = _queue.get()
|
left = _queue.get()
|
||||||
right = _queue.get()
|
right = _queue.get()
|
||||||
weight = left.weight + right.weight
|
weight = left.freq+ right.freq
|
||||||
root = Node('*', weight)
|
root = Node('*', weight)
|
||||||
|
|
||||||
|
# insert the new sub-tree into the minheap and add the tree to the queue
|
||||||
|
heap.insert(root)
|
||||||
|
_queue.put(root)
|
||||||
|
|
||||||
# Once there is one item in the queue left we can simply return the new thing
|
# Once there is one item in the queue left we can simply return the new thing
|
||||||
|
heap.print()
|
||||||
return heap
|
return heap
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user