-
Notifications
You must be signed in to change notification settings - Fork 91
/
TrieTree.cpp
64 lines (63 loc) · 1.43 KB
/
TrieTree.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
/// Using Trie Tree we can find a word from a dictionary
#include<bits/stdc++.h>
using namespace std;
struct node{
bool end_flag;
node *next[27];
node(){ /// constructor
end_flag = false;
for(int i=0; i<26; i++)
next[i] = NULL;
}
}*root;
void insert_word(string s){ /// for insert into dictionary
node *cur = root;
for(int i=0; i<s.size(); i++){
int id = s[i]-'a';
if(cur->next[id]==NULL)
cur->next[id]=new node();
cur = cur->next[id];
}
cur->end_flag=true;
}
bool search_word(string s){ /// for search into dictionary
node *cur = root;
for(int i=0; i<s.size(); i++){
int id = s[i]-'a';
if(cur->next[id]==NULL)
return false;
cur = cur->next[id];
}
return cur->end_flag;
}
void delete_tree(node *cur){ /// delete the dictionary, free the memory
for(int i=0; i<26; i++){
if(cur->next[i]){
delete_tree(cur->next[i]);
}
}
delete(cur);
}
int main()
{
root = new node();
int n;
cout<<"How many word: ";
cin>>n;
while(n--){
string s;
cin>>s;
insert_word(s);
}
int q;
cout<<"How many query: ";
cin>>q;
while(q--){
string s;
cin>>s;
if(search_word(s)) cout<<"Found!"<<endl;
else cout<<"Not found!"<<endl;
}
delete_tree(root);
return 0;
}