forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie_implementation.cpp
144 lines (136 loc) · 2.81 KB
/
Trie_implementation.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
/*Tries are tree based structures used for efficient
retrieval of key form huge set of strings.
This is a basic implementation of Trie in which strings are inserted and
then there are some queries to find if strings are present in trie or not.
Time Complexity for insert and find function:
O(l) where l is the length of word.
Space complexity: O(n*m) where n is number of keys
and m=max(l) i.e max length of input strings.*/
#include<bits/stdc++.h>
using namespace std;
//Trie Node
class Node{
public:
char data;
//to store children of node
unordered_map<char,Node*> children;
bool terminal;
Node(char d){
data=d;
terminal=false;
}
};
class Trie{
Node*root;
public:
Trie(){
root=new Node('\0');
}
//to insert strings in Trie
void insert(string w){
Node*temp=root;
for(int i=0;w[i]!='\0';i++){
char ch=w[i];
if(temp->children.count(ch)){
temp=temp->children[ch];
}
else{
Node*n=new Node(ch);
temp->children[ch]=n;
temp=n;
}
}
temp->terminal=true;
}
//To find if string is present or not
bool find(string w){
Node*temp=root;
for(int i=0;w[i]!='\0';i++){
char ch=w[i];
if(temp->children.count(ch)==0){
return false;
}
else{
temp=temp->children[ch];
}
}
return temp->terminal;
}
//to delete a string from trie
void deletion(string w){
Node*temp=root;
for(int i=0;w[i]!='\0';i++){
char ch=w[i];
if(temp->children.count(ch)==0){
return;
}
else{
temp=temp->children[ch];
}
}
temp->terminal=false;
}
};
int main(){
//initialize trie
Trie T;
cout<<"Enter number of strings:"<<endl;
int n;
cin>>n;
string*arr=new string[n];
cout<<"Enter the strings:"<<endl;
for(int i=0;i<n;i++){
cin>>arr[i];
T.insert(arr[i]);
}
cout<<"Enter number of queries to find if strings are present or not"<<endl;
int q;
cin>>q;
cout<<"Enter a string for each query:"<<endl;
while(q--){
string s;
cin>>s;
if(T.find(s)){
//String is present in set of strings
cout<<"The given string is present in the set of strings"<<endl;
}
else{
cout<<"The given string is absent in the set of strings"<<endl;
}
}
//for deletion
string str;
cin>>str;
T.deletion(str);
if(T.find(str)){
//String is present in set of strings
cout<<"The given string is present in the set of strings"<<endl;
}
else{
cout<<"The given string is absent in the set of strings"<<endl;
}
}
/*Sample input:
5
"a"
"hello"
"HEllo"
"apple"
"news"
6
"a"
"hello"
"HEllo"
"not"
"new"
"news"
"news"
Sample output:
The given string is present in the set of strings
The given string is present in the set of strings
The given string is present in the set of strings
The given string is absent in the set of strings
The given string is absent in the set of strings
The given string is present in the set of strings
The given string is absent in the set of strings
*/