Skip to content
Snippets Groups Projects
Commit 0c4e5c85 authored by Jacob Theisen's avatar Jacob Theisen
Browse files

sjo2

parent 34e81f08
Branches
No related tags found
No related merge requests found
......@@ -49,22 +49,12 @@ for i in all_strings:
table = sorted(table,key=lambda x: x.freq)
# A Huffman Tree Node
class node:
def __init__(self, freq, symbol, left=None, right=None):
# frequency of symbol
self.freq = freq
# symbol name (character)
self.symbol = symbol
# node left of current node
self.left = left
# node right of current node
self.right = right
# tree direction (0/1)
self.huff = ''
......@@ -76,59 +66,38 @@ class rep_tree():
def printNodes(node, val=''):
# huffman code for current node
newVal = val + str(node.huff)
# if node is not an edge node
# then traverse inside it
if(node.left):
printNodes(node.left, newVal)
if(node.right):
printNodes(node.right, newVal)
# if node is edge node then
# display its huffman code
if(not node.left and not node.right):
rep_tree_arr.append(rep_tree(node.symbol, newVal))
# list containing unused nodes
nodes = []
for j in table:
nodes.append(node(j.freq, j.char))
while len(nodes) > 1:
# pick 2 smallest nodes
left = nodes[0]
right = nodes[1]
# assign directional value to these nodes
left.huff = 0
right.huff = 1
# combine the 2 smallest nodes to create
# new node as their parent
newNode = node(left.freq+right.freq, left.symbol+right.symbol, left, right)
# remove the 2 nodes and add their
# parent as new node among others
nodes.remove(left)
nodes.remove(right)
nodes.append(newNode)
# Huffman Tree is ready!
printNodes(nodes[0])
bit_string = ''
for i in all_strings:
a = [a for a in rep_tree_arr if a.symb == i]
bit_string += a[0].val
......
......@@ -101,6 +101,7 @@ for i in range(0,len(lz_code),6):
elif k not in string.printable:
print('extende')
counter +=1
print(counter)
with open('./hm_uncomp', 'a') as file:
file.write(huff_string[:value-counter])
huff_string = huff_string[value-counter:]
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment