i am working on a school project and this is my question:
Generate a binary file that contains the table of encoding and the data of the file using the Huffman encoding.
First i need to read the data from a file and create a Huffman tree so i created it and it is all working, but i am not able to generate the binary file because the data i have are nodes not objects so i cannot put the data in the binary file and i am getting this error:
TypeError: a bytes-like object is required, not 'node'
q = {}
a_file = open("george.txt", 'r')
for line in a_file:
key, value = line.split()
q[key] = value
class node:
def __init__(self, freq, symbol, left=None, right=None):
self.freq = freq
self.symbol = symbol
self.left = left
self.right = right
self.huff = ''
def printNodes(node, val=''):
newVal = val + str(node.huff)
if(node.left):
printNodes(node.left, newVal)
if(node.right):
printNodes(node.right, newVal)
if(not node.left and not node.right):
print(f"{node.symbol} -> {newVal}")
chars = ['a', 'b', 'c', 'd', 'e', 'f']
# frequency of characters
freq = [q['a'], q['b'], q['c'], q['d'], q['e'], q['f']]
nodes = []
for x in range(len(chars)):
nodes.append(node(freq[x], chars[x]))
while len(nodes) > 1:
nodes = sorted(nodes, key=lambda x: x.freq)
left = nodes[0]
right = nodes[1]
left.huff = 0
right.huff = 1
newNode = node(left.freq+right.freq, left.symbol+right.symbol, left, right)
nodes.remove(left)
nodes.remove(right)
nodes.append(newNode)
printNodes(nodes[0])
with open('binary.bin', 'wb') as f:
f.write(nodes[0])