forked from amega-dsa/starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.hpp
61 lines (54 loc) · 1.07 KB
/
trie.hpp
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
#include <iostream>
#include <string>
using namespace std;
#define CHAR_SIZE 26
// node structure
class trie_node
{
trie_node *next[CHAR_SIZE];
int is_word;
public:
trie_node()
{
this->is_word = 0;
for (int i = 0; i < CHAR_SIZE; i++)
{
this->next[i] = NULL;
}
}
void insert(string name);
bool search(string name);
};
void trie_node::insert(string name)
{
trie_node *temp = this; //storing root node
for (int i = 0; i < name.length(); i++)
{
//if character of string is nt found then name one
if (temp->next[(int)name[i] - (int)'a'] == NULL)
{
temp->next[(int)name[i] - (int)'a'] = new trie_node();
}
//traverse to the next node
temp = temp->next[(int)name[i] - (int)'a'];
}
// incrementing is_word to indicate that a word ends here
temp->is_word++;
}
bool trie_node::search(string name)
{
int level;
int len = name.length();
int ind;
trie_node *temp = this;
for (level = 0; level < len; level++)
{
ind = (int)name[level] - (int)'a';
if (!temp->next[ind])
{
return false;
}
temp = temp->next[ind];
}
return (temp->is_word);
}