Heapify, bad syntax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Traclo
    New Member
    • Oct 2007
    • 12

    Heapify, bad syntax

    With this code I'm trying to create a huffman encoding tree, to compress text. But heapify just won't work for me :(

    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
    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.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by Traclo
    With this code I'm trying to create a huffman encoding tree, to compress text. But heapify just won't work for me :(

    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
    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.
    Try adding a closed parenthesis.[code=Python]listofNodes.app end(Node(weight = 1, item = 'eof'))[/code]

    Comment

    • Traclo
      New Member
      • Oct 2007
      • 12

      #3
      Well this is embarrassing.

      Comment

      Working...