我將Trie編程為python中的類。搜索和插入功能很明確,但是現(xiàn)在我嘗試對python函數(shù)進(jìn)行編程__str__,以便可以在屏幕上打印它。但是我的功能不起作用!class Trie(object): def __init__(self): self.children = {} self.val = None def __str__(self): s = '' if self.children == {}: return ' | ' for i in self.children: s = s + i + self.children[i].__str__() return s def insert(self, key, val): if not key: self.val = val return elif key[0] not in self.children: self.children[key[0]] = Trie() self.children[key[0]].insert(key[1:], val)現(xiàn)在,如果我創(chuàng)建一個Trie對象:tr = Trie()tr.insert('hallo', 54)tr.insert('hello', 69)tr.insert('hellas', 99)當(dāng)我現(xiàn)在打印Trie時,會出現(xiàn)以下問題:條目hello和hellas并不完全。print trhallo | ellas | o 我該如何解決這個問題?
添加回答
舉報(bào)
0/150
提交
取消