I am working on a problem to develop auto-complete functionality to practice different applications of Tries. The auto-complete function will return all the possible words in the wordlist given a prefix. I came up with the following solution which returns the result as expected. I need some help to review my code, in order to enhance the OOP structure and improve the code quality over all.
Please let me know your thoughts, any feedback is welcome.
class Trie():
def __init__(self):
self.children = {}
self.flag = False # Flag to represent that a word ends at this node
def insert(self, word):
for char in word:
if char not in self.children:
self.children[char]= Trie()
self = self.children[char]
self.flag = True
def all_suffixes(self, prefix):
results = set()
if self.flag:
results.add(prefix)
if not self.children:
return results
return reduce(lambda a, b: a | b, [node.all_suffixes(prefix + char) for (char, node) in self.children.items()])
def autocomplete(self, prefix):
node = self
for char in prefix:
if char not in node.children:
return None
node = node.children[char]
return list(node.all_suffixes(prefix))
if __name__=="__main__":
word_list = ['aardvark','ark', 'altimeter','altitude', 'apotactic', 'bagonet', 'boatlip',
'carburant', 'chyliferous', 'consonance', 'cyclospondylic',
'dictyostele', 'echelon', 'estadal', 'flaunty', 'gesneriaceous',
'hygienic', 'infracentral', 'jipijapa', 'lipoceratous', 'melanthaceae']
c = Trie()
for word in word_list:
c.insert(word)
print c.autocomplete('a')
Output - ['aardvark', 'ark', 'altimeter', 'altitude', 'apotactic']
I was thinking on the lines of having two classes one as Trie()
and the other as Autocomplete()
. The __init__(self,word_list)
for Autocomplete()
can then have the for
loop code snippet to create the Trie from the word list.
|results
was already in the solution that I was originally referring to and for some reason it got left out while posting the question. \$\endgroup\$