How to create a trie in Python

后端 未结 12 1405
别那么骄傲
别那么骄傲 2020-11-22 12:39

I\'m interested in tries and DAWGs (direct acyclic word graph) and I\'ve been reading a lot about them but I don\'t understand what should the output trie or DAWG file look

12条回答
  •  自闭症患者
    2020-11-22 13:12

    This is much like a previous answer but simpler to read:

    def make_trie(words):
        trie = {}
        for word in words:
            head = trie
            for char in word:
                if char not in head:
                    head[char] = {}
                head = head[char]
            head["_end_"] = "_end_"
        return trie
    

提交回复
热议问题