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: ...@@ -49,22 +49,12 @@ for i in all_strings:
table = sorted(table,key=lambda x: x.freq) table = sorted(table,key=lambda x: x.freq)
# A Huffman Tree Node
class node: class node:
def __init__(self, freq, symbol, left=None, right=None): def __init__(self, freq, symbol, left=None, right=None):
# frequency of symbol
self.freq = freq self.freq = freq
# symbol name (character)
self.symbol = symbol self.symbol = symbol
# node left of current node
self.left = left self.left = left
# node right of current node
self.right = right self.right = right
# tree direction (0/1)
self.huff = '' self.huff = ''
...@@ -76,59 +66,38 @@ class rep_tree(): ...@@ -76,59 +66,38 @@ class rep_tree():
def printNodes(node, val=''): def printNodes(node, val=''):
# huffman code for current node
newVal = val + str(node.huff) newVal = val + str(node.huff)
# if node is not an edge node
# then traverse inside it
if(node.left): if(node.left):
printNodes(node.left, newVal) printNodes(node.left, newVal)
if(node.right): if(node.right):
printNodes(node.right, newVal) printNodes(node.right, newVal)
# if node is edge node then
# display its huffman code
if(not node.left and not node.right): if(not node.left and not node.right):
rep_tree_arr.append(rep_tree(node.symbol, newVal)) rep_tree_arr.append(rep_tree(node.symbol, newVal))
# list containing unused nodes
nodes = [] nodes = []
for j in table: for j in table:
nodes.append(node(j.freq, j.char)) nodes.append(node(j.freq, j.char))
while len(nodes) > 1: while len(nodes) > 1:
# pick 2 smallest nodes
left = nodes[0] left = nodes[0]
right = nodes[1] right = nodes[1]
# assign directional value to these nodes
left.huff = 0 left.huff = 0
right.huff = 1 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) 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(left)
nodes.remove(right) nodes.remove(right)
nodes.append(newNode) nodes.append(newNode)
# Huffman Tree is ready!
printNodes(nodes[0]) printNodes(nodes[0])
bit_string = '' bit_string = ''
for i in all_strings: for i in all_strings:
a = [a for a in rep_tree_arr if a.symb == i] a = [a for a in rep_tree_arr if a.symb == i]
bit_string += a[0].val bit_string += a[0].val
......
...@@ -101,6 +101,7 @@ for i in range(0,len(lz_code),6): ...@@ -101,6 +101,7 @@ for i in range(0,len(lz_code),6):
elif k not in string.printable: elif k not in string.printable:
print('extende') print('extende')
counter +=1 counter +=1
print(counter)
with open('./hm_uncomp', 'a') as file: with open('./hm_uncomp', 'a') as file:
file.write(huff_string[:value-counter]) file.write(huff_string[:value-counter])
huff_string = 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