TypeError:'Node'和'Node'的实例之间不支持'<'吗?

时间:2019-11-16 13:40:05

标签: python huffman-code heapq

class Node:
    def __init__(self, weight_, ch_, code_):
        self.weight = weight_
        self.ch = ch_
        self.code = code_
        self.left = None
        self.right = None
        self.parent = None

    def set_left(self, node):
        self.left = node
        node.parent = self
    def set_right(self, node):
        self.right = node
        node.parent = self

    def __cmp__(self, other): 
        if self.weight < other.weight:
            return -1
        elif self.weight > other.weight:
            return 1
        else:
            return 0

    def is_leaf(self):

        return self.left is None and self.right is None
    def __repr__(self):
        return "A node with {} weight that holds character {}".format(self.weight, self.ch)


list = []

for x,y in frekans.items():
    list.append( Node(x, y, "") ) 
pprint(list)

heapq.heapify(list) 
pprint(list)

0 个答案:

没有答案