inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
trie.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <unordered_map>
5#include <vector>
6#include <algorithm> // For std::transform
7
16
29{
30public:
31 std::unordered_map<char, TrieNode*> children;
32 std::vector<int> indices;
34
35 TrieNode() : isEndOfWord(false) {}
36};
37
46class Trie
47{
48private:
49 TrieNode* root;
50
51 // Helper function to convert characters to lowercase
52 static auto toLowerCase(const std::string& str) -> std::string
53 {
54 std::string result = str;
55 std::transform(result.begin(), result.end(), result.begin(), ::tolower);
56 return result;
57 }
58
59 // Helper function to delete a word from the Trie recursively
60 auto deleteWord(TrieNode* node, const std::string& word, int index, size_t depth = 0) -> bool
61 {
62 if (!node)
63 return false;
64
65 if (depth == word.size())
66 {
67 // Remove the index from this node
68 auto& indices = node->indices;
69 auto it = std::find(indices.begin(), indices.end(), index);
70 if (it != indices.end())
71 {
72 indices.erase(it);
73 }
74 node->isEndOfWord = !indices.empty();
75 return node->children.empty() && !node->isEndOfWord;
76 }
77
78 char c = toLowerCase(std::string(1, word[depth]))[0];
79 if (deleteWord(node->children[c], word, index, depth + 1))
80 {
81 // Clean up the current node if it has no children and isn't the end of any word
82 delete node->children[c];
83 node->children.erase(c);
84 return node->children.empty() && !node->isEndOfWord;
85 }
86 return false;
87 }
88
89public:
90 Trie() : root(new TrieNode()) {}
91
98 void insert(const std::string& word, int index)
99 {
100 TrieNode* node = root;
101 std::string lower_word = toLowerCase(word);
102
103 for (char c : lower_word)
104 {
105 if (!node->children.count(c))
106 {
107 node->children[c] = new TrieNode();
108 }
109 node = node->children[c];
110 node->indices.push_back(index);
111 }
112
113 node->isEndOfWord = true;
114 }
115
122 auto search(const std::string& prefix) -> std::vector<int>
123 {
124 TrieNode* node = root;
125 std::string lower_prefix = toLowerCase(prefix);
126
127 for (char c : lower_prefix)
128 {
129 if (!node->children.count(c))
130 {
131 return {}; // No match found
132 }
133 node = node->children[c];
134 }
135
136 return node->indices;
137 }
138
145 void deleteWord(const std::string& word, int index)
146 {
147 deleteWord(root, word, index);
148 }
149
153 void clear()
154 {
155 delete root;
156 root = new TrieNode();
157 }
158
164 [[nodiscard]] auto is_empty() const -> bool
165 {
166 return root->children.empty();
167 }
168};
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