【问题标题】:How to get all possible words?如何获得所有可能的单词?
【发布时间】:2013-04-24 08:16:13
【问题描述】:

我有一个方法可以在前缀树中找到所有可能的单词。它接收一个节点并找到该节点的所有可能单词。 但是,我需要它能够接受节点组合并找到组合的可能单词。 该方法将搜索填充了单词字典的 trie。 例如,与其从一个带有前缀“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 会稍微编辑一下
  • 你能告诉我们Node类的结构是什么吗?
  • @BartoszMarcinkowski 确定我会弹出它

标签: java search trie


【解决方案1】:

我不太了解您的问题,但您所做的似乎首先是错误的,因为一旦您输出换行符,您就会丢失搜索路径的完整前缀。您可能应该将findWords(Node node) 的签名更改为findWords(String prefix, Node node),以保持前缀。我还建议对该方法进行轻微的重组:

public void findWords(String prefix, Node node) { 

    if(node == null) return; 

    if(node.isWord) // Check here
        System.out.println(prefix);

    //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) {
            // append next character to prefix
            findWords(prefix + ('a' + i), node.next[i]);  
        }  
    }  
}

不用说,这是非常低效的。您可能想在递归之前检查 StringBuilder 类、append(char) 和从递归调用返回时检查 removeCharAt(length-1)

【讨论】:

  • 非常感谢,我会调查的。
  • 我只是让节点成为我前缀中的第一个字母吗?
  • 什么意思?您使用初始前缀 "" 调用此方法,它的行为应与您的原始方法的预期相同。
  • 是的,只是解决问题,阅读代码并不快我是新手。再次感谢我应该能够弄清楚我想要做什么。
【解决方案2】:

这对你有用吗?作为Node的一种方法。

public List<String> findWords() {

    List<String> result = new ArrayList<String>();

    if(this.isWord)
        result.add("");

    for(int i = 0; i < R; i++) 
        if(next[i] != null){ 
            List<String> childResult = next[i].findWords(); 
            char letter = (char) (97 + i);
            for(String sufix : childResult)
                result.add("" + letter + sufix);
        }  
    return result;
}

【讨论】:

  • 谢谢,这看起来很有用,我试试看。
  • 如果要打印所有单词,只需这样做: for(String word : root.findWords()) System.out.println(word);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-21
  • 2011-05-16
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
相关资源
最近更新 更多