-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathtrie.py
46 lines (45 loc) · 907 Bytes
/
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
class Trienode:
def __init__(self):
self.ch=[None]*26
self.endofword=None
class Tries:
def __init__(self):
self.head=None
def findkey(self,x):
if ord(x)>=97 and ord(x)<=122:
return ord(x)-97
if ord(x)>=65 and ord(x)<=90:
return ord(x)-65
def add(self,s):
if self.head==None:
self.head=Trienode()
tmp=self.head.ch
for i in range(len(s)):
key=self.findkey(s[i])
if tmp[key]==None:
tmp[key]=Trienode()
if i!=len(s)-1:
tmp=tmp[key].ch
else:
tmp[key].endofword='#'
def search(self,s):
if self.head==None:
print("Not Found")
else:
tmp=self.head.ch
for i in range(len(s)):
key=self.findkey(s[i])
if tmp[key]==None:
print("Not found")
return
else:
tempo=tmp
tmp=tmp[key].ch
if tempo[key].endofword=='#':
print("FOUND")
else:
print("Not Found")
t=Tries()
t.add("apple")
t.add("ball")
t.search("apple")