-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTries.h
More file actions
49 lines (36 loc) · 878 Bytes
/
Copy pathTries.h
File metadata and controls
49 lines (36 loc) · 878 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
47
48
49
class Tries {
public:
const static int size = 27;
Tries* children[size];
bool is_end;
Tries* CreateNode() {
Tries* root = new Tries;
root->is_end = false;
for (int i = 0; i < root->size; i++) {
root->children[i] = NULL;
}
return root;
}
void InsertWord(Tries* root, string word) {
Tries* loc = root;
for (int i = 0; i < word.length(); i++) {
int index = word[i] - 'A';
if (loc->children[index] == NULL) {
loc->children[index] = CreateNode();
}
loc = loc->children[index];
}
loc->is_end = true;
}
bool SearchWord(Tries* root, string word) {
Tries* loc = root;
for (int i = 0; i < word.length(); i++) {
int index = word[i] - 'A';
if (loc->children[index] == NULL) {
return false;
}
loc = loc->children[index];
}
return (loc->is_end);
}
};