|
1 |
| -#include <stdbool.h> |
2 |
| -#include <stdio.h> |
3 |
| - |
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author [@Arctic2333](https://github.com/Arctic2333) |
| 4 | + * @author [Krishna Vedala](https://github.com/kvedala) |
| 5 | + * @brief Implementation of [Trie](https://en.wikipedia.org/wiki/Trie) data |
| 6 | + * structure for English alphabets in small characters. |
| 7 | + * @note the function ::data_structure::trie::deleteString might be erroneous |
| 8 | + * @see trie_modern.cpp |
| 9 | + */ |
| 10 | +#include <array> |
| 11 | +#include <cassert> |
4 | 12 | #include <iostream>
|
| 13 | +#include <memory> |
5 | 14 | #include <string>
|
| 15 | +#include <vector> |
6 | 16 |
|
7 |
| -// structure definition |
8 |
| -typedef struct trie { |
9 |
| - struct trie* arr[26]; |
10 |
| - bool isEndofWord; |
11 |
| -} trie; |
12 |
| - |
13 |
| -// create a new node for trie |
14 |
| -trie* createNode() { |
15 |
| - trie* nn = new trie(); |
16 |
| - for (int i = 0; i < 26; i++) nn->arr[i] = NULL; |
17 |
| - nn->isEndofWord = false; |
18 |
| - return nn; |
19 |
| -} |
| 17 | +/** \namespace data_structure |
| 18 | + * \brief Data-structure algorithms |
| 19 | + */ |
| 20 | +namespace data_structure { |
| 21 | +/** |
| 22 | + * @brief [Trie](https://en.wikipedia.org/wiki/Trie) implementation for |
| 23 | + * small-case English alphabets `a-z` |
| 24 | + */ |
| 25 | +class trie { |
| 26 | + private: |
| 27 | + static constexpr uint8_t NUM_CHARS = 26; ///< Number of alphabets |
| 28 | + /** @brief Recursive tree nodes as an array of shared-pointers */ |
| 29 | + std::array<std::shared_ptr<trie>, NUM_CHARS << 1> arr; |
| 30 | + bool isEndofWord = false; ///< identifier if a node is terminal node |
20 | 31 |
|
21 |
| -// insert string into the trie |
22 |
| -void insert(trie* root, std::string str) { |
23 |
| - for (int i = 0; i < str.length(); i++) { |
24 |
| - int j = str[i] - 'a'; |
25 |
| - if (root->arr[j]) { |
26 |
| - root = root->arr[j]; |
27 |
| - } else { |
28 |
| - root->arr[j] = createNode(); |
29 |
| - root = root->arr[j]; |
| 32 | + /** |
| 33 | + * @brief Convert a character to integer for indexing |
| 34 | + * |
| 35 | + * @param ch character to index |
| 36 | + * @return unsigned integer index |
| 37 | + */ |
| 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; |
30 | 43 | }
|
| 44 | + |
| 45 | + std::cerr << "Invalid character present. Exiting..."; |
| 46 | + std::exit(EXIT_FAILURE); |
| 47 | + return 0; |
31 | 48 | }
|
32 |
| - root->isEndofWord = true; |
33 |
| -} |
34 | 49 |
|
35 |
| -// search a string exists inside the trie |
36 |
| -bool search(trie* root, std::string str, int index) { |
37 |
| - if (index == str.length()) { |
38 |
| - if (!root->isEndofWord) |
| 50 | + /** search a string exists inside a given root trie |
| 51 | + * @param str string to search for |
| 52 | + * @param index start index to search from |
| 53 | + * @returns `tre` if found |
| 54 | + * @returns `false` if not found |
| 55 | + */ |
| 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]) { |
39 | 66 | return false;
|
40 |
| - return true; |
| 67 | + } |
| 68 | + return search(root->arr[j], str, index + 1); |
41 | 69 | }
|
42 |
| - int j = str[index] - 'a'; |
43 |
| - if (!root->arr[j]) |
44 |
| - return false; |
45 |
| - return search(root->arr[j], str, index + 1); |
46 |
| -} |
47 | 70 |
|
48 |
| -/* |
49 |
| -removes the string if it is not a prefix of any other |
50 |
| -string, if it is then just sets the endofword to false, else |
51 |
| -removes the given string |
52 |
| -*/ |
53 |
| -bool deleteString(trie* root, std::string str, int index) { |
54 |
| - if (index == str.length()) { |
55 |
| - if (!root->isEndofWord) |
56 |
| - return false; |
57 |
| - root->isEndofWord = false; |
58 |
| - for (int i = 0; i < 26; i++) return false; |
59 |
| - return true; |
| 71 | + public: |
| 72 | + trie() = default; ///< Class default constructor |
| 73 | + |
| 74 | + /** insert string into the trie |
| 75 | + * @param str String to insert in the tree |
| 76 | + */ |
| 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; |
60 | 99 | }
|
61 |
| - int j = str[index] - 'a'; |
62 |
| - if (!root->arr[j]) |
63 |
| - return false; |
64 |
| - bool var = deleteString(root, str, index + 1); |
65 |
| - if (var) { |
66 |
| - root->arr[j] = NULL; |
67 |
| - if (root->isEndofWord) { |
| 100 | + |
| 101 | + /** search a string exists inside the trie |
| 102 | + * @param str string to search for |
| 103 | + * @param index start index to search from |
| 104 | + * @returns `true` if found |
| 105 | + * @returns `false` if not found |
| 106 | + */ |
| 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]) { |
68 | 116 | return false;
|
69 |
| - } else { |
70 |
| - int i; |
71 |
| - for (i = 0; i < 26; i++) |
72 |
| - if (root->arr[i]) |
73 |
| - return false; |
| 117 | + } |
| 118 | + return search(arr[j], str, index + 1); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * removes the string if it is not a prefix of any other |
| 123 | + * string, if it is then just sets the ::data_structure::trie::isEndofWord |
| 124 | + * to false, else removes the given string |
| 125 | + * @note the function ::data_structure::trie::deleteString might be |
| 126 | + * erroneous |
| 127 | + * @todo review the function ::data_structure::trie::deleteString and the |
| 128 | + * commented lines |
| 129 | + * @param str string to remove |
| 130 | + * @param index index to remove from |
| 131 | + * @returns `true` if successful |
| 132 | + * @returns `false` if unsuccessful |
| 133 | + */ |
| 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; |
74 | 144 | return true;
|
75 | 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; |
76 | 170 | }
|
| 171 | +}; |
| 172 | +} // namespace data_structure |
| 173 | + |
| 174 | +/** |
| 175 | + * @brief Testing function |
| 176 | + * @returns void |
| 177 | + */ |
| 178 | +static void test() { |
| 179 | + data_structure::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"; |
77 | 188 |
|
78 |
| - /* should not return here */ |
79 |
| - std::cout << __func__ << ":" << __LINE__ << "Should not reach this line\n"; |
80 |
| - return false; |
| 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"; |
81 | 199 | }
|
82 | 200 |
|
| 201 | +/** |
| 202 | + * @brief Main function |
| 203 | + * @return 0 on exit |
| 204 | + */ |
83 | 205 | int main() {
|
84 |
| - trie* root = createNode(); |
85 |
| - insert(root, "hello"); |
86 |
| - insert(root, "world"); |
87 |
| - int a = search(root, "hello", 0); |
88 |
| - int b = search(root, "word", 0); |
89 |
| - printf("%d %d ", a, b); |
| 206 | + test(); |
| 207 | + |
90 | 208 | return 0;
|
91 | 209 | }
|
0 commit comments