Skip to content

Commit 69e3c4b

Browse files
Alagappan MaruthappanAlagappan Maruthappan
Alagappan Maruthappan
authored and
Alagappan Maruthappan
committed
Trie implementation
1 parent d3beb2a commit 69e3c4b

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

Diff for: trie.py

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# https://leetcode.com/problems/implement-trie-prefix-tree/
2+
3+
4+
class Node:
5+
def __init__(self, val):
6+
self.val = val
7+
self.children = {}
8+
9+
def add_children(self, val):
10+
if val not in self.children:
11+
self.children[val] = Node(val)
12+
return self.children[val]
13+
14+
15+
class Trie:
16+
17+
def __init__(self):
18+
"""
19+
Initialize your data structure here.
20+
"""
21+
self.root = Node("/")
22+
23+
def insert(self, word):
24+
"""
25+
Inserts a word into the trie.
26+
:type word: str
27+
:rtype: void
28+
"""
29+
def insert_util(word, i, root):
30+
if i == len(word):
31+
root.add_children("$")
32+
return
33+
insert_util(word, i + 1, root.add_children(word[i]))
34+
insert_util(word, 0, self.root)
35+
36+
def search(self, word):
37+
"""
38+
Returns if the word is in the trie.
39+
:type word: str
40+
:rtype: bool
41+
"""
42+
def search_util(word, i, root):
43+
if i == len(word):
44+
return "$" in root.children
45+
return word[i] in root.children and search_util(word, i + 1, root.children[word[i]])
46+
return search_util(word, 0, self.root)
47+
48+
def startsWith(self, prefix):
49+
"""
50+
Returns if there is any word in the trie that starts with the given prefix.
51+
:type prefix: str
52+
:rtype: bool
53+
"""
54+
def search_util(word, i, root):
55+
if i == len(word):
56+
return True
57+
return word[i] in root.children and search_util(word, i + 1, root.children[word[i]])
58+
return search_util(prefix, 0, self.root)
59+
60+
61+
# Your Trie object will be instantiated and called as such:
62+
# obj = Trie()
63+
# obj.insert(word)
64+
# param_2 = obj.search(word)
65+
# param_3 = obj.startsWith(prefix)
66+
67+
class Trie_Iterative:
68+
69+
def __init__(self):
70+
"""
71+
Initialize your data structure here.
72+
"""
73+
self.root = Node("/")
74+
75+
def insert(self, word):
76+
"""
77+
Inserts a word into the trie.
78+
:type word: str
79+
:rtype: void
80+
"""
81+
root = self.root
82+
for i in range(0, len(word)):
83+
root = root.add_children(word[i])
84+
root.add_children("$")
85+
86+
def search(self, word):
87+
"""
88+
Returns if the word is in the trie.
89+
:type word: str
90+
:rtype: bool
91+
"""
92+
root = self.root
93+
ans = True
94+
for i in range(0, len(word)):
95+
ans = word[i] in root.children
96+
if not ans:
97+
break
98+
root = root.children[word[i]]
99+
return ans and "$" in root.children
100+
101+
def startsWith(self, prefix):
102+
"""
103+
Returns if there is any word in the trie that starts with the given prefix.
104+
:type prefix: str
105+
:rtype: bool
106+
"""
107+
root = self.root
108+
ans = True
109+
for i in range(0, len(prefix)):
110+
ans = prefix[i] in root.children
111+
if not ans:
112+
break
113+
root = root.children[prefix[i]]
114+
return ans

0 commit comments

Comments
 (0)