I tried to implement the code from this article.

Roll Your Own Autocomplete Solution Using Tries

<https://archive.ph/RDCs3>

But I can't create the recursive data structure. Just an idea of what to use 
from the standard library would help.
    
    
     Trie(object):
        def __init__(self, value=None):
            self.children = {}
            self.value = value
            self.flag = False # Flag to indicate that a word ends at this node
        
        def add(self, char):
            val = self.value + char if self.value else char
            self.children[char] = Trie(val)
        
        def insert(self, word):
            node = self
            for char in word:
                if char not in node.children:
                    node.add(char)
                node = node.children[char]
            node.flag = True
        
        def find(self, word):
            node = self
            for char in word:
                if char not in node.children:
                    return None
                node = node.children[char]
            return node.value
    
    
    Run
    
    
     Trie(object):
        ...
        def all_prefixes(self):
            results = set()
            if self.flag:
                results.add(self.value)
            if not self.children: return results
            return reduce(lambda a, b: a | b,
                         [node.all_prefixes() for
                          node in self.children.values()]) | results
        
        def autocomplete(self, prefix):
            node = self
            for char in prefix:
                if char not in node.children:
                    return set()
                node = node.children[char]
            return node.all_prefixes()
    
    
    Run

Reply via email to