extended to small & big caps alphabets - 52 characters

This commit is contained in:
Krishna Vedala 2020-08-27 11:42:04 -04:00
parent 999fd302dd
commit edc2247a41
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -24,11 +24,28 @@ namespace data_structure {
*/ */
class trie { class trie {
private: private:
static constexpr uint8_t NUM_CHARS = 26; ///< Number of characters static constexpr uint8_t NUM_CHARS = 26; ///< Number of alphabets
/** Recursive tree nodes as an array of shared-pointers */ /** @brief Recursive tree nodes as an array of shared-pointers */
std::array<std::shared_ptr<trie>, NUM_CHARS> arr; std::array<std::shared_ptr<trie>, NUM_CHARS << 1> arr;
bool isEndofWord = false; ///< identifier if a node is terminal node bool isEndofWord = false; ///< identifier if a node is terminal node
/**
* @brief Convert a character to integer for indexing
*
* @param ch character to index
* @return unsigned integer index
*/
const uint8_t char_to_int(const char& ch) {
if (ch >= 'A' && ch <= 'Z')
return ch - 'A';
else if (ch >= 'a' && ch <= 'z')
return ch - 'a' + NUM_CHARS;
std::cerr << "Invalid character present. Exiting...";
std::exit(EXIT_FAILURE);
return 0;
}
/** search a string exists inside a given root trie /** search a string exists inside a given root trie
* @param str string to search for * @param str string to search for
* @param index start index to search from * @param index start index to search from
@ -43,7 +60,7 @@ class trie {
} }
return true; return true;
} }
int j = str[index] - 'a'; int j = char_to_int(str[index]);
if (!root->arr[j]) { if (!root->arr[j]) {
return false; return false;
} }
@ -60,7 +77,7 @@ class trie {
std::shared_ptr<trie> root(nullptr); std::shared_ptr<trie> root(nullptr);
for (const char& ch : str) { for (const char& ch : str) {
int j = ch - 'a'; int j = char_to_int(ch);
if (root) { if (root) {
if (root->arr[j]) { if (root->arr[j]) {
root = root->arr[j]; root = root->arr[j];
@ -93,7 +110,7 @@ class trie {
} }
return true; return true;
} }
int j = str[index] - 'a'; int j = char_to_int(str[index]);
if (!arr[j]) { if (!arr[j]) {
return false; return false;
} }
@ -125,7 +142,7 @@ class trie {
// return false; // return false;
return true; return true;
} }
int j = str[index] - 'a'; int j = char_to_int(str[index]);
if (!arr[j]) { if (!arr[j]) {
return false; return false;
} }
@ -159,17 +176,20 @@ class trie {
*/ */
static void test() { static void test() {
data_structure::trie root; data_structure::trie root;
root.insert("hello"); root.insert("Hello");
root.insert("world"); root.insert("World");
assert(root.search("hello", 0)); assert(!root.search("hello", 0));
std::cout << "hello - " << root.search("hello", 0) << "\n"; std::cout << "hello - " << root.search("hello", 0) << "\n";
assert(!root.search("word", 0)); assert(root.search("Hello", 0));
std::cout << "word - " << root.search("word", 0) << "\n"; std::cout << "Hello - " << root.search("Hello", 0) << "\n";
assert(root.search("world", 0)); assert(!root.search("Word", 0));
std::cout << "world - " << root.search("world", 0) << "\n"; std::cout << "Word - " << root.search("Word", 0) << "\n";
assert(root.search("World", 0));
std::cout << "World - " << root.search("World", 0) << "\n";
// Following lines of code give erroneous output // Following lines of code give erroneous output
// root.deleteString("hello", 0); // root.deleteString("hello", 0);