python版本 字典树的构造和测试

最近在看中文分词基数, 其中涉及到了字典树的知识, 研究了一下:

class Trie(object):
    class Node:
        def __init__(self):
            self.is_word = False  # 是否一个单词的结尾
            self.dict = dict()

    def __init__(self):
        self.root = Trie.Node()

    def insert(self, word):
        node = self.root
        for c in word:
            if c not in node.dict:
                node.dict[c] = Trie.Node()
            node = node.dict[c]
        node.is_word = True


    def search(self, word):
        node = self.root
        for c in word:
            if c not in node.dict:
                return False
            else:
                node = node.dict[c]
        return node.is_word

    def startsWith(self, prefix):
        node = self.root
        for c in prefix:
            if c not in node.dict:
                return False
            else:
                node = node.dict[c]
        return True

测试:

trie= Trie()

trie.insert("雷峰")
trie.insert("雷峰塔")
 
print(trie.search("雷"))
print(trie.search("雷峰"))
print(trie.search("雷峰塔"))

# 结果
False
True
True

参考 https://blog.csdn.net/qq_31456593/article/details/105212366#comments_12986905

你可能感兴趣的:(数据结构,python,nlp,字典树)