-
Notifications
You must be signed in to change notification settings - Fork 2
/
Trie.cpp
107 lines (104 loc) · 2.53 KB
/
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
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
#include <bits/stdc++.h>
using namespace std;
struct Trie_Node
{
struct Trie_Node* alphabet[26];
bool isLeaf;
Trie_Node()
{
isLeaf = false;
for(int i = 0; i < 26; i++)
alphabet[i] = NULL;
}
};
class Trie {
Trie_Node* root;
public:
/** Initialize your data structure here. */
Trie() {
root = new Trie_Node;
}
/** Inserts a word into the trie. */
void insert(string str) {
if(!root)
root = new Trie_Node;
Trie_Node* temp = root;
int index;
for(int i = 0; i < str.size(); i++)
{
index = str[i] - 'a';
if(!temp->alphabet[index])
temp->alphabet[index] = new Trie_Node;
temp = temp->alphabet[index];
}
temp->isLeaf = true;
}
/** Returns if the word is in the trie. */
bool search(string str) {
Trie_Node* temp = root;
if(!root && str == "")
return true;
int index;
for(int i = 0; i < str.size(); i++)
{
index = str[i] - 'a';
if(!temp->alphabet[index])
return false;
temp = temp->alphabet[index];
}
if(temp->isLeaf)
return true;
return false;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string str) {
Trie_Node* temp = root;
if(!root && str == "")
return true;
int index;
for(int i = 0; i < str.size(); i++)
{
index = str[i] - 'a';
if(!temp->alphabet[index])
return false;
temp = temp->alphabet[index];
}
return true;
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/
int main()
{
Trie t;
while(1)
{
string str;
cout << "Enter a string::" << endl;
cin>>str;
int type;
cout << "Enter type of query\n1. for insert\n2. for search\n3. for finding matching prefix::" << endl;
cin>>type;
if(type==1)
t.insert(str);
else if(type==2)
{
if(t.search(str))
cout << "string is already present in the trie::" << endl;
else
cout << "string is not present in the trie::" << endl;
}
else
{
if(t.startsWith(str))
cout << "Trie contain this string as a prefix::" << endl;
else
cout << "Trie doesn't contain this string as a prefix::" << endl;
}
}
}