-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.py
38 lines (32 loc) · 1.02 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
class TrieNode:
def __init__(self):
self.children = {}
self.endOfWord = False
self.images = []
class Trie:
def __init__(self, children=None):
self.root = TrieNode()
if children is not None:
self.root.children = children
def insert(self, word, image):
node = self.root
for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
node.endOfWord = True
node.images.append(image)
def search(self, prefix):
node = self.root
for char in prefix:
if char not in node.children:
return []
node = node.children[char]
return self._collect_images(node)
def _collect_images(self, node):
images = []
if node.endOfWord:
images.extend(node.images)
for child in node.children.values():
images.extend(self._collect_images(child))
return images