-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocomplete.cpp
More file actions
106 lines (83 loc) · 2.12 KB
/
autocomplete.cpp
File metadata and controls
106 lines (83 loc) · 2.12 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
#include <bits/stdc++.h>
using namespace std;
const int MAX = 26;
struct trie{
struct trie* chd[MAX] = {};
int freq;
bool flag;
};
void insert_trie(struct trie * tree,char * wd){
struct trie * curr=tree;// curr
while(*wd!='\0'){
if(curr->chd[*wd-'a']==NULL){
curr->chd[*wd-'a']= (struct trie *) calloc(1, sizeof(struct trie));
}
curr=curr->chd[*wd-'a'];
++wd;
}
curr->flag=true;
}
struct trie* search_trie(struct trie* curr,char* wd){
while(*wd!='\0'){
if(curr->chd[*wd-'a']!=NULL){
curr=curr->chd[*wd-'a'];
++wd;
}
else
break;
}
if(*wd=='\0')
return curr;
else
return NULL;
}
void autocomplete(struct trie* tree, vector<char> wd, char* prefix){
bool nochild=true;
if(tree->flag && wd.size()!=0){// if wd.size() is not kept here , it gonna print the prefix so keep this check
// first print what is given as prefix
cout<<prefix;// now after this print the things uu got in vector wd
for(int i=0;i<wd.size();i++){
cout<<wd[i];
}
cout<<endl;
}
for(int i=0;i<MAX;i++){
if(tree->chd[i]!=nullptr){
// nochild=false;// it has a child
wd.push_back(i+'a');
autocomplete(tree->chd[i],wd,prefix);
wd.pop_back();
}
}
wd.pop_back();
}
int main(){
struct trie * tree = new trie();
char filename[100];
ifstream bad;
printf("Name of WordList : ");
cin.getline(filename, 100);
bad.open(filename);
if(!bad.is_open()){
exit(EXIT_FAILURE);
}
char wd[50];
bad >> wd;
while(bad.good()){
insert_trie(tree,wd); // creating trie
bad >> wd;
}
int chc = 1;
vector<char> ch;
ch.clear();
cout << "Enter Word to Search : ";
cin >> wd;
trie* temp = search_trie(tree,wd);
if(temp == nullptr)
cout << "NO Word with this prefix in WordList."<< "\n";
else{
cout << "Your word could be : \n";
autocomplete(temp, ch, wd);
}
return 0;
}