【问题标题】:Using Recursion to add to a trie使用递归添加到 trie
【发布时间】:2020-05-28 20:47:11
【问题描述】:

我一直在通过python学习Trie结构。与其他尝试相比,他的 trie 有点不同的是,我们正在尝试在 trie 的每个节点中实现一个计数器,以进行自动完成(这是项目的最终希望)。到目前为止,我认为使用递归函数将字母放入字典列表是一个好主意。

最终产品(特里):

Trie = {"value":"*start"
        "count":1
        "children":["value":"t"
                    "count":1
                    "children":["value":"e"
                                "count":1
                                "children":[...]

我知道递归会非常有用,因为它只是在列表中添加字母,但是,我不知道如何构造基本函数以及如何告诉计算机引用最后一部分字典不写出来

Trie["children"]["children"]["children"]["children"]

很多次。你们能给我一些关于如何构造函数的想法吗?

--谢谢

【问题讨论】:

    标签: python-3.x autocomplete trie


    【解决方案1】:

    也许您只想在词尾节点中存储一个计数器。 (我总是为此使用一个单独的节点!)。因此,给定一个前缀,您沿着 Trie 向下工作,然后执行遍历以找到前缀下方的词尾节点,并通过它们的计数器值将它们推入二叉树。一旦你有了所有的词尾节点,你就执行一个后序遍历来收集具有最大计数器的词尾节点——不管你决定了多少。使用这些收集的每个词尾节点,您可以获得最有可能适合前缀的候选词列表!从词尾节点开始,并在每个节点中存储父节点,您可以放大到根以重新组合词!

    【讨论】:

      【解决方案2】:

      Trie 本身包含根节点和插入/查找函数

      class Trie:
          def __init__(self):
               ## Initialize this Trie (add a root node)
               self.root = TrieNode()
      
          def insert(self, word):
               ## Add a word to the Trie
               current_node = self.root
              for char in word:
                  if char not in current_node.children:
                       current_node.insert(char)
                  current_node = current_node.children[char]
              current_node.is_word = True
          
          def find(self, prefix):
              ## Find the Trie node that represents this prefix
              current_node = self.root
              for char in prefix:
                   if char not in current_node.children:
                       return None
                   current_node = current_node.children[char]
                   return current_node
      

      这是一个用trie实现自动补全的类方法。

      class TrieNode:
          def __init__(self):
              ## Initialize this node in the Trie
              self.is_word = False
              self.children = {}
              self.results = []
      
          def insert(self, char):
               ## Add a child node in this Trie
               self.children[char] = TrieNode()
          
          def suffixes(self, suffix=''):
              ## Recursive function that collects the suffix for 
              ## all complete words below this point
              for char, node in self.children.items():
                  if node.is_word:
                       self.results.append(suffix + char)
                  if node.children:
                        self.results += node.suffixes(suffix + char)
              results = self.results.copy()
              self.results = []
              return list(set(results))
      

      可以使用以下命令获得解决方案。

      MyTrie = Trie()
      wordList = ["ant", "anthology", "antagonist", "antonym", 
          "fun", "function", "factory", "trie", "trigger", "trigonometry", "tripod"]
      for word in wordList:
          MyTrie.insert(word)
      
      MyTrie.find('a').suffixes()
      MyTrie.find('f').suffixes()
      MyTrie.find('t').suffixes()
      MyTrie.find('tri').suffixes()
      MyTrie.find('anto').suffixes()
      

      【讨论】:

        猜你喜欢
        • 2013-01-01
        • 2016-05-31
        • 2023-02-02
        • 1970-01-01
        • 2019-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多