/
ImplementTrie(Prefix Tree).cpp
59 lines (54 loc) · 1.39 KB
/
ImplementTrie(Prefix Tree).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class TrieNode {
public:
// Initialize your data structure here.
char c;
map<char,TrieNode*> child;
TrieNode(char character) {
c = character;
}
};
class Trie {
public:
Trie() {
root = new TrieNode('\0');
end = new TrieNode('\0');
}
// Inserts a word into the trie.
void insert(string word) {
int i;
TrieNode *tmp, *child;
for(i = 0, tmp = root; word[i]; i++, tmp = child){
child = tmp->child[word[i]];
if(!child){
child = new TrieNode(word[i]);
tmp->child[word[i]]=child;
}
}
tmp->child[0]=end;
}
// Returns if the word is in the trie.
bool search(string word) {
int i;
TrieNode *tmp, *child;
for(i = 0, tmp = root; word[i]; i++, tmp = child){
child = tmp->child[word[i]];
if(!child) return false;
}
if(tmp->child[0] == end) return true;
return false;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
int i;
TrieNode *tmp, *child;
for(i = 0, tmp = root; prefix[i]; i++, tmp=child){
child=tmp->child[prefix[i]];
if(!child) return false;
}
return true;
}
private:
TrieNode* root;
TrieNode* end;
};