Trie
一 普通字典树
208 1, 实现字典树
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class TrieNode { // Initialize your data structure here. private TrieNode[] children; public boolean hasWord; public TrieNode() { children = new TrieNode[26]; hasWord = false; } public void insert(String word, int index){ if (index == word.length()){ this.hasWord = true; return; } int pos = word.charAt(index) - 'a'; if (children[pos] == null) { children[pos] = new TrieNode(); } children[pos].insert(word, index + 1); } public TrieNode find(String word, int index){ if (index == word.length()){ return this; } int pos = word.charAt(index) - 'a'; if(children[pos] == null){ return null; } return children[pos].find(word, index + 1); } } public class Trie { private TrieNode root; public Trie() { root = new TrieNode(); } // Inserts a word into the trie. public void insert(String word) { root.insert(word, 0); } // Returns if the word is in the trie. public boolean search(String word) { TrieNode node = root.find(word, 0); return (node != null && node.hasWord); } // Returns if there is any word in the trie // that starts with the given prefix. public boolean startsWith(String prefix) { TrieNode node = root.find(prefix, 0); return node != null; } }
211 2 单词的添加和查找
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class WordDictionary { private TrieNode root = new TrieNode(); // Adds a word into the data structure. public void addWord(String word) { // Write your code here TrieNode node = root; for (int i = 0; i < word.length(); i++){ char c = word.charAt(i); if (node.children[c - 'a'] == null){ node.children[c - 'a'] = new TrieNode(); } node = node.children[c - 'a']; } node.hasWord = true; } // Returns if the word is in the data structure. A word could // contain the dot character '.' to represent any one letter. public boolean search(String word) { // Write your code here return find(word, 0, root); } public boolean find(String word, int index, TrieNode now){ if (index == word.length()){ return now.hasWord; } char c = word.charAt(index); if (c == '.'){ for (int i = 0; i < 26; i++){ if (now.children[i] != null){ if (find(word, index + 1, now.children[i])){ return true; } } } return false; } else if (now.children[c - 'a'] != null){ return find(word, index + 1, now.children[c - 'a']); } else { return false; } } } class TrieNode{ public TrieNode[] children; public boolean hasWord; public TrieNode(){ children = new TrieNode[26]; hasWord = false; } }
212 3 字典树的应用 单词搜索2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Solution { Set<String> res = new HashSet<>(); public List<String> findWords(char[][] board, String[] words) { Trie trie = new Trie(); for (String str : words) { trie.insert(str); } int m = board.length, n = board[0].length; boolean[][] visited = new boolean[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { dfs(board, visited, "", i, j, trie); } } return new ArrayList<String>(res); } public void dfs(char[][] board, boolean[][] visited, int i, int j, String item, Trie trie) { if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) return; if (visited[i][j]) return; item += board[i][j]; if (!trie.startsWith(item)) return; if (trie.search(item)) res.add(item); visited[i][j] = true; dfs(board, visited, i, j - 1, item, trie); dfs(board, visited, i, j + 1, item, trie); dfs(board, visited, i + 1, j, item, trie); dfs(board, visited, i - 1, j, item, trie); visited[i][j] = false; } }
二 普通字典树的优化
字典树 后缀树 Patricia critbit
Ternary
三 字典树的应用
词汇搜索,词频统计等字符串操作是搜索引擎和文本处理经常用到的业务,字典树对于查找的新能由于 枚举 二叉查找树,和哈希表查找