/
ImplementTrie.cpp
140 lines (106 loc) · 3.03 KB
/
ImplementTrie.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
Problem statement
Implement Trie Data Structure to support these operations:
insert(word) - To insert a string "word" in Trie
search(word) - To check if string "word" is present in Trie or not.
startsWith(word) - To check if there is any string in the Trie that starts with the given prefix string "word".
Three type of queries denote these operations:
Type 1: To insert a string "word" in Trie.
1 word
Type 2: To check if the string "word" is present in Trie or not.
2 word
Type 3: To check if there is any string in the Trie that starts with the given prefix string "word".
3 word
/*
Your Trie object will be instantiated and called as such:
Trie* obj = new Trie();
obj->insert(word);
bool check2 = obj->search(word);
bool check3 = obj->startsWith(prefix);
*/
class TrieNode{
public:
char data;
TrieNode* children[26];
bool isTerminal;
TrieNode( char ch )
{
data = ch;
for(int i=0;i<26;i++){
children[i] == NULL;
}
isTerminal = false;
}
};
class Trie {
TrieNode* root;
public:
/** Initialize your data structure here. */
Trie() {
root = new TrieNode('\0');
}
void insertUtils(TrieNode* root, string word){
if(word.length() == 0){
root->isTerminal = true;
return;
}
int index = word[0] - 'a';
TrieNode* child;
if(root ->children[index] != NULL){
//present hain trie m word
child = root->children[index];
}
else{
//absent
child = new TrieNode(word[0]);
root ->children[index] = child;
}
//Recursion
insertUtils(child,word.substr(1));
}
/** Inserts a word into the trie. */
void insert(string word) {
insertUtils(root, word);
}
bool searchUtil(TrieNode* root, string word){
if(word.length() == 0){
return root->isTerminal;
}
int index = word[0] - 'a';
TrieNode* child;
if(root ->children[index] != NULL){
//present hain trie m word
child = root->children[index];
}
else{
//absent
return false;
}
//Recursion
return searchUtil(child,word.substr(1));
}
/** Returns if the word is in the trie. */
bool search(string word) {
return searchUtil(root,word);
}
bool prefixUtil(TrieNode* root, string word){
if(word.length() == 0){
return true;
}
int index = word[0] - 'a';
TrieNode* child;
if(root ->children[index] != NULL){
//present hain trie m word
child = root->children[index];
}
else{
//absent
return false;
}
//Recursion
return prefixUtil(child,word.substr(1));
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
return prefixUtil(root,prefix);
}
};