-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.cpp
46 lines (40 loc) · 955 Bytes
/
trie.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
#include<cstdio>
#include<string.h>
const int ALPHABET = 26;
int toNumber(char ch) { return ch - 'A';}
struct TrieNode {
TrieNode* child[ALPHABET];
bool terminal;
TrieNode() : terminal(false) {
memset(child, 0, sizeof(child));
}
~TrieNode() {
for(int i = 0; i < ALPHABET; i++){
if(child[i]) delete child[i];
}
}
void insert(const char* str) {
if(*str == '\0') {
terminal = true;
return ;
}
int next = toNumber(*str);
if(child[next] == NULL) child[next] = new TrieNode();
child[next]->insert(str+1);
}
TrieNode* find(const char* str) {
if(*str == 0) return this;
int next = toNumber(*str);
if(child[next] == NULL) return NULL;
return child[next]->find(str+1);
}
};
int main(){
TrieNode trie;
trie.insert("ASHGOLD");
trie.insert("APPLE");
trie.insert("SUPERAHN");
if(trie.find("ASHGOLD") != NULL) printf("ASHGOLD exists!!\n");
if(trie.find("ASHGOLF") != NULL) printf("ASHGOLF exists!!\n");
return 0;
}