Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Lief:
- def __init__(self, e, is_end):
- self.e = e
- self.children = {}
- self.is_end = is_end
- def add_child(self, e):
- self.children['e'] = Node(e)
- def get_child(self, e):
- return self.children['e']
- def add_word(self, word):
- if word[0] not in self.children:
- new_lief = Lief(word[0], False)
- if len(word) > 1:
- new_lief.add_word(word[1:])
- else:
- new_lief.is_end = True
- self.children[word[0]] = new_lief
- else:
- self.children[word[0]].add_word(word[1:])
- def search_word(self, word):
- if word[0] == self.e:
- if len(word) == 1 and self.is_end:
- return True
- elif len(word) > 1 and word[0] in self.children:
- return self.children[word[0]].search_word(word[1:])
- else:
- else:
- return False
- class Trie:
- def __init__(self):
- self.heads = {}
- def add_word(self, word):
- if word[0] not in self.heads:
- new_lief = Lief(word[0], False)
- if len(word) > 1:
- new_lief.add_word(word[1:])
- else:
- new_lief.is_end = True
- self.heads[word[0]] = new_lief
- else:
- self.heads[word[0]].add_word(word[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement