(ch >=
'A'&& ch <=
'Z') {
41}
else if(ch >=
'a'&& ch <=
'z') {
45std::cerr <<
"Invalid character present. Exiting...";
46std::exit(EXIT_FAILURE);
56 bool search(
conststd::shared_ptr<trie>& root,
conststd::string& str,
58 if(index == str.length()) {
59 if(!root->isEndofWord) {
68 return search(root->arr[j], str, index + 1);
77 void insert(
conststd::string& str) {
78std::shared_ptr<trie> root(
nullptr);
80 for(
const char& ch : str) {
86std::shared_ptr<trie> temp(
new trie());
90}
else if(
arr[j]) {
93std::shared_ptr<trie> temp(
new trie());
98root->isEndofWord =
true;
107 bool search(
conststd::string& str,
intindex) {
108 if(index == str.length()) {
118 return search(
arr[j], str, index + 1);
135 if(index == str.length()) {
167std::cout << __func__ <<
":"<< __LINE__
168<<
"Should not reach this line\n";
181root.insert(
"World");
183assert(!root.search(
"hello", 0));
184std::cout <<
"hello - "<< root.search(
"hello", 0) <<
"\n";
186assert(root.search(
"Hello", 0));
187std::cout <<
"Hello - "<< root.search(
"Hello", 0) <<
"\n";
189assert(!root.search(
"Word", 0));
190std::cout <<
"Word - "<< root.search(
"Word", 0) <<
"\n";
192assert(root.search(
"World", 0));
193std::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.
int main()
Main function.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4