For my assignment, I am to do a encode and decode for huffman trees. I have a problem creating my tree, and I am stuck.
Don't mind the print statements - they are just for me to test and see what the output is when my function runs.
For the first for loop, I got all the values and index from the text file I used in my main block for testing.
In the second for loop I inserted all the stuff into the priority queue.
I am so stuck about where to go next - I'm trying to make nodes, but I am confused about how to progress. Can someone tell me if I'm doing this right?
def _create_code(self, frequencies):
'''(HuffmanCoder, sequence(int)) -> NoneType
iterate over index into the sequence keeping it 256 elements long, '''
#fix docstring
p = PriorityQueue()
print frequencies
index = 0
for value in frequencies:
if value != 0:
print value #priority
print index #elm
print '-----------'
index = index + 1
for i in range(len(frequencies)):
if frequencies[i] != 0:
p.insert(i, frequencies[i])
print i,frequencies[i]
if p.is_empty():
a = p.get_min()
b = p.get_min()
n1 = self.HuffmanNode(None, None, a)
n2 = self.HuffmanNode(None, None, b)
print a, b, n1, n2
while not p.is_empty():
p.get_min()
I manually inserted the first two to start my tree, is that correct?
How do I keep going? I know the idea of it, just code-wise I am very stuck.
This is using python by the way. I tried looking at Wikipedia, I know the steps, I just need help on code and how I should keep going, thanks!
The HuffmanNode comes from this nested class:
class HuffmanNode(object):
def __init__(self, left=None, right=None, root=None):
self.left = left
self.right = right
self.root = root