本文轉自 http://www.cnblogs.com/gaochundong 作者:Dennis Gao 大神
字典樹(Trie)是一種很特別的樹狀信息檢索數據結構,如同其名,它的構成就像一本字典,可以讓你快速的進行字符插入、字符串搜索等。
Trie 一詞來自 retrieval,發音爲 /tri:/ "tree",也有人讀爲 /traɪ/ "try"。
字典樹設計的核心思想是空間換時間,所以數據結構本身比較消耗空間。但它利用了字符串的共同前綴(Common Prefix)作爲存儲依據,以此來節省存儲空間,並加速搜索時間。Trie 的字符串搜索時間複雜度爲 O(m),m 爲最長的字符串的長度,其查詢性能與集合中的字符串的數量無關。其在搜索字符串時表現出的高效,使得特別適用於構建文本搜索和詞頻統計等應用。
字典樹的性質
- 根節點(Root)不包含字符,除根節點外的每一個節點都僅包含一個字符;
- 從根節點到某一節點路徑上所經過的字符連接起來,即爲該節點對應的字符串;
- 任意節點的所有子節點所包含的字符都不相同;
如下圖的 Trie 樹中包含了字符串集合 ["Joe", "John", "Johnny", "Jane", "Jack"]。
Trie 關鍵詞查找過程:
- 每次從根結點開始搜索;
- 獲取關鍵詞的第一個字符,根據該字符選擇對應的子節點,轉到該子節點繼續檢索;
- 在相應的子節點上,獲取關鍵詞的第二個字符,進一步選擇對應的子節點進行檢索;
- 以此類推,進行迭代過程;
- 在某個節點處,關鍵詞的所有字母已被取出,則讀取附在該節點上的信息,查找完成。
關鍵詞的插入和查找過程的時間複雜度均爲 O(key_length),空間複雜度 O(ALPHABET_SIZE * key_length * N) ,其中 N 是關鍵詞的數量。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) 6 7 // Alphabet size (# of symbols) 8 #define ALPHABET_SIZE (26) 9 10 // Converts key current character into index 11 // use only 'a' through 'z' and lower case 12 #define CHAR_TO_INDEX(c) ((int)c - (int)'a') 13 14 // trie node 15 typedef struct trie_node trie_node_t; 16 struct trie_node 17 { 18 int value; 19 trie_node_t *children[ALPHABET_SIZE]; 20 }; 21 22 // trie ADT 23 typedef struct trie trie_t; 24 struct trie 25 { 26 trie_node_t *root; 27 int count; 28 }; 29 30 // Returns new trie node (initialized to NULLs) 31 trie_node_t *getNode(void) 32 { 33 trie_node_t *pNode = NULL; 34 35 pNode = (trie_node_t *)malloc(sizeof(trie_node_t)); 36 37 if (pNode) 38 { 39 int i; 40 41 pNode->value = 0; 42 43 for (i = 0; i < ALPHABET_SIZE; i++) 44 { 45 pNode->children[i] = NULL; 46 } 47 } 48 49 return pNode; 50 } 51 52 // Initializes trie (root is dummy node) 53 void initialize(trie_t *pTrie) 54 { 55 pTrie->root = getNode(); 56 pTrie->count = 0; 57 } 58 59 // If not present, inserts key into trie 60 // If the key is prefix of trie node, just marks leaf node 61 void insert(trie_t *pTrie, char key[]) 62 { 63 int level; 64 int length = strlen(key); 65 int index; 66 trie_node_t *pCrawl; 67 68 pTrie->count++; 69 pCrawl = pTrie->root; 70 71 for (level = 0; level < length; level++) 72 { 73 index = CHAR_TO_INDEX(key[level]); 74 if (!pCrawl->children[index]) 75 { 76 pCrawl->children[index] = getNode(); 77 } 78 79 pCrawl = pCrawl->children[index]; 80 } 81 82 // mark last node as leaf 83 pCrawl->value = pTrie->count; 84 } 85 86 // Returns non zero, if key presents in trie 87 int search(trie_t *pTrie, char key[]) 88 { 89 int level; 90 int length = strlen(key); 91 int index; 92 trie_node_t *pCrawl; 93 94 pCrawl = pTrie->root; 95 96 for (level = 0; level < length; level++) 97 { 98 index = CHAR_TO_INDEX(key[level]); 99 100 if (!pCrawl->children[index]) 101 { 102 return 0; 103 } 104 105 pCrawl = pCrawl->children[index]; 106 } 107 108 return (0 != pCrawl && pCrawl->value); 109 } 110 111 // Driver 112 int main() 113 { 114 // Input keys (use only 'a' through 'z' and lower case) 115 char keys[][8] = { "the", "a", "there", "answer", "any", "by", "bye", "their" }; 116 117 char output[][32] = { "Not present in trie", "Present in trie" }; 118 119 trie_t trie; 120 initialize(&trie); 121 122 // Construct trie 123 for (int i = 0; i < ARRAY_SIZE(keys); i++) 124 { 125 insert(&trie, keys[i]); 126 } 127 128 // Search for different keys 129 printf("%s --- %s\n", "the", output[search(&trie, "the")]); 130 printf("%s --- %s\n", "these", output[search(&trie, "these")]); 131 printf("%s --- %s\n", "their", output[search(&trie, "their")]); 132 printf("%s --- %s\n", "thaw", output[search(&trie, "thaw")]); 133 134 return 0; 135 }
Trie 的應用
- 字符串檢索:事先將已知的一些字符串(字典)的有關信息保存到 Trie 裏,查找另外一些未知字符串是否出現過或者出現頻率。
- 字符串最長公共前綴:Trie 利用多個字符串的公共前綴來節省存儲空間,反之,當我們把大量字符串存儲到一棵 Trie 上時,我們可以快速得到某些字符串的公共前綴。
- 排序:Trie 樹是一棵多叉樹,只要先序遍歷整棵樹,輸出相應的字符串,便是按字典序排序的結果。
- 作爲其他數據結構和算法的輔助結構:如後綴樹,AC自動機等。