使用Trie在文件中搜索前缀和位置

问题描述

使用此特定数据结构困扰的面试问题。我使用两个列表解决了这个问题,但是他们正在寻找Trie的实现。不太熟悉此数据结构。我有创建节点并在单词中插入Trie的基准代码,然后如果包含前缀则返回true或false。我将如何实现不仅返回true,还返回前缀在文档中的位置的功能?因此,如果提供此文档:

Hello,World! Computer Science is a great major if you enjoy the sciences.

prefixPosition(“ science”)将返回{24,66} prefixPosition(“ i”)将返回{32,49}

我最初是通过创建一个列表将文档拆分到“”来实现的。然后遍历该列表,并通过计算列表中的单词,创建第二个包含字符位置的列表,为我们所做的拆分添加1个字符(在空格上)并将其存储。然后遍历“原始”列表,以查看元素是否以我们的前缀开头(如果有的话),返回到字符位置列表并获取其位置。

在使用Trie时,我如何通过某种类型的链表来确定位置?

class TrieNode {
    private final Map<Character,TrieNode> children = new HashMap<>();
    private boolean endOfWord;

    Map<Character,TrieNode> getChildren() {
        return children;
    }

    boolean isEndOfWord() {
        return endOfWord;
    }

    void setEndOfWord(boolean endOfWord) {
        this.endOfWord = endOfWord;
    }
}
class Trie {
    private TrieNode root;

    Trie() {
        root = new TrieNode();
    }

    void insert(String word) {
        TrieNode current = root;

        for (char l : word.tochararray()) {
            current = current.getChildren().computeIfAbsent(l,c -> new TrieNode());
        }
        current.setEndOfWord(true);
    }

    boolean delete(String word) {
        return delete(root,word,0);
    }

    boolean containsNode(String word) {
        TrieNode current = root;

        for (int i = 0; i < word.length(); i++) {
            char ch = word.charat(i);
            TrieNode node = current.getChildren().get(ch);
            if (node == null) {
                return false;
            }
            current = node;
        }
        return current.isEndOfWord();
    }

    boolean isEmpty() {
        return root == null;
    }

    private boolean delete(TrieNode current,String word,int index) {
        if (index == word.length()) {
            if (!current.isEndOfWord()) {
                return false;
            }
            current.setEndOfWord(false);
            return current.getChildren().isEmpty();
        }
        char ch = word.charat(index);
        TrieNode node = current.getChildren().get(ch);
        if (node == null) {
            return false;
        }
        boolean shouldDeleteCurrentNode = delete(node,index + 1) && !node.isEndOfWord();

        if (shouldDeleteCurrentNode) {
            current.getChildren().remove(ch);
            return current.getChildren().isEmpty();
        }
        return false;
    }

    public boolean find(String word) {
        TrieNode current = root;
        for (int i = 0; i < word.length(); i++) {
            char ch = word.charat(i);
            TrieNode node = current.getChildren().get(ch);
            if (node == null) {
                return false;
            }
            current = node;
        }
        return current.isEndOfWord();
    }

    public TrieNode searchNode(String str){
        Map<Character,TrieNode> children = root.getChildren();
        TrieNode t = null;
        for(int i=0; i<str.length(); i++){
            char c = str.charat(i);
            if(children.containsKey(c)){
                t = children.get(c);
                children = t.getChildren();
            }else{
                return null;
            }
        }

        return t;
    }

    public boolean startsWith(String prefix) {
        if(searchNode(prefix) == null)
            return false;
        else
            return true;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)