美文网首页
Trie板子 -- Java版

Trie板子 -- Java版

作者: Skymiles | 来源:发表于2021-06-07 17:29 被阅读0次

DFS版本

class Trie {
    public class TrieNode {
        boolean isWord;
        TrieNode[] children;
        public TrieNode() {
            isWord = false;
            children = new TrieNode[26];
        }
    }
    TrieNode root = null;
    /** Initialize your data structure here. */
    public Trie() {
        root = new TrieNode();
    }
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        dfsInsert(root, word, 0);
    }
    private void dfsInsert(TrieNode node, String word, int idxOfWord) {
        if (idxOfWord == word.length() || node == null) {
            node.isWord = true;
            return;
        }
        char ch = word.charAt(idxOfWord);
        if (node.children[ch - 'a'] == null) {
            node.children[ch - 'a'] = new TrieNode();
        }
        dfsInsert(node.children[ch - 'a'], word, idxOfWord + 1);
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
        return dfsSearch(root, word, 0);
    }
    private boolean dfsSearch(TrieNode node, String word, int idxOfWord) {
        if (idxOfWord == word.length()) {
            return node.isWord;
        }
        char ch = word.charAt(idxOfWord);
        if (node.children[ch - 'a'] == null) {
            return false;
        }
        return dfsSearch(node.children[ch - 'a'], word, idxOfWord + 1);
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        return dfsStartsWith(root, prefix, 0);
    }
    private boolean dfsStartsWith(TrieNode node, String word, int idxOfWord) {
        if (idxOfWord == word.length()) {
            return true;
        }
        char ch = word.charAt(idxOfWord);
        if (node.children[ch - 'a'] == null) {
            return false;
        }
        return dfsStartsWith(node.children[ch - 'a'], word, idxOfWord + 1); 
    }
}
/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */

迭代版本

class Trie {
    class TrieNode {
        boolean isWord;
        TrieNode[] children;
        public TrieNode() {
            isWord = false;
            children = new TrieNode[26];
        }
    }
    TrieNode root = null;
    /** Initialize your data structure here. */
    public Trie() {
        root = new TrieNode();
    }
    
    /** Inserts a word into the trie. */
    public void insert(String word) {
        TrieNode node = root;
        for (int i = 0; i < word.length(); i++) {
            char ch = word.charAt(i);
            if (node.children[ch - 'a'] == null) {
                node.children[ch - 'a'] = new TrieNode();
            }
            node = node.children[ch - 'a'];
        }
        node.isWord = true;
    }
    
    /** Returns if the word is in the trie. */
    public boolean search(String word) {
         TrieNode node = root;
         for (int i = 0; i < word.length(); i++) {
            char ch = word.charAt(i);
            if (node.children[ch - 'a'] == null) {
                return false;
            }
            node = node.children[ch - 'a'];
         }
         return node.isWord;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    public boolean startsWith(String prefix) {
        TrieNode node = root;
         for (int i = 0; i < prefix.length(); i++) {
            char ch = prefix.charAt(i);
            if (node.children[ch - 'a'] == null) {
                return false;
            }
            node = node.children[ch - 'a'];
         }
         return true;
    }
}
/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */

相关文章

  • Trie板子 -- Java版

    DFS版本 迭代版本

  • 212 Word Search II

    212Word Search II15.1%Hard dfs+trie自定义一个简化版的trie,只需要adddf...

  • 蒙版

    蒙版最原始的意思是蒙在上面的板子,蒙上板子后有种朦胧的感觉。现在主要指的是ps中蒙版技术,即图层蒙版、剪贴蒙版、矢...

  • Trie的Java实现

    简单实现了一个具有CRUD操作能力的Trie。CRUD操作即插入(Create),读取(Read),改变(Upda...

  • 强化二 字典树 Trie

    Trie 的考点 实现一个 Trie 比较 Trie 和 Hash 的优劣 字符矩阵类问题使用 Trie 比 Ha...

  • 认识JAVA

    Java语言版本: Java SE:标准版Java EE:企业版Java ME:微缩版 Java语言的特点: 跨平...

  • Leetcode-208Implement Trie (Pref

    208. Implement Trie (Prefix Tree) Implement a trie with i...

  • 208. Implement Trie (Prefix Tree

    题目208. Implement Trie (Prefix Tree) Implement a trie with...

  • java基础知识回顾整理(一)

    java语言版本 java SE -标准版 java EE -企业版 javaME-微缩版 java语言的特点 -...

  • 力扣 208 实现 Trie (前缀树)

    题意:实现Trie 思路:利用Trie的数据结构实现插入,查询,和prefix,具体见代码 思想:Trie 复杂度...

网友评论

      本文标题:Trie板子 -- Java版

      本文链接:https://www.haomeiwen.com/subject/edxfeltx.html