-
Notifications
You must be signed in to change notification settings - Fork 47
/
Trie.cpp
31 lines (30 loc) · 792 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
void insert(struct TrieNode *root, string key)
{
// code here
if(root == NULL) return;
TrieNode *temp = root;
for(int i = 0; i < key.length(); i++)
{
char c = key[i];
if(temp -> children[c - 'a'] == NULL)
{
temp -> children[c - 'a'] = new TrieNode();
}
temp = temp -> children[c - 'a'];
}
temp -> isLeaf = true;
}
//Function to use TRIE data structure and search the given string.
bool search(struct TrieNode *root, string key)
{
// code here
TrieNode *temp = root;
for(int i = 0; i < key.length(); i++)
{
char c = key[i];
if(temp -> children[c - 'a'] == NULL) return false;
temp = temp -> children[c - 'a'];
}
if(temp -> isLeaf) return true;
return false;
}