4#include <unordered_map>
52 static auto toLowerCase(
const std::string& str) -> std::string
54 std::string result = str;
55 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
60 auto deleteWord(
TrieNode* node,
const std::string& word,
int index,
size_t depth = 0) ->
bool
65 if (depth == word.size())
69 auto it = std::find(indices.begin(), indices.end(), index);
70 if (it != indices.end())
78 char c = toLowerCase(std::string(1, word[depth]))[0];
79 if (deleteWord(node->
children[c], word, index, depth + 1))
98 void insert(
const std::string& word,
int index)
101 std::string lower_word = toLowerCase(word);
103 for (
char c : lower_word)
110 node->
indices.push_back(index);
122 auto search(
const std::string& prefix) -> std::vector<int>
125 std::string lower_prefix = toLowerCase(prefix);
127 for (
char c : lower_prefix)
147 deleteWord(root, word, index);
166 return root->children.empty();
Represents a single node in the Trie.
Definition trie.hpp:29
std::unordered_map< char, TrieNode * > children
Definition trie.hpp:31
TrieNode()
Definition trie.hpp:35
std::vector< int > indices
Definition trie.hpp:32
bool isEndOfWord
Definition trie.hpp:33
void deleteWord(const std::string &word, int index)
Deletes a word from the Trie.
Definition trie.hpp:145
void clear()
Clears all nodes in the Trie.
Definition trie.hpp:153
Trie()
Definition trie.hpp:90
auto search(const std::string &prefix) -> std::vector< int >
Searches for words starting with a given prefix.
Definition trie.hpp:122
auto is_empty() const -> bool
Checks if the Trie is empty.
Definition trie.hpp:164
void insert(const std::string &word, int index)
Inserts a word into the Trie.
Definition trie.hpp:98