forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrieshubh.cpp
59 lines (46 loc) · 1021 Bytes
/
Trieshubh.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
47
48
49
50
51
52
53
54
55
56
57
58
59
class Node{
public:
char data;
bool terminal;
unordered_map<char, Node*> children;
Node(char data){
this->data = data;
this->terminal = false;
}
};
class Trie {
Node* root;
int count;
Trie() {
this->root = new Node('\0');
this->count = 0;
}
void addword(string str){
Node* temp = root;
rep(i,sz(str)){
char ch = str[i];
if(temp->children.count(ch)){
temp = temp->children[ch];
}
else{
Node* n = new Node(ch);
temp->children[ch] = n;
temp = n;
}
}
temp->terminal = true;
}
bool search(st str){
Node* temp = root;
rep(i,sz(str)){
char ch = str[i];
if(temp->children.count(ch)){
temp = temp->children[ch];
}
else{
return false;
}
}
return temp->terminal;
}
};