1. 程式人生 > >Trie Tree的構建 -- find/search/searchAll/search Prefix

Trie Tree的構建 -- find/search/searchAll/search Prefix

比如網上一個例子

一組單詞,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 }