forked from vipulchaudhary16/data-structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayGround.cpp
More file actions
79 lines (66 loc) · 1.39 KB
/
PlayGround.cpp
File metadata and controls
79 lines (66 loc) · 1.39 KB
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
#include<bits/stdc++.h>
using namespace std;
class TrieNode
{
public:
char data;
TrieNode *children[26];
bool isTerminal;
TrieNode(char data)
{
this->data = data;
for (int i = 0; i < 26; i++)
{
children[i] = NULL;
}
isTerminal = false;
}
};
class Trie {
private:
void insertUtil(TrieNode *root, string word)
{
if (word.size() == 0)
{
root->isTerminal = true;
return;
}
int ind = word[0] - 'a';
TrieNode *child;
if (root->children[ind] != NULL)
{
// present
child = root->children[ind];
}
else
{
// absent
child = new TrieNode(word[0]);
root->children[ind] = child;
}
insertUtil(child, word.substr(1));
}
bool searchUtil(TrieNode *root, string word)
{
if(!root) return false;
if (word.size() == 0)
return root->isTerminal;
int ind = word[0] - 'a';
return searchUtil(root->children[ind], word.substr(1));
}
public:
TrieNode *root;
Trie() {
root = new TrieNode('\0');
}
void insert(string word) {
insertUtil(root, word);
}
bool search(string word) {
return searchUtil(root, word);
}
};
int main()
{
return 0;
}