class Trie:
root = {}
END = '/'
def add(self, word):
# 从根节点遍历单词,char by char,如果不存在则新增,最后加上一个单词结束标志
node = self.root
for c in word:
node = node.setdefault(c, {})
node[self.END] = None
def find(self, word):
node = self.root
for c in word:
if c not in node:
return False
node = node[c]
return self.END in node
trie 树
最新推荐文章于 2020-01-29 16:17:36 发布