-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
46 lines (38 loc) · 887 Bytes
/
trie.cpp
File metadata and controls
46 lines (38 loc) · 887 Bytes
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<iostream>
using namespace std;
struct TRIE{
bool finish;
TRIE *node[26];
TRIE() {
finish = false;
for (int i = 0; i < 26; i++) node[i] = NULL;
}
~TRIE(){
for (int i = 0; i < 26; i++){
if(node[i]) delete node[i];
}
}
void insert(char *str){
if (*str == NULL){
finish = true;
return;
}
int cur = *str - 'A';
if (node[cur] == NULL) node[cur] = new TRIE();
node[cur]->insert(str + 1);
}
bool find(char *str){
if (*str == NULL){
if (finish == true) return true;
return false;
}
int cur = *str - 'A';
if (node[cur] == NULL) return false;
return node[cur]->find(str + 1);
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}