-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInsertAndSearchInATrie.cpp
42 lines (38 loc) · 1.07 KB
/
InsertAndSearchInATrie.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
// User function template for C++
// trie node
/*
struct TrieNode {
struct TrieNode *children[ALPHABET_SIZE];
// isLeaf is true if the node represents
// end of a word
bool isLeaf;
};
*/
class Solution
{
public:
//Function to insert string into TRIE.
void insert(struct TrieNode *root, string key)
{
// code here
for (int i=0;i<key.size();i++)
{
if (root->children[key[i]-'a']==NULL)
{root->children[key[i]-'a']=new TrieNode();}
root=root->children[key[i]-'a'];
}
root->isLeaf=true;
}
//Function to use TRIE data structure and search the given string.
bool search(struct TrieNode *root, string key)
{
// code here
for (int i=0;i<key.size();i++)
{
if (root->children[key[i]-'a']==NULL) return false;
root=root->children[key[i]-'a'];
}
if (root->isLeaf==true) return true;
return false;
}
};