-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trie_2.0.py
139 lines (114 loc) · 3.83 KB
/
Trie_2.0.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
# Trie 2.0
# -> insert word
# -> search word
# -> remove word
# -> search prefix
# -> count word occurrence
# -> count prefix occurence
# for each operation it takes o(n) time complexity where n is length of word
class Trie_node:
def __init__(self,data=None):
self.data = data
self.children = [None]*26
self.ends_count = 0
self.prefix_count = 0
class Trie:
def __init__(self,root):
self.root = root
def insert_word_util(self,word):
curr = self.root
for i in word:
index = ord(i)-97
if curr.children[index]!=None:
# increment prefix count
curr.children[index].prefix_count+=1
curr = curr.children[index]
else:
new_node = Trie_node(i)
curr.children[index] = new_node
# increment prefix count
new_node.prefix_count+=1
curr = curr.children[index]
# increment the ends count
curr.ends_count+=1
def search_word_util(self,word):
curr = self.root
for i in word:
index = ord(i)-97
# if char present
if curr.children[index]!=None and curr.children[index].prefix_count>0:
curr = curr.children[index]
# if not present
else:
return False
# is last char terminal
return curr.ends_count>0
def search_prefix_util(self,prefix):
curr = self.root
for i in prefix:
index = ord(i)-97
# if char present
if curr.children[index]!=None and curr.children[index].prefix_count>0:
curr = curr.children[index]
# if not present
else:
return False
# is last char terminal
return True
def count_word_occ_util(self,word):
if not self.search_word(word):
return 0
curr = self.root
for i in word:
index = ord(i)-97
curr = curr.children[index]
# return last char terminal ends with count
return curr.ends_count
def count_prefix_occ_util(self,prefix):
if not self.search_prefix(prefix):
return 0
curr = self.root
for i in prefix:
index = ord(i)-97
curr = curr.children[index]
# return last char terminal ends with count
return curr.prefix_count
def remove_word_util(self,word):
if not self.search_word(word):
print("word not present")
return False
curr = self.root
for i in word:
index = ord(i)-97
node = curr.children[index]
node.prefix_count-=1
curr = node
node.ends_count-=1
return True
# insert word
def insert_word(self,word):
self.insert_word_util(word)
# search word
def search_word(self,word):
return self.search_word_util(word)
# search prefix
def search_prefix(self,prefix):
return self.search_prefix_util(prefix)
# count word occurrence
def count_word_occ(self,word):
return self.count_word_occ_util(word)
# count prefix occurrence
def count_prefix_occ(self,prefix):
return self.count_prefix_occ_util(prefix)
# remove word
def remove_word(self,word):
self.remove_word_util(word)
if __name__ == '__main__':
root = Trie_node("R")
obj = Trie(root)
obj.insert_word("samsung")
obj.insert_word("samsung")
obj.insert_word("vivo")
obj.remove_word("vivo")
print(obj.count_word_occ("samsung"))
print(obj.count_prefix_occ("viv"))