我正在编写代码将单词插入到 trie 数据结构中,然后搜索单词。我收到行 self = self.trieDict[word[0]] 无效的语法错误(插入函数中的第三行)
#Trie data structure
class TrieNode():
trieDict = {}
isComplete = False
def __init__(self, dic, isComplete):
self.trieDict = dic
self.isComplete = isComplete
#self is the root node
def insert(self, word):
while len(word) != 0 and self is not None:
if word[0] in self.trieDict:
self = self.trieDict[word[0]]
word = word[1:]
else:
child = self.TrieNode({}, False)
self.trieDict[word[0]] = child
self = child
word = word[1:]
self.isComplete = True
def search(self, word):
while len(word) != 0 and self is not None:
if word[0] in self.trieDict:
word = word[1:]
self = self.trieDict[word[0]]
else:
return False
return self.isComplete
一只萌萌小番薯
大话西游666
相关分类