首页 > 编程语言 > 详细

Python实现Tire树

时间:2021-05-25 22:24:49      阅读:15      评论:0      收藏:0      [点我收藏+]
class Project(object):
    def __init__(self):
        self.node = {}
        self.end_char = #
    def insert(self, word):
        node = self.node
        for char in word:
            node = node.setdefault(char, {})
        node[self.end_char] = self.end_char
    def search(self, word):
        node = self.node
        for char in word:
            if char not in node:
                return False
            node = node[char]
        return self.end_char in node
    def startWith(self, prefix):
        node = self.node
        for char in prefix:
            if char not in node:
                return False
            node = node[char]
        return True

if __name__ == __main__:
    t = Project()
    t.insert(xuexi)
    t.insert(duexi)
    t.insert(xia)
    print(t.search(xuexi))
    print(t.startWith(zhao))

 

Python实现Tire树

原文:https://www.cnblogs.com/demo-deng/p/14810333.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!