-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
65 lines (57 loc) · 1.58 KB
/
trie.cpp
File metadata and controls
65 lines (57 loc) · 1.58 KB
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
60
61
62
63
64
65
// trie code . modified from Shafaet's planet .
// tested problems: UVA (UVA 11362 Phonebook), UVA 11488 Hyper prefix sets
class trie{
public:
struct node {
bool endmark; // kono word shesh hoise kina ei node e
ll f; // ei node hoye koyta string geche.
node* next[26 + 1]; // ith character paoa gese kina
node()
{
endmark = false;
f = 0;
for (int i = 0; i < 26; i++)
next[i] = NULL;
}
} *root;
trie(){
root = new node;
}
public:
void insert(char* str)
{
node* curr = root;
for (int i = 0; str[i] ; i++) {
int id = str[i] - '0';
if (curr->next[id] == NULL)
curr->next[id] = new node();
curr = curr->next[id];
curr->f = (curr->f) + 1; // koyta string gelo count rakhtesi
}
curr->endmark = true; // ei node e eshe ekta string shesh hoise
}
ll search(char* str, int prefixallow = 0 )
{
node* curr = root;
ll mx = 0;
for (int i = 0; str[i] ; i++) {
int id = str[i] - '0';
if (curr->next[id] == NULL)
return false;
curr = curr->next[id];
// if prefix allow then write here.....
}
return curr->endmark;
}
//dfs can be operated in this style
void del(node* cur)
{
for (int i = 0; i < 26; i++)
if (cur->next[i])
del(cur->next[i]);
delete (cur);
}
~trie(){
del( root );
}
};