TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
trie_tree.cpp
Go to the documentation of this file.
1
10
#include <array>
11
#include <cassert>
12
#include <cstdint>
13
#include <iostream>
14
#include <memory>
15
#include <string>
16
20
namespace
data_structures
{
25
class
trie
{
26
private
:
27
static
constexpr
uint8_t
NUM_CHARS
= 26;
29
std::array<std::shared_ptr<trie>,
NUM_CHARS << 1>
arr
;
30
bool
isEndofWord
=
false
;
31
38
uint8_t
char_to_int
(
const
char
& ch)
const
{
39
if
(ch >=
'A'
&& ch <=
'Z'
) {
40
return
ch -
'A'
;
41
}
else
if
(ch >=
'a'
&& ch <=
'z'
) {
42
return
ch -
'a'
+
NUM_CHARS
;
43
}
44
45
std::cerr <<
"Invalid character present. Exiting..."
;
46
std::exit(EXIT_FAILURE);
47
return
0;
48
}
49
56
bool
search
(
const
std::shared_ptr<trie>& root,
const
std::string& str,
57
int
index) {
58
if
(index == str.length()) {
59
if
(!root->isEndofWord) {
60
return
false
;
61
}
62
return
true
;
63
}
64
int
j =
char_to_int
(str[index]);
65
if
(!root->arr[j]) {
66
return
false
;
67
}
68
return
search
(root->arr[j], str, index + 1);
69
}
70
71
public
:
72
trie
() =
default
;
73
77
void
insert
(
const
std::string& str) {
78
std::shared_ptr<trie> root(
nullptr
);
79
80
for
(
const
char
& ch : str) {
81
int
j =
char_to_int
(ch);
82
if
(root) {
83
if
(root->arr[j]) {
84
root = root->arr[j];
85
}
else
{
86
std::shared_ptr<trie> temp(
new
trie
());
87
root->arr[j] = temp;
88
root = temp;
89
}
90
}
else
if
(
arr
[j]) {
91
root =
arr
[j];
92
}
else
{
93
std::shared_ptr<trie> temp(
new
trie
());
94
arr
[j] = temp;
95
root = temp;
96
}
97
}
98
root->isEndofWord =
true
;
99
}
100
107
bool
search
(
const
std::string& str,
int
index) {
108
if
(index == str.length()) {
109
if
(!
isEndofWord
) {
110
return
false
;
111
}
112
return
true
;
113
}
114
int
j =
char_to_int
(str[index]);
115
if
(!
arr
[j]) {
116
return
false
;
117
}
118
return
search
(
arr
[j], str, index + 1);
119
}
120
134
bool
deleteString
(
const
std::string& str,
int
index) {
135
if
(index == str.length()) {
136
if
(!
isEndofWord
) {
137
return
false
;
138
}
139
isEndofWord
=
false
;
140
// following lines - possible source of error?
141
// for (int i = 0; i < NUM_CHARS; i++)
142
// if (!arr[i])
143
// return false;
144
return
true
;
145
}
146
int
j =
char_to_int
(str[index]);
147
if
(!
arr
[j]) {
148
return
false
;
149
}
150
bool
var =
deleteString
(str, index + 1);
151
if
(var) {
152
arr
[j].reset();
153
if
(
isEndofWord
) {
154
return
false
;
155
}
else
{
156
int
i = 0;
157
for
(i = 0; i <
NUM_CHARS
; i++) {
158
if
(
arr
[i]) {
159
return
false
;
160
}
161
}
162
return
true
;
163
}
164
}
165
166
/* should not return here */
167
std::cout << __func__ <<
":"
<< __LINE__
168
<<
"Should not reach this line\n"
;
169
return
false
;
170
}
171
};
172
}
// namespace data_structures
173
178
static
void
test
() {
179
data_structures::trie
root;
180
root.
insert
(
"Hello"
);
181
root.insert(
"World"
);
182
183
assert(!root.search(
"hello"
, 0));
184
std::cout <<
"hello - "
<< root.search(
"hello"
, 0) <<
"\n"
;
185
186
assert(root.search(
"Hello"
, 0));
187
std::cout <<
"Hello - "
<< root.search(
"Hello"
, 0) <<
"\n"
;
188
189
assert(!root.search(
"Word"
, 0));
190
std::cout <<
"Word - "
<< root.search(
"Word"
, 0) <<
"\n"
;
191
192
assert(root.search(
"World"
, 0));
193
std::cout <<
"World - "
<< root.search(
"World"
, 0) <<
"\n"
;
194
195
// Following lines of code give erroneous output
196
// root.deleteString("hello", 0);
197
// assert(!root.search("hello", 0));
198
// std::cout << "hello - " << root.search("world", 0) << "\n";
199
}
200
205
int
main
() {
206
test
();
207
208
return
0;
209
}
test
void test()
Definition
caesar_cipher.cpp:100
data_structures::trie
Trie implementation for small-case English alphabets a-z
Definition
trie_tree.cpp:25
data_structures::trie::insert
void insert(const std::string &str)
Definition
trie_tree.cpp:77
data_structures::trie::arr
std::array< std::shared_ptr< trie >, NUM_CHARS<< 1 > arr
Recursive tree nodes as an array of shared-pointers.
Definition
trie_tree.cpp:29
data_structures::trie::search
bool search(const std::string &str, int index)
Definition
trie_tree.cpp:107
data_structures::trie::NUM_CHARS
static constexpr uint8_t NUM_CHARS
Number of alphabets.
Definition
trie_tree.cpp:27
data_structures::trie::isEndofWord
bool isEndofWord
identifier if a node is terminal node
Definition
trie_tree.cpp:30
data_structures::trie::trie
trie()=default
Class default constructor.
data_structures::trie::search
bool search(const std::shared_ptr< trie > &root, const std::string &str, int index)
Definition
trie_tree.cpp:56
data_structures::trie::char_to_int
uint8_t char_to_int(const char &ch) const
Convert a character to integer for indexing.
Definition
trie_tree.cpp:38
data_structures::trie::deleteString
bool deleteString(const std::string &str, int index)
Definition
trie_tree.cpp:134
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
data_structures
for IO operations
search
for std::assert
Definition
binary_search.cpp:47
data_structures
trie_tree.cpp
Generated by
1.18.0