小编xev*_*aaa的帖子

如何为霍夫曼编码和解码创建树?

对于我的任务,我要对霍夫曼树进行编码和解码.我在创建树时遇到问题,而且卡住了.

不要介意打印语句 - 它们只是供我测试并查看函数运行时的输出.

对于第一个for循环,我从我在主块中用于测试的文本文件中获取了所有值和索引.

在第二个for循环中,我将所有内容插入优先级队列.

我对下一步去哪里感到困惑 - 我正在尝试制作节点,但我对如何进展感到困惑.有人能告诉我,如果我这样做的话吗?

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 …
Run Code Online (Sandbox Code Playgroud)

python tree encode huffman-code

9
推荐指数
3
解决办法
4万
查看次数

标签 统计

encode ×1

huffman-code ×1

python ×1

tree ×1