-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.cpp
More file actions
174 lines (142 loc) · 3.62 KB
/
Copy pathTrie.cpp
File metadata and controls
174 lines (142 loc) · 3.62 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
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
#include <bits/stdc++.h>
#include <string>
using namespace std;
struct Trie
{
Trie *child[26];
int we; //End of Word
};
Trie* createNode()
{
struct Trie *newnode = new Trie;
newnode->we = 0;
for(int i=0 ; i<26 ; i++)
newnode->child[i] = NULL;
return newnode;
}
void insertNode(struct Trie *root , string key)
{
struct Trie *trie = root;
for(int i=0 ; i<key.length() ; i++)
{
//getting the index of each character
int index = key[i] - 'a';
if(trie->child[index] == NULL)
trie->child[index] = createNode();
trie = trie->child[index]; //moving to its children for further storing
}
trie->we += 1;
}
bool searchNode(struct Trie *root , string key)
{
struct Trie *trie = root;
for(int i=0 ; i<key.length() ; i++)
{
int index = key[i] - 'a';
if(!trie->child[index]) //if((trie->child[index] == NULL) || (trie->child[index]->we == 0))
return false;
trie = trie->child[index];
}
return (trie != NULL && trie->we); //return true;
}
bool deleteNode(struct Trie *root , string key)
{
if(searchNode(root , key))
{
struct Trie *trie = root;
for(int i=0 ; i<key.length() ; i++)
{
int index = key[i] - 'a';
trie->child[index]->we -= 1;
trie = trie->child[index];
}
return true;
}
return false;
}
void printWord(string str[] , int n)
{
cout<<endl;
for(int i=0 ; i<n ; i++)
cout<<str[i];
}
void display(struct Trie *root , string wordArray[] , int pos)
{
if(root == NULL)
return;
if(root->we)
printWord(wordArray , pos);
for(int i=0 ; i<26 ; i++)
{
if(root->child[i] != NULL)
{
wordArray[pos] = i+'a';
display(root->child[i] , wordArray , pos+1);
}
}
}
int main()
{
struct Trie *root = createNode();
string keys[15];
int choice;
while(1)
{
cout<<"\n1.Insert \t 2.Search \t 3.Delete \t 4.Display \t 5.EXIT"<<endl;
cout<<"Enter choice : ";
cin>>choice;
switch(choice)
{
case 1:
{
int n;
cout<<"Enter number of strings : ";
cin>>n;
cout<<"Enter the strings :\n";
for(int i=0 ; i<n ; i++)
{
cin>>keys[i];
insertNode(root , keys[i]);
}
break;
}
case 2:
{
string key;
cout<<"Enter the string : ";
cin>>key;
bool result = searchNode(root , key);
if(result)
cout<<"String is found"<<endl;
else
cout<<"String is not found"<<endl;
break;
}
case 3:
{
string key;
cout<<"Enter the string : ";
cin>>key;
bool result = deleteNode(root , key);
if(result)
cout<<"String is deleted"<<endl;
else
cout<<"String is not found"<<endl;
break;
}
case 4:
{
//There is some issue in it and working on it
cout<<"Contents of Trie : "<<endl;
display(root , keys , 0);
break;
}
case 5:
exit(0);
break;
default:
cout<<"\nINVALID CHOICE\n";
}
}
return 0;
}