-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_trie.h
170 lines (139 loc) · 3.23 KB
/
search_trie.h
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
#include "utils.h"
using namespace std;
struct trie{
bool isEndofWord;
struct trie *next[26];
};
typedef struct trie Trie;
Trie *getNode(){
Trie *temp = new Trie;
temp->isEndofWord = false;
for(int i=0;i<26;i++){
temp->next[i] = NULL;
}
return temp;
}
Trie *root = getNode();
bool isLastNode(Trie *root){
for(int i=0;i<26;i++){
if(root->next[i] != NULL)
return false;
}
return true;
}
void insert_trie(string key, Trie *root){
Trie *crawl = root;
for(int i=0;i<key.length();i++){
int index = key.at(i)-97;
if(crawl->next[index] == NULL){
crawl->next[index] = getNode();
}
crawl = crawl->next[index];
}
crawl->isEndofWord=true;
}
void printRecursive(Trie *temp, vector<string> &vec, string prefix){
// cout<<prefix<<endl;
if(temp->isEndofWord){
vec.push_back(prefix);
}
if(isLastNode(temp)){
return;
}
for(int i=0;i<26;i++){
if(temp->next[i]!=NULL){
printRecursive(temp->next[i], vec, prefix+char(i+97));
}
}
}
vector<string> suggestions(string query){
Trie *temp = root;
int len = query.length();
vector<string> vec;
for(int i=0;i<len;i++){
int index = query.at(i) - 97;
if(temp->next[index]==NULL){
return vec;
}
temp=temp->next[index];
}
bool lastword;
if(temp->isEndofWord == true){
lastword=true;
vec.push_back(query);
}
else
lastword=false;
bool last = isLastNode(temp);
if(last==true && lastword==true){
vec.push_back(query);
return vec;
}
if(isLastNode(temp) == false){
printRecursive(temp, vec, query);
return vec;
}
}
bool isContainsOtherThanCharacter(char *str){
for(int i=0;str[i]!='\0';i++){
if( isalpha(str[i]))
continue;
else
return true;
}
}
void init_trie(){
DIR *dir;
struct dirent *ent;
// bin directory;
if((dir=opendir("/bin")) != NULL){
while((ent = readdir(dir)) != NULL){
if(isContainsOtherThanCharacter(ent->d_name))
continue;
insert_trie(ent->d_name, root);
}
closedir(dir);
}
else{
exit(-1);
}
// usr/bin
if((dir=opendir("/usr/bin")) != NULL){
while((ent=readdir(dir)) != NULL){
if(isContainsOtherThanCharacter(ent->d_name))
continue;
insert_trie(ent->d_name, root);
}
closedir(dir);
}
else
{
exit(-1);
}
// user/local/bin
if((dir=opendir("/usr/local/bin")) != NULL){
while((ent=readdir(dir)) != NULL){
if(isContainsOtherThanCharacter(ent->d_name))
continue;
insert_trie(ent->d_name, root);
}
closedir(dir);
}
else
{
exit(-1);
}
// sbin
if((dir=opendir("/sbin")) != NULL){
while((ent=readdir(dir)) != NULL){
if(isContainsOtherThanCharacter(ent->d_name))
continue;
insert_trie(ent->d_name, root);
}
closedir(dir);
}
else
{
exit(-1);
}
}