39 if (ch >=
'A' && ch <=
'Z') {
41 }
else if (ch >=
'a' && ch <=
'z') {
45 std::cerr <<
"Invalid character present. Exiting...";
46 std::exit(EXIT_FAILURE);
56 bool search(
const std::shared_ptr<trie>& root,
const std::string& str,
58 if (index == str.length()) {
59 if (!root->isEndofWord) {
68 return search(root->arr[j], str, index + 1);
77 void insert(
const std::string& str) {
78 std::shared_ptr<trie> root(
nullptr);
80 for (
const char& ch : str) {
86 std::shared_ptr<trie> temp(
new trie());
93 std::shared_ptr<trie> temp(
new trie());
98 root->isEndofWord =
true;
107 bool search(
const std::string& str,
int index) {
108 if (index == str.length()) {
135 if (index == str.length()) {
167 std::cout << __func__ <<
":" << __LINE__
168 <<
"Should not reach this line\n";
180 root.insert(
"Hello");
181 root.insert(
"World");
183 assert(!root.search(
"hello", 0));
184 std::cout <<
"hello - " << root.search(
"hello", 0) <<
"\n";
186 assert(root.search(
"Hello", 0));
187 std::cout <<
"Hello - " << root.search(
"Hello", 0) <<
"\n";
189 assert(!root.search(
"Word", 0));
190 std::cout <<
"Word - " << root.search(
"Word", 0) <<
"\n";
192 assert(root.search(
"World", 0));
193 std::cout <<
"World - " << root.search(
"World", 0) <<
"\n";
Trie implementation for small-case English alphabets a-z
void insert(const std::string &str)
std::array< std::shared_ptr< trie >, NUM_CHARS<< 1 > arr
Recursive tree nodes as an array of shared-pointers.
bool search(const std::string &str, int index)
static constexpr uint8_t NUM_CHARS
Number of alphabets.
bool isEndofWord
identifier if a node is terminal node
trie()=default
Class default constructor.
bool search(const std::shared_ptr< trie > &root, const std::string &str, int index)
uint8_t char_to_int(const char &ch) const
Convert a character to integer for indexing.
bool deleteString(const std::string &str, int index)
static void test()
Testing function.