-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.cpp
42 lines (38 loc) · 981 Bytes
/
dictionary.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
#include "dictionary.h"
#include "constants.h"
using namespace std;
Dictionary::Dictionary( vector <string>& words)
{
d_root = new TrieNode();
for(auto word:words){
addWord(word);
}
}
TrieNode* Dictionary::getNewNode(){
TrieNode* new_node = new TrieNode();
return new_node;
}
void Dictionary::addWord(const string& word){
TrieNode *current = d_root;
for (auto character:word)
{
if (current->children[character]==nullptr)
{
current->children[character] = getNewNode();
}
current = current->children[character];
}
current->is_end_of_word = true;
}
bool Dictionary::isWordPresent(const string& word){
TrieNode *current = d_root;
for (auto character:word)
{
if (current->children[character]!=nullptr) {
current = current->children[character];
} else {
return false;
}
}
return (current != nullptr && current->is_end_of_word );
}