-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie_implementation.cpp
137 lines (128 loc) · 4.38 KB
/
trie_implementation.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
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
#include <iostream>
#define CHAR_SIZE 26
using namespace std;
class trie
{
// Node Structure for a Trie
struct trie_node
{
char val; // What is the value of that node
int count; // How many node is with that value
int ends_here; // How many strings ends at that node
trie_node *child[CHAR_SIZE]; // Array of pointers to hold the children CHAR_SIZE may vary. In this implementation it is only for lower case english alphabets
};
trie_node *root; // Hold the root of the trie
trie_node *get_node(int index) // Function to get the new node every time a child is created
{
trie_node *new_node = new trie_node;
new_node->val = 'a' + index; // Assign the value to the new_node
new_node->count = new_node->ends_here = 0; // make count and ends_here equal to 0
for (int i = 0; i < CHAR_SIZE; ++i)
{
new_node->child[i] = NULL; // Make all the child node NULL
}
return new_node; // Return the new_node
}
public:
trie()
{
root = get_node('/' - 'a'); // The root will store the forward slash in this implementation '/'
}
void insert(const string &);
bool search(const string &);
int starts_with(const string &);
bool is_empty(const trie_node *);
bool have_children(const trie_node *);
trie_node* get_root () {
return root;
}
};
// Function to check is trie is empty or not
bool trie::is_empty(const trie_node *root)
{
for (int i = 0; i < CHAR_SIZE; ++i)
{
if (root->child[i])
{
return false;
}
}
return true;
}
// Function to check whether the trie have children or not
bool trie::have_children(const trie_node *root)
{
return !is_empty(root);
}
// Function to insert in the trie
void trie::insert(const string &word)
{
trie_node *current = root; // Point the current node to root pointer
int index;
for (int i = 0, len = word.size(); i < len; ++i)
{
index = word[i] - 'a'; // Get the index of the word
if (!current->child[index]) // Check if the child is NULL or not
{
current->child[index] = get_node(index); // If Child is NULL then assign a new_node to it;
}
current->child[index]->count += 1; // Increment the current child count by 1
current = current->child[index]; // Make the current pointer point to the current child
}
current->ends_here += 1; // Increment the current end_here by 1
}
// Function to search in the trie
bool trie::search(const string &word)
{
trie_node *current = root; // Point the current node to root pointer
int index;
for (int i = 0, len = word.size(); i < len; ++i)
{ // Iterate over the word
index = word[i] - 'a'; // Get the index of the word[i]
if (!current->child[index])
{ // Check if there exists a child node of not
return false; // if the child node doesn't exist then return false
}
current = current->child[index]; // Make the current pointer point to the current child
}
return (current->ends_here > 0); // Check if any string ends_here or not
}
// Function to check how many string are there with the given prefix
int trie::starts_with(const string &prefix)
{
trie_node *current = root; // Point the current node to the root node
int index;
for (int i = 0, len = prefix.size(); i < len; ++i)
{ // Iterate over the whole prefix
index = prefix[i] - 'a'; // Get the index of the current character of the prefix
if (!current->child[index])
{ // Check if there exists a child or not
return 0; // if child doesn't exist return 0
}
current = current->child[index]; // Move the pointer to the next node
}
return current->count; // return the count at that node
}
int main()
{
int n;
string s;
trie trie_root;
cin >> n;
while (getline(cin, s))
{
if (s == "-1")
{
break;
}
trie_root.insert(s);
}
while (n--)
{
getline(cin, s);
cout << boolalpha << trie_root.search(s) << ' ';
cout << trie_root.starts_with(s) << '\n';
}
cout << trie_root.have_children (trie_root.get_root ()) << '\n';
return 0;
}