-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuto-complete feature using Trie.cpp
151 lines (134 loc) · 3.27 KB
/
Auto-complete feature using 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
/*
All operation of TRIE with hash Map.
8
the a there answer any by bye their
4
abc abcd aa abbbaba
*/
#include<bits/stdc++.h>
using namespace std;
struct node{
map<char, node*> om;
bool isEnd;
};
node* getNode();
void insertString(node*&, string);
string searchString(node*, string);
node* deleteString(node*, string, int);
void autoComplete(node*, string);
void displayAll(node*);
void _displayAll(node*, string);
int main(){
node *root = nullptr;
int n, a;
string str;
cin >> n;
for(int i = 0; i < n; i++){
cin >> str;
insertString(root, str);
}
while(1){
cin >> a;
switch(a){
case 1: cin >> str;
cout << searchString(root, str) << endl;
break;
case 2: cin >> str;
if(searchString(root, str) == "No")
cout << "Not Exist" << endl;
else
root = deleteString(root, str, 0);
break;
case 3: cin >> str;
autoComplete(root, str);
break;
case 4: displayAll(root);
break;
default: return 0;
}
}
return 0;
}
node* getNode(){
node *temp = nullptr;
temp = new node;
temp->isEnd = false;
return temp;
}
void insertString(node *&root, string str){
node *temp = nullptr;
if(root == nullptr)
root = getNode();
temp = root;
for(int i = 0; i < str.length(); i++){
if(temp->om.find(str[i]) == temp->om.end())
temp->om[str[i]] = getNode();
temp = temp->om[str[i]];
}
temp->isEnd = true;
}
string searchString(node *root, string str){
node *temp = nullptr;
if(root == nullptr)
return "No";
temp = root;
for(int i = 0; i < str.length(); i++){
if(temp->om.find(str[i]) == temp->om.end())
return "No";
temp = temp->om[str[i]];
}
if(temp->isEnd == true)
return "Yes";
else
return "No";
}
node* deleteString(node *root, string str, int index){
if(index == str.length()){
root->isEnd = false;
if(root->om.empty())
return nullptr;
else
return root;
}
else{
char ch = str[index];
node *temp = nullptr;
temp = deleteString(root->om[ch], str, index + 1);
if(temp == nullptr){
root->om.erase(ch);
if(root->isEnd == true)
return root;
if(root->om.empty())
return nullptr;
else
return root;
}
else{
return root;
}
}
}
void displayAll(node *root){
string res;
if(root == nullptr){
cout << "Nothing exist" << endl;
return;
}
_displayAll(root, res);
}
void autoComplete(node *root, string str){
for(int i = 0; i < str.length(); i++){
root = root->om[str[i]];
}
_displayAll(root, str);
}
void _displayAll(node *root, string res){
map<char, node*>::iterator it;
if(root->isEnd == true)
cout << res << " ";
if(root->om.empty())
return;
for(it = root->om.begin(); it != root->om.end(); it++){
_displayAll(it->second, res + it->first);
}
}