-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.py
More file actions
73 lines (57 loc) · 2.22 KB
/
Copy pathtrie.py
File metadata and controls
73 lines (57 loc) · 2.22 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
from tool_box.array import Array # import Array from array.py
from tool_box.sll_queue import Queue # import Queue from sll_queue.py
class TrieNode:
def __init__(self):
self.children = Array(26) # 'a' = 0, 'b' = 1, ..., 'z' = 25
self.is_end_of_word = False
def char_to_index(self, char):
return ord(char) - ord('a')
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word):
curr_node = self.root
for char in word:
index = curr_node.char_to_index(char)
if curr_node.children[index] is None:
curr_node.children[index] = TrieNode()
curr_node = curr_node.children[index]
curr_node.is_end_of_word = True
def search(self, word):
curr_node = self.root
for char in word:
index = curr_node.char_to_index(char)
if curr_node.children[index] is None:
return False
curr_node = curr_node.children[index]
return curr_node.is_end_of_word
def starts_with(self, word):
""" checks if there is any word in the trie that starts with the given prefix """
curr_node = self.root
for char in word:
index = curr_node.char_to_index(char)
if curr_node.children[index] is None:
return False
curr_node = curr_node.children[index]
return True
def print(self, curr_node=None, word=""):
if curr_node is None:
curr_node = self.root
if curr_node.is_end_of_word:
print(" -", word)
for i in range(26):
child_node = curr_node.children[i]
if child_node is not None:
self.print(child_node, word + chr(i + ord('a')))
def get_all_words(self, curr_node=None, prefix="", words=None):
if words is None:
words = Queue()
if curr_node is None:
curr_node = self.root
if curr_node.is_end_of_word:
words.enqueue(prefix)
for i in range(26):
child_node = curr_node.children[i]
if child_node is not None:
self.get_all_words(child_node, prefix + chr(i + ord('a')), words)
return words