I was researching effective ways to store huffman trees in a file to allow a decoder to reconstruct the huffman tree and decode the file properly. Currently I've made a function to create my huffman encoding given frequencies.
I saw Efficient way of storing Huffman tree, and so I wrote a recursive function to mimic the recursive function in the answer of the post:
>>> huffman = [(b'A', '00'), (b'C', '01'), (b'E', '10'), (b'B', '110'), (b'D', '111')]
>>> write(hf)
b'001A1C01E01B1D'
In the post, the answer uses objects to contain node-leafs. However, I wanted to decode the string straight into a list of tuples like in the huffman
variable above. I tried my hand at this, and came up with the following function, which kinda works:
def read(p):
if p[0] == "1":
x = p[1]
p = p[2:]
if len(p) == 0:
c = [(x, "")]
else:
b = read(p)
for a in range(len(b)):
b[a] = (b[a][0], "1" + b[a][1])
c = [(x, "0")] + b
return c
if p[0] == "0":
c = read(p[1:])
return c
The function returns this when called:
>>> read("001C1A01E01D1B")
[('C', '0'), ('A', '10'), ('E', '110'), ('D', '1110'), ('B', '1111')]
This is clearly not the same as the orginial dictionary huffman
above.
How can you recursively decode a huffman tree dictionary, without using a node class, and get the original dictionary?