Getting a list of words from a Trie

后端 未结 10 982
野性不改
野性不改 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条回答
  •  旧时难觅i
    2021-01-02 15:51

    After your for loop, add a call to printAllStringsInTrie(current, s);

    void printAllStringsInTrie(Node t, String prefix) {
      if (t.current_marker) System.out.println(prefix);
      for (int i = 0; i < t.child.length; i++) {
        if (t.child[i] != null) {
          printAllStringsInTrie(t.child[i], prefix + ('a' + i));  // does + work on (String, char)?
        }
      }
    }
    

提交回复
热议问题