-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.py
executable file
·73 lines (54 loc) · 1.62 KB
/
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
import sys
import re
class TrieNode():
def __init__(self):
self.children = {}
self.last = False
class Trie():
def __init__(self):
self.root = TrieNode()
self.word_list = []
def formTrie(self, keys):
for key in keys:
self.insert(key) # inserting one key to the trie.
def insert(self, key):
node = self.root
for a in list(key):
if not node.children.get(a):
node.children[a] = TrieNode()
node = node.children[a]
node.last = True
def search(self, key):
node = self.root
found = True
for a in list(key):
if not node.children.get(a):
found = False
break
node = node.children[a]
return node and node.last and found
def suggestionsRec(self, node, word):
if node.last:
self.word_list.append(word)
for a,n in node.children.items():
self.suggestionsRec(n, word + a)
def printAutoSuggestions(self, key):
node = self.root
not_found = False
temp_word = ''
L = []
for a in list(key):
if not node.children.get(a):
not_found = True
break
temp_word += a
node = node.children[a]
if temp_word != key:
not_found = True
if not_found:
return L
else:
self.suggestionsRec(node, temp_word)
for s in self.word_list:
L.append(s)
return L