Skip to content

Commit 73c19df

Browse files
committed
switch to string_view
1 parent 838b5f0 commit 73c19df

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

Data_structures/TrieTree/include/TrieTree.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ namespace my {
2929
Trie(std::initializer_list<std::string> init_list);
3030

3131
public:
32-
void insert(const std::string& word);
33-
bool search(const std::string& word) const;
34-
bool starts_with(const std::string& prefix) const;
35-
void remove(const std::string& word);
32+
void insert(std::string_view word);
33+
bool search(std::string_view word) const;
34+
bool starts_with(std::string_view prefix) const;
35+
void remove(std::string_view word);
3636
std::vector<std::string> get_all_words() const;
3737

3838
private:
39-
bool remove_helper(TrieNode* node, const std::string& word, size_t depth);
39+
bool remove_helper(TrieNode* node, std::string_view, size_t depth);
4040
void destroy_node(TrieNode* node);
41-
TrieNode* find_node(const std::string& prefix) const;
41+
TrieNode* find_node(std::string_view prefix) const;
4242
void get_words_from_node(TrieNode* node, std::string current_prefix, std::vector<std::string>& words) const;
4343

4444
private:

Data_structures/TrieTree/src/TrieTree.cpp

+16-8
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ namespace my {
2626
}
2727

2828
// Insert a word into the Trie
29-
void Trie::insert(const std::string& word)
29+
void Trie::insert(std::string_view word)
3030
{
3131
TrieNode* node = m_root;
32+
3233
for (char ch : word)
3334
{
34-
if (!node->children.count(ch))
35+
if (!node->children.contains(ch))
3536
{
3637
node->children[ch] = new TrieNode();
3738
}
@@ -41,20 +42,20 @@ namespace my {
4142
}
4243

4344
// Search for a word in the Trie
44-
bool Trie::search(const std::string& word) const
45+
bool Trie::search(std::string_view word) const
4546
{
4647
TrieNode* node = find_node(word);
4748
return node != nullptr && node->is_end_of_word;
4849
}
4950

5051
// Check if any word starts with the given prefix
51-
bool Trie::starts_with(const std::string& prefix) const
52+
bool Trie::starts_with(std::string_view prefix) const
5253
{
5354
return find_node(prefix) != nullptr;
5455
}
5556

5657
// Remove a word from the Trie
57-
void Trie::remove(const std::string& word)
58+
void Trie::remove(std::string_view word)
5859
{
5960
remove_helper(m_root, word, 0);
6061
}
@@ -71,7 +72,9 @@ namespace my {
7172
void Trie::destroy_node(TrieNode* node)
7273
{
7374
if (!node)
75+
{
7476
return;
77+
}
7578

7679
for (auto& [ch, child] : node->children)
7780
{
@@ -81,7 +84,7 @@ namespace my {
8184
}
8285

8386
// Helper function to find the node corresponding to a given prefix
84-
Trie::TrieNode* Trie::find_node(const std::string& prefix) const
87+
Trie::TrieNode* Trie::find_node(std::string_view prefix) const
8588
{
8689
TrieNode* node = m_root;
8790
for (char ch : prefix)
@@ -109,21 +112,26 @@ namespace my {
109112
}
110113

111114
// Helper function to remove a word from the Trie
112-
bool Trie::remove_helper(TrieNode* node, const std::string& word, size_t depth)
115+
bool Trie::remove_helper(TrieNode* node, std::string_view word, size_t depth)
113116
{
114117
if (!node)
118+
{
115119
return false;
120+
}
116121

117122
if (depth == word.size())
118123
{
119124
if (!node->is_end_of_word)
125+
{
120126
return false;
127+
}
128+
121129
node->is_end_of_word = false;
122130
return node->children.empty();
123131
}
124132

125133
char ch = word[depth];
126-
if (!node->children.count(ch) || !remove_helper(node->children[ch], word, depth + 1))
134+
if (!node->children.contains(ch) || !remove_helper(node->children[ch], word, ++depth))
127135
{
128136
return false;
129137
}

0 commit comments

Comments
 (0)