forked from Shivesh-K/tag_files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.h
60 lines (44 loc) · 1.06 KB
/
trie.h
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
#include<bits/stdc++.h>
using namespace std;
struct TrieNode
{
char ele;
bool isEndofWord;
unordered_map<char, TrieNode *> child;
};
TrieNode *getNewNode(char ch)
{
TrieNode *node = new TrieNode;
node->ele = ch;
node->isEndofWord = false;
return node;
}
TrieNode *root = getNewNode('/');
void getMoreTags(string tag, vector<string> &moreTags, TrieNode *current)
{
if (current == nullptr)
return;
if (current->isEndofWord)
moreTags.push_back(tag);
for (auto el : current->child)
getMoreTags(tag + el.first, moreTags, el.second);
}
void insertInTrie(string &tag)
{
int len = tag.length();
TrieNode *current = root;
for (int i = 0; i < len; i++)
{
char val = tag[i];
if ((current->child)[val])
current = (current->child)[val];
else
{
TrieNode *new_node = getNewNode(tag[i]);
(current->child)[val] = new_node;
current = new_node;
}
if (i == len - 1)
current->isEndofWord = true;
}
}