比如网上一个例子
一组单词,inn, int, at, age, adv, ant, 我们可以得到下面的Trie:
这里的节点上存的是一个单词,实际上,每个节点走过的路径就是该节点代表的单词.
下面这个链接 对Trie tree 比较好的code 模板,针对leetcode 的:
https://www.programcreek.com/2014/05/leetcode-implement-trie-prefix-tree-java/
TrieTree Node 定义有两种形式
1. 如果只包含 a-z的字母
class TrieNode{ char c; TrieNode[] children; boolean isLeaf; TrieNode(){ children = new TrieNode[26]; } }
2. 如果定义个一个通用性的模板:
class TrieNode{ char c; Map<Character, TrieNode> children; boolean isLeaf; TrieNode(){ children = new HashMap<>(); } }
一个完整的Trie Tree 模板, 包含了测试程序, 测试过208. Implement Trie (Prefix Tree)/ 425. Word Squares 这道 backtracking + trie解法的题。
1 // "static void main" must be defined in a public class. 2 public class Main { 3 public static void main(String[] args) { 4 System.out.println("Hello World!"); 5 Trie trie = new Trie(); 6 trie.insert("apple"); 7 trie.insert("appde"); 8 trie.insert("app"); 9 trie.insert("appxxxx"); 10 trie.insert("epe"); 11 // trie.searchAll("apee"); 12 13 System.out.println(trie.searchAll("")); 14 } 15 } 16 17 class Trie { 18 19 /** Initialize your data structure here. */ 20 TrieNode root; 21 public Trie() { 22 root = new TrieNode(); 23 } 24 25 /** Inserts a word into the trie. */ 26 public void insert(String word) { 27 TrieNode p = root; 28 for(int i=0; i<word.length(); i++){ 29 char c = word.charAt(i); 30 if(p.children[c-'a'] != null){ 31 p = p.children[c-'a']; 32 } 33 else { 34 TrieNode node = new TrieNode(c); 35 p.children[c-'a'] = node; 36 p = node; 37 } 38 if(i == word.length()-1){ 39 p.isLeaf = true; 40 } 41 } 42 43 } 44 45 /** Returns if the word is in the trie. */ 46 public boolean search(String word) { 47 TrieNode p = searchNode(word); 48 if(p!=null && p.isLeaf == true) return true; 49 return false; 50 51 } 52 53 /** Returns if there is any word in the trie that starts with the given prefix. */ 54 public boolean startsWith(String prefix) { 55 TrieNode p = searchNode(prefix); 56 return p != null; 57 } 58 59 public List<String> searchAll(String prefix){ 60 TrieNode p = searchNode(prefix); 61 List<String> result = new ArrayList<>(); 62 if(p == null) return result; 63 preOrder(p, result, prefix); 64 return result; 65 } 66 67 private void preOrder(TrieNode root, List<String> result, String curResult){ 68 if(root.isLeaf) { 69 result.add(curResult.toString()); 70 } 71 72 for(int i=0; i<root.children.length; i++){ 73 //curResult.append(root.children[i].c) 74 if(root.children[i] != null){ 75 //curResult.append(root.children[i].c); 76 String tmp = curResult + root.children[i].c ; 77 preOrder(root.children[i],result,tmp); 78 } 79 } 80 } 81 82 private TrieNode searchNode(String str){ 83 TrieNode p = root; 84 for(int i=0; i<str.length(); i++){ 85 char c = str.charAt(i); 86 if(p.children[c-'a'] == null) return null; 87 else { 88 p = p.children[c-'a']; 89 } 90 } 91 92 return p; 93 } 94 95 class TrieNode{ 96 char c; 97 TrieNode[] children; 98 boolean isLeaf; 99 TrieNode(){ 100 children = new TrieNode[26]; 101 } 102 TrieNode(char c){ 103 this.c = c; 104 children = new TrieNode[26]; 105 } 106 } 107 }