-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTries.py
49 lines (39 loc) · 897 Bytes
/
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
vclass TrieNode() :
def __init__(self,data = None) :
self.data = data
self.child = [None for i in range(26)]
self.end = False
class Trie() :
def __init__(self) :
self.root = TrieNode()
def insert(self,word) :
temp = self.root
for c in word :
index = ord(c) - ord('a')
if not temp.child[index] :
# print(c)
node = TrieNode(c)
# print(node.data)
temp.child[index] = node
temp = temp.child[index]
temp.end = True
def search(self,word) :
temp = self.root
for c in word :
index = ord(c) - ord('a')
# print(temp.child[index].data)
if not temp.child[index] :
return False
temp = temp.child[index]
return temp.end
def main():
T = Trie()
T.insert('manas')
T.insert('nitk')
T.insert('dsa')
if T.search('mana') :
print("Found")
else :
print("Not Found")
if __name__ == '__main__':
main()