-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path208_implement_trie.py
105 lines (98 loc) · 2.65 KB
/
208_implement_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
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
class TrieNode(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.word = False
self.children = [None for i in range(26)]
class Trie(object):
def __init__(self):
self.root = TrieNode()
def insert(self, word):
"""
Inserts a word into the trie.
:type word: str
:rtype: void
"""
trav = self.root
for c in word:
index = ord(c) - ord('a')
if trav.children[index] == None:
trav.children[index] = TrieNode()
trav = trav.children[index]
else:
trav = trav.children[index]
trav.word = True
def search(self, word):
"""
Returns if the word is in the trie.
:type word: str
:rtype: bool
"""
trav = self.root
for c in word:
index = ord(c) - ord('a')
if trav.children[index] != None:
trav = trav.children[index]
else:
return False
if trav.word:
return True
return False
def startsWith(self, prefix):
"""
Returns if there is any word in the trie
that starts with the given prefix.
:type prefix: str
:rtype: bool
"""
trav = self.root
for c in prefix:
index = ord(c) - ord('a')
if trav.children[index] != None:
trav = trav.children[index]
else:
return False
return True
def suggest(self, prefix):
"""
Prints all words possible given a prefix
"""
trav = self.root
for c in prefix:
index = ord(c) - ord('a')
if trav.children[index] != None:
trav = trav.children[index]
else:
print "prefix not found"
return self.traverse(trav, prefix)
def traverse(self,trav, prefix):
if trav.word:
print prefix
for i , node in enumerate(trav.children):
if node != None:
self.traverse(node, prefix+chr(i+ord('a')))
return
trie = Trie()
trie.insert("ab")
trie.insert("ac")
trie.insert("ca")
trie.insert("abacus")
trie.insert("ad")
trie.insert("azhar")
trie.insert("aksar")
trie.insert("jacob")
trie.insert("jake")
trie.insert("judy")
trie.insert("jennifer")
print "a found" , trie.search("a")
print "startsWith" , trie.startsWith("a")
print "----"
print "stuff starting with ab"
trie.suggest("ab")
print "----"
print "friends with J"
trie.suggest("j")
print "----"
print "friends starting with jen"
trie.suggest("jen")