-
Notifications
You must be signed in to change notification settings - Fork 2
/
Implement dictionary using trie.txt
70 lines (63 loc) · 1.85 KB
/
Implement dictionary using trie.txt
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
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
unordered_map<char,Node*> m;
string meaning;
bool terminal;
Node(char d)
{
data=d;
terminal=false;
}
};
void insert(string word,string mean,Node* root)
{
Node* temp=root;
for(unsigned int i=0;i<word.length();i++)
{
char ch=word[i];
if(temp->m.find(ch)!=temp->m.end())
temp=temp->m[ch];
else
{
Node* n=new Node(ch);
temp->m[ch]=n;
temp=temp->m[ch];
}
}
temp->terminal=true;
temp->meaning=mean;
}
void getMeaning(string word,Node* root)
{
Node *temp=root;
for(unsigned int i=0;i<word.length();i++)
{
char ch=word[i];
if(temp->m.find(ch)!=temp->m.end())
temp=temp->m[ch];
else
{
cout<<"Not Found\n";
return;
}
}
if(temp->terminal)
cout<<temp->meaning<<"\n";
else
cout<<"Not found\n";
}
int main()
{
Node *root=new Node('\0');
insert("language", "the method of human communication",root);
insert("computer", "A computer is a machine that can be instructed to carry out sequences of arithmetic or logical operations automatically via computer programming",root);
insert("map", "a diagrammatic representation of an area",root);
insert("book", "a written or printed work consisting of pages glued or sewn together along one side and bound in covers.",root);
insert("science", "the intellectual and practical activity encompassing the systematic study of the structure and behaviour of the physical and natural world through observation and experiment.",root);
getMeaning("map",root);
getMeaning("ayu",root);
getMeaning("scienc",root);
}