class Node(object):
def __init__(self, value):
self._children = {}
self._value = value
def _add_child(self, char, value, overwirte = False):
child = self._children.get(char)
if child is None:
child = Node(value)
self._children[char] = child
elif overwirte:
child._value = value
return child
class Trie(Node):
def __init__(self):
super().__init__(None)
def __contain__(self, key):
return self[key] is not None
def __getitem__(self, key):
state = self
for char in key:
state = state._children.get(char)
if state is None:
return None
return state._value
def __setitem__(self, key, value):
state = self
for i, char in enumerate(key):
if i < len(key) - 1:
state = state._add_child(char, None, False)
else:
state = state._add_child(char, value, True)
if __name__ == '__main__':
trie = Trie()
trie['自然'] = 'Nature'
trie['自然人'] = 'human'
trie['自然语言'] = 'language'
trie['自然'] = 'talk to me'
trie['入门'] = 'introduction'
assert trie['入门'] == 'introduction'
字典树
最新推荐文章于 2023-03-30 11:14:47 发布