With this code I'm trying to create a huffman encoding tree, to compress text. But heapify just won't work for me :(
For some reason the line "heapify(listof Nodes)" says it has bad syntax. And for the life of me I just can't figure out what it is. The rest of the function seems fine (I know I can reduce length by getting rid of variables but this is a working copy to get the ideas clear in my head), except for that one line.
I'm on windows vista if thats important, with the latest python.
Help would be appreciated and thank you.
p.s. One interesting thing is if I comment out the line "listofNodes.ap pend(Node(weigh t = 1, item = 'eof')" then the syntax is fixed. But I don't know how to get both lines of code to work still.
Code:
def create_tree(dict): listofNodes = [] for element in dict.items(): newNode = Node(weight = element[1], item = element[0]) listofNodes.append(newNode) listofNodes.append(Node(weight = 1, item = 'eof') heapify(listofNodes) while len(listofNodes) > 1 : x = heappop(listofNodes) y = heappop(listofNodes) weight1 = x.weight weight2 = y.weight newnode = Node(weight = weight1+weight2, item = None) heappush(listofNodes, newnode) return listofNodes
I'm on windows vista if thats important, with the latest python.
Help would be appreciated and thank you.
p.s. One interesting thing is if I comment out the line "listofNodes.ap pend(Node(weigh t = 1, item = 'eof')" then the syntax is fixed. But I don't know how to get both lines of code to work still.
Comment