-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrie
207 lines (202 loc) · 3.8 KB
/
Trie
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include<bits/stdc++.h>
using namespace std;
int n,h;
class node
{
public:
node *parent;
node *child[26];
vector<int> ind;
};
class trie
{
public:
node *root;
trie()
{
root=(node*)calloc(1,sizeof(node));
}
void insert(string w,int index)
{
node *np=root;
int i=0,c=0;
while(i< w.length())
{
if(np->child[w[i]-'a']==NULL)
{
np->child[w[i]-'a']=(node*)calloc(1,sizeof(node));
np->child[w[i]-'a']->parent=np;
}else c++;
np=np->child[w[i]-'a'];
i++;
}
np=np->parent;
if(c==w.length()&&np->ind.size()!=0)
{
cout<<"\n Word Already Exist";
}else
cout<<"\n Word Inserted Successfully!!!";
np->ind.push_back(index);
}
void display(node *temp,vector<char> v)
{
for(int i=0;i<26;i++)
{
if(temp->child[i]!=NULL)
{
v.push_back('a'+i);
display(temp->child[i],v);
v.pop_back();
}
}
if(temp->ind.size()!=0)
{
vector<char>::iterator it;
for(it=v.begin();it!=v.end();it++)
{
cout<<*it;
}
cout<<"\n Total Occurence of the word:- "<<temp->ind.size()<<" ,Index:- ";
for(int i=0;i<temp->ind.size();i++)
{
cout<<temp->ind[i];
if(i!=temp->ind.size()-1)
cout<<",";
}
cout<<endl;
}
v.pop_back();
}
int search(node *temp,string s)
{
int i=0;
while(i<s.length())
{
if(temp->child[s[i]-'a']!=NULL)
{
temp=temp->child[s[i]-'a'];
i++;
}else
{
cout<<"\n Not found";
return 0;
}
}
if(i==s.length()&&temp->ind.size()>0)
{
cout<<"\n Word found at:- ";
vector<int>::iterator it;
for(it=temp->ind.begin();it<temp->ind.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
return 1;
}else
{
cout<<"\n Not found";
return 0;
}
}
int removeword(node *temp,string w)
{
int i=0;
if(search(temp,w)==0)
{
cout<<"\n Word doesn't exist";
return 0;
}
else
while(i<w.length())
{
temp=temp->child[w[i]-'a'];
i++;
}
int c=0;
for(int i=0;i<26;i++)
{
if(temp->child[i]!=NULL)
{
c=1;
break;
}
}
if(c==1)
temp->ind.pop_back();
else
{
node *np=new node;
while(temp->ind.size()==0)
{
np=temp->parent;
for(int i=0;i<26;i++)
{
if(np->child[i]==temp)
{
np->child[i]=NULL;
}
}
temp=np;
}
}
cout<<"Node Remved successfully!!!";
return 0;
}
};
int main()
{
cout<<"\n *****************************************";
char choice;
int operation;
vector<char> v;
trie t;
do
{
cout<<"\n *********Main menu:- *********";
cout<<"\n1- Insertion ";
cout<<"\n2- Display ";
cout<<"\n3- Search ";
cout<<"\n4- Delete ";
cout<<"\n Enter ur choice:- ";
cin>>operation;
if(operation==1)
{
cout<<"\n Enter the total number of words u want to insert";
cin>>n;
cout<<"\n Enter the total max size of words ";
cin>>h;
string word;
for(int i=0;i<n;i++)
{
cout<<"\n Enter the words "<<i+1;
cin>>word;
while(word.length()>h)
{
cout<<"\n Word size length exceeding !!please enter valid word";
cin>>word;
}
t.insert(word,i+1);
}
}else if(operation==2)
{
t.display(t.root,v);
}
else if(operation==3)
{
string s;
cout<<"\n Enter the string u want to search:- ";
cin>>s;
t.search(t.root,s);
}
else if(operation==4)
{
string s;
cout<<"\n Enter the string u want to delete:- ";
cin>>s;
t.removeword(t.root, s);
}
cout<<"\n Do u want to continue";
cin>>choice;
}while(choice=='y');
return 0;
}