How can I create a tree for Huffman encoding and decoding?

前端 未结 5 2020
轮回少年
轮回少年 2021-02-05 21:04

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

5条回答
  •  轮回少年
    2021-02-05 21:18

    @Dave walk_tree is missing tree processing code

    # Recursively walk the tree down to the leaves,
    # assigning a code value to each symbol
    def walk_tree(node, prefix="", code={}):
        if isinstance(node[1].left[1], HuffmanNode):
            walk_tree(node[1].left,prefix+"0", code)
        else:
            code[node[1].left[1]]=prefix+"0"
        if isinstance(node[1].right[1],HuffmanNode):
            walk_tree(node[1].right,prefix+"1", code)
        else:
            code[node[1].right[1]]=prefix+"1"
        return(code)
    

提交回复
热议问题