-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrie.cpp
39 lines (36 loc) · 799 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
struct Node {
bool isEnd;
Node* arr[26];
Node(){
isEnd =false;
for(int i=0;i<26;++i){
arr[i] = nullptr;
}
}
};
bool search(Node*root , const string &s){
Node*temp = root;
for(int i=0;i<(int)s.size();++i){
int val = s[i]-'0';
if(temp->arr[val] != nullptr){
temp = temp->arr[val];
if(temp->isEnd){
return false;
}
}else{
return true;
}
}
return false;
}
void add(Node*root , const string &s){
Node*temp = root;
for(int i=0;i<(int)s.size();++i){
int val = s[i]-'0';
if(temp->arr[val] == nullptr){
temp -> arr[val] = new Node;
}
temp = temp->arr[val];
}
temp->isEnd = true;
}