Introduction
什麼是TrieTree?
Trie稱為前綴樹或字典樹,是有序樹的一種,Node的key通常為String類型。Trie Tree與Binary-Searching Tree不同的點是,Trie Tree的Key並不會直接保存在Node中,而是它在Tree中的位置所決定的。一個Node中的所有的childrens都有相同的Prefix(前綴)。假設有個Node的key 為T
,它的children將會是Time
, Tim
, Test
等,因為他們都會相同的Prefix(前綴)T
。
Trie Tree 的應用
Trie Tree 結構圖

Trie Tree Template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| class TrieNode{
public:
TrieNode(){
//suppose we are considering a string consist with a-z
//at most 26 childrens for a node
child = vector<TrieNode*>(26);
}
vector<TrieNode*> child; //
bool isWord = false; //indicate current word is a word
void AddNode(string& str){
TrieNode*root = this;
for(int i = 0;i<str.length();i++){
//adding a node that key is str[i]
if(!root->child[str[i] - 'a']) root->child[str[i] - 'a'] = new TrieNode();
root = root->child[str[i] - 'a'];
}
}
//Other function define here...
//Find a word etc...
};
|
參考資料
https://zh.wikipedia.org/wiki/Trie