小编Aus*_*tin的帖子

TypeError:“ HeapNode”和“ HeapNode”的实例之间不支持“ <”

当我尝试将节点推入霍夫曼树的堆时,出现以下错误:

TypeError:“ HeapNode”和“ HeapNode”的实例之间不支持“ <”

    class HuffmanCoding:
        def __init__(self, path):
            self.path = path
            self.heap = []
            self.codes = {}
            self.reverse_mapping = {}

        def make_heap(self, frequency):
            for key in frequency:
                node = HeapNode(key, frequency[key])
                heapq.heappush(self.heap, node)
Run Code Online (Sandbox Code Playgroud)

节点类:

    class HeapNode:
        def __init__(self, char, freq):
            self.char = char
            self.freq = freq
            self.left = None
            self.right = None

        def __cmp__(self, other):
            if(other == None):
                return -1
            if(not isinstance(other, HeapNode)):
                return -1
            return self.freq > other.freq
Run Code Online (Sandbox Code Playgroud)

该错误是由以下原因引起的:

    heapq.heappush(self.heap, node)
Run Code Online (Sandbox Code Playgroud)

完整代码由github.com/bhrigu123提供

python heap huffman-code nodes python-3.x

4
推荐指数
1
解决办法
1452
查看次数

标签 统计

heap ×1

huffman-code ×1

nodes ×1

python ×1

python-3.x ×1