-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtries.py
160 lines (110 loc) · 3.79 KB
/
tries.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
class TrieNode:
def __init__(self, char = ''):
self.children = [None] * 26 # English alphabet chars
self.is_end_word = False
self.char = char
def mark_leaf(self):
self.is_end_word = True
def unmark_leaf(self):
self.is_end_word = False
class Trie:
def __init__(self):
self.root = TrieNode()
def get_index(self, t):
return ord(t) - ord('a')
def insert(self, key):
"""Insert the trie node at the correct index
Set is_end_word to True for last node
Cases: common prefix, no common prefix, word exists
"""
if key == None:
return
key = key.lower()
current = self.root
index = 0
for level in range(len(key)):
index = self.get_index(key[level])
if current.children[index] == None:
current.children[index] = TrieNode()
print(key[level] + ' inserted')
current = current.children[index]
current.mark_leaf()
print(f"'{key}' inserted")
def search(self, key):
"""Trace the path corresponding to the characters in word
Check if is_end_word is True (word must end in leafnode)
Cases: non-existent, word is a substring, word exists
"""
if key == None:
return False
key = key.lower()
current = self.root
index = 0
for level in range(len(key)):
index = self.get_index(key[level])
if current.children[index] == None:
return False
current = current.children[index]
if current != None and current.is_end_word:
print(f'{key} found')
return True
return False
def no_children(self, current):
for i in range(len(current.children)):
if current.children[i] != None:
return Flase
return True
def delete_helper(self, key, current, length, level):
"""Unmark end and remove nodes to root if there are no children
Cases: no suffix/prefix, word is prefix, has common prefix
"""
deleted = False
if current == None:
print('Key does not exist')
return deleted
if level == length:
if self.no_children(current):
current = None
deleted = True
else:
current.unmark_leaf()
deleted = False
else:
child = current.children[self.get_index(key[level])]
child_deleted = self.delete_helper(key, child, length, level + 1)
if child_deleted:
current.children[self.get_index(key[level])] = None
if current.is_end_word:
deleted = False
elif self.no_children(current) == False:
deleted = False
else:
current = None
deleted = True
else:
deleted = False
return deleted
def delete(self, key):
if self.root == None or key == None:
print('None key or empty trie error')
return
self.delete_helper(key, self.root, len(key), 0)
print(f"{key} deleted")
def count_words(self, root):
"""Trace all paths to all leaf nodes
Count nodes with is_end_word as True
"""
count = 0
if root.is_end_word:
count += 1
for i in range(26):
if root.children[i] != None:
count += self.count_words(root.children[i])
return count
trie = Trie()
keys = ['there', 'any', 'the', 'a']
for i in range(len(keys)):
trie.insert(keys[i])
trie.search('any')
trie.delete('any')
print(trie.count_words(trie.root))