关于python的HuffmanCompression编码问题
我出现在HuffmanCompression类中定义了子类Trie并定义了Trie的初始化函数__init__(),但是出现了如下错误:
File “D:/Python/练习/HuffmanTree.py”, line 107, in
hc = HuffmanCompression(s)
File “D:/Python/练习/HuffmanTree.py”, line 51, in init
heapq.heappush(heap, HuffmanCompression.Trie(cnt, char))#入堆操作
TypeError: ‘<’ not supported between instances of ‘Trie’ and ‘Trie’。
这是源代码:
import heapq #导入堆的数据结构
import collections
def get_rate(compressed_binary, uncompressed_bits):#进行二进制编码
return len(compressed_binary) * 100 / uncompressed_bits
class SimpleCompression:
def __init__(self, string):
self.symbols = set(string)
self.bit_len = 1
while 2**self.bit_len < len(self.symbols):
self.bit_len += 1
self.string = string
self.s2b = {}
self.b2s = {}
i = 0
for s in self.symbols:
b = bin(i)[2:]
if len(b) < self.bit_len:
b = (self.bit_len - len(b)) * '0' + b
self.s2b[s] = b #进行编码时存储的二进制数
self.b2s[b] = s #进行解码时存储的字符串
i += 1
def compress(self): #编码
bits = ''
for s in self.string: #装换为二进制
bits += self.s2b[s]
return bits
def uncompress(self, bits):#解码
string = ''
for i in range(0, len(bits), self.bit_len):
string += self.b2s[bits[i:i + self.bit_len]]
return string
class HuffmanCompression:
class Trie: #建立树的结构
def __init__(self, val, char=''):
self.val = val
self.char = char
self.coding = ''
self.left = self.right = None
def __cmp__(self, other):#比较大小
return (self.val - other.val)
def __init__(self, string):
self.string = string
counter = collections.Counter(string)
heap = [] #创建堆
for char, cnt in counter.items():
print(char, cnt)
#词句语句有问题,类型描述指的是Trie和Trie的类型中不含有<操作符
heapq.heappush(heap, HuffmanCompression.Trie(cnt, char))#入堆操作
while len(heap) != 1:
left = heapq.heappop(heap) #出堆
right = heapq.heappop(heap)
trie = HuffmanCompression.Trie(left.val + right.val)
trie.left, trie.right = left, right
heapq.heappush(heap, trie)
self.root = heap[0]
self.s2b = {}
self.bfs_encode(self.root, self.s2b) #广度优先遍历编码
def bfs_encode(self, root, s2b):
queue = collections.deque()
queue.append(root) #向队列中添加编码的字符
while queue:
node = queue.popleft()
if node.char:
s2b[node.char] = node.coding
continue
if node.left:
node.left.coding = node.coding + '0' #编码左节点
queue.append(node.left)
if node.right:
node.left.coding = node.coding + '1' #编码右节点
queue.append(node.right)
def compress(self): #返回压缩后节字节数
bits = ''
for char in self.string:
bits += self.s2b[char]
return bits
def uncompress(self, bits):#未压缩的字节数
string = ''
root = self.root
for bit in bits:
if bit == '0':
root = root.left
else:
root = root.right
if root.char:
string += root.char
root = self.root
return string
if __name__ == "__main__":
s = 'everyday is awesome!'
#ASCII码
bits = len(s)*8
print('Total bits: %d' % bits)
#simple compression
sc = SimpleCompression(s)
compressed = sc.compress()
print("Compressed binary: " + compressed)
print("Uncompressed: " + sc.uncompress(compressed))
print(sc.s2b)
print("Simole Compression-compress rate: %d%%" % get_rate(compressed, bits))
print('==================================')
hc = HuffmanCompression(s)
compressed = hc.compress()
print('Compressed binary: ' + compressed)
print("Uncompressed: " + hc.uncompress(compressed))
print(hc.s2b)
print("Huffman Compression-compress rate: %d%%" % get_rate(compressed, bits))
请大佬救救我!