Getting a list of words from a Trie

后端 未结 10 987
野性不改
野性不改 2021-01-02 15:29

I\'m looking to use the following code to not check whether there is a word matching in the Trie but to return a list all words beginning with the prefix inputted by the use

10条回答
  •  日久生厌
    2021-01-02 15:50

    You need to traverse the sub-tree starting at the node you found for the prefix.

    Start in the same way, i.e. finding the correct node. Then, instead of checking its marker, traverse that tree (i.e. go over all its descendants; a DFS is a good way to do it) , saving the substring used to reach the "current" node from the first node.

    If the current node is marked as a word, output* the prefix + substring reached.

    * or add it to a list or something.

提交回复
热议问题