我有一種方法可以查找前綴樹中所有可能的單詞。它接受一個節點併爲該節點找到所有可能的單詞。 但是,我需要它能夠接受節點的組合並找到可能的單詞組合。 該方法將搜索樹中,該樹填充了單詞詞典。 例如,如果從一個字母中找到所有可能的單詞,並且前綴'a',它可以從前綴'ab'或'abo'中找到所有可能的單詞。 我只是不知道如何使用節點組合而不是僅從一個節點進行搜索。如何獲取所有可能的單詞?
public void findWords(Node node) {
if(node == null) return;
//searches through the branches of the node
// R is 26(the branches from each node on the trie)
// each one being a letter of the alphabet.
for(int i = 0; i < R; i++) {
if(node.next[i] != null) {
//printing each character
System.out.print((char) (97 + i));
//identifying letter combo
if(node.next[i].isWord == true) {
System.out.println();
}
//back into the loop
findWords(node.next[i]);
}
}
}
節點類:
public class TextPredictTrie {
private final int R = 26; // the trie branches
private Node root = new Node(); // the root node
// the t9 mapped array which maps number to string on the typing board
private String[] t9 = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
// trie node definition
private class Node {
private boolean isWord;
private Node[] next;
public Node() {
this(false);
}
public Node(boolean isWord) {
this.isWord = isWord;
this.next = new Node[R];
}
}
什麼是'R'在這裏?你在找什麼樣的單詞?需要更好的信息,你正在努力實現.. – LPD 2013-04-30 12:09:48
@LPD將編輯它 – user1835504 2013-04-30 12:11:03
你能告訴我們什麼是Node類的結構? – 2013-04-30 12:23:29