-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trie.cpp
184 lines (147 loc) · 2.72 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
Name : Aarti Rathi
Wesbite : https://shinchancode.github.io/3d-react-portfolio/
*/
#include<bits/stdc++.h>
using namespace std;
class Node
{
public:
char data;
pair<bool,string> is_last;
unordered_map<char,Node*> children;
Node(char d)
{
data = d;
is_last.first=false;
is_last.second="-1";
}
};
class Trie
{
Node *root;
Node* tmp;
public:
//Initialize trie data structure here
Trie()
{
root=new Node('\0');
}
//Inserting a word into the trie
void insert(string word,string phn_no)
{
Node* temp = root;
for(int i=0;i<word.length();i++)
{
char ch = word[i];
if(temp->children.count(ch))
{
temp = temp->children[ch];
}
else
{
Node *n = new Node(ch);
temp->children[ch]=n;
temp = temp->children[ch];
}
}
temp->is_last.first = 1;
temp->is_last.second = phn_no;
cout<<"\n\t\t\t\t\t\t Data Added Successfully \n";
}
//Returns if the word is in the trie
bool search(string word)
{
Node *temp= root;
for(int i=0;i<word.length();i++)
{
char ch = word[i];
if(!temp->children.count(ch))
{
return false;
}
temp = temp->children[ch];
}
if(temp->is_last.first==1)
{
return true;
}
return false;
}
bool starts_with(string prefix)
{
Node* temp=root;
for(int i=0;i<prefix.size();i++)
{
char ch = prefix[i];
if(!temp->children.count(ch))
{
return false;
}
temp = temp->children[ch];
}
tmp=temp;
return true;
}
//Remove the word if exists
void remove(string word)
{
Node *temp= root;
bool check=true;
for(int i=0;i<word.length();i++)
{
char ch = word[i];
if(!temp->children.count(ch))
{
check=false;
}
else
{
temp = temp->children[ch];
}
}
if(check)
{
temp->is_last.first=false;
cout<<"\n\t\t\t\t\t\t Data Deleted Successfully \n";
return;
}
else
{
cout<<"\n\t\t\t\t\t\t Invalid!! Please Enter a valid name\n";
}
}
void build_words(Node *temp,string curr)
{
for(char c='a';c<='z';c++)
{
if(temp->children.count(c))
build_words(temp->children[c],curr+temp->data);
else
{
if(temp->is_last.first)
{
cout<<"\t\t\t\t\t\t ";
cout<<curr+temp->data<<endl;
return;
}
}
}
}
//Show recommendations according to the given word
void show_recommendations(string incom_word)
{
Node *temp = root;
if(!starts_with(incom_word))
{
cout<<"\n\t\t\t\t\t\t Invalid!! Please Enter a valid Prefix\n";
return ;
}
else
{
string cpy = incom_word;
cpy.pop_back();
build_words(tmp,cpy);
}
}
};