Trie板子 -- Java版

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版)