-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.py
61 lines (52 loc) · 1.53 KB
/
Trie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import random
class TrieNode():
def __init__(self, char: str):
self.char = char
self.children = [] # child nodes
self.word_end = False # if the word ends at this node
self.words = set() # words in and below this node
def add(root, word: str):
node = root
for char in word:
found = False
for child in node.children:
if child.char == char:
child.words.add(word)
node = child
found = True
break
if not found:
new_node = TrieNode(char)
new_node.words.add(word)
node.children.append(new_node)
node = new_node
node.word_end = True
def find_prefix(root, prefix: str):
node = root
if not root.children:
return False, set()
for char in prefix:
char_not_found = True
for child in node.children:
if child.char == char:
char_not_found = False
node = child
break
if char_not_found:
return False, set()
return True, node.words
def database_to_trie(database):
root = TrieNode('*')
for word in database:
add(root, word)
return root
def generate_name(n):
name = ""
for i in range(n):
name += chr(random.randint(97,122))
return name
def generate_database(N):
database = []
for i in range(N):
database.append(generate_name(random.randint(4,8)))
return database