单词查找树-python

class Trie:
def init(self):
self.root = {}
self.end_of_word = ‘#’

def insert(self,word:str):
    node = self.root
    for char in word:
        node = node.setdefault(char,{})
    node[self.end_of_word]=self.end_of_word

def search(self,word):
    node = self.root
    for char in word:
        if char not in node:
            return False
        node = node[char]
    return self.end_of_word in node

def startsWith(self,prefix:str):
    node = self.node
    for char in prefix:
        if char not in node:
            return False
        node = node[char]
    return True

你可能感兴趣的:(Python,算法)