trie,又称前缀树或字典树. 它利用字符串的公共前缀来节约存储空间.

实现
虽然python没有指针,但是可以用嵌套字典来实现树结构.对于非ascii的单词,统一用unicode编码来插入与搜索.
#coding=utf-8
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
原文地址:http://blog.csdn.net/handsomekang/article/details/41446319