【问题标题】:Get Words out of a Trie Data Structure从 Trie 数据结构中获取单词
【发布时间】:2015-04-29 15:46:53
【问题描述】:

我有以下 Trie 数据结构:

public class CDictionary implements IDictionary {

private static final int N = 'z' -'a'+1;

private static class Node {
    private boolean end = false;
    private Node[] next = new Node[N];
}

private int size = 0;
private Node root = new Node();

@Override
public boolean contains(String word) {
    Node node = this.contains(root,word,0);
    if (node == null) {
        return false;
    }
    return node.end;
}

private Node contains(Node node, String str, int d) {
    if (node == null) return null;
    if (d == str.length()) return node;

    char c = str.charAt(d);
    return contains(node.next[c-'a'], str, d+1);
}

@Override
public void insert(String word) {
    this.root = insert(this.root, word, 0);
    this.size++;
}

private Node insert(Node node, String str, int d) {
    if (node == null) node = new Node();
    if (d == str.length()) {
        node.end = true;
        return node;
    }

    char c = str.charAt(d);
    node.next[c-'a'] = this.insert(node.next[c-'a'], str, d+1);
    return node;
}

@Override
public int size() {
    return size;
}

Trie 中充满了一些词,例如

对于,每个,家,是,它,鸡蛋,红色...

现在我需要一个函数来获取具有特定长度的所有单词,例如长度 3

public List<String> getWords(int lenght) {

}

使用上面提到的单词,它应该返回一个带有单词的列表

为,蛋,红

问题是如何从 Trie 结构中恢复这些单词?

【问题讨论】:

    标签: java search trie


    【解决方案1】:

    您需要通过结构递归到最大深度 N(在本例中为 3)

    您可以通过在字典中添加几个方法来做到这一点...

    public List<String> findWordsOfLength(int length) {
        // Create new empty list for results
        List<String> results = new ArrayList<>();
        // Start at the root node (level 0)...
        findWordsOfLength(root, "", 0, length, results);
        // Return the results
        return results;
    }
    
    public void findWordsOfLength(Node node, String wordSoFar, int depth, int maxDepth, List<String> results) {
        // Go through each "child" of this node
        for(int k = 0; k < node.next.length; k++) {
           Node child = node.next[k];
           // If this child exists...
           if(child != null) {
               // Work out the letter that this child represents
               char letter = 'a' + k;
               // If we have reached "maxDepth" letters...
               if(depth == maxDepth) {
                   // Add this letter to the end of the word so far and then add the word to the results list
                   results.add(wordSoFar + letter);
               } else {
                   // Otherwise recurse to the next level
                   findWordsOfLength(child, wordSoDar + letter, depth + 1, maxDepth, results);
               }
           }
        }
    }
    

    (我还没有编译/测试过这个,但它应该让你知道你需要做什么)

    希望这会有所帮助。

    【讨论】:

    • 事实上,当“3”被传入时,这可能会找到长度为“4”的单词,就像我说的,没有测试过......您可能还需要检查最后一个节点是否是终端(即不要将它添加到结果列表中,除非它的所有子节点都是空的......)
    • 谢谢你这确实有效。我已经解决了字长的问题。我以 1 的深度开始递归方法
    猜你喜欢
    • 2011-02-17
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多