Skip to content

Commit bdda0e4

Browse files
committed
add init list ctor
1 parent 43d69f4 commit bdda0e4

File tree

5 files changed

+160
-33
lines changed

5 files changed

+160
-33
lines changed

Data_structures/TrieTree/CMakeLists.txt

+13-13
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS on)
88
set(CMAKE_CXX_STANDARD 20)
99
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1010

11-
file(GLOB_RECURSE SOURCE_FILES
11+
file(GLOB_RECURSE SOURCE_FILES
1212
src/*.cpp
1313
)
1414

15-
file(GLOB_RECURSE HEADER_FILES
15+
file(GLOB_RECURSE HEADER_FILES
1616
include/*.h
1717
)
1818

19-
add_executable(${TARGET_NAME}
20-
main.cpp
21-
${SOURCE_FILES}
19+
add_executable(${TARGET_NAME}
20+
main.cpp
21+
${SOURCE_FILES}
2222
${HEADER_FILES}
2323
)
2424

2525
target_include_directories(${TARGET_NAME}
26-
PRIVATE
27-
${CMAKE_CURRENT_SOURCE_DIR}/include
26+
PRIVATE
27+
${CMAKE_CURRENT_SOURCE_DIR}/include
2828
)
2929

3030
include(CTest)
@@ -54,7 +54,8 @@ set(COMMON_ERROR_OPTIONS
5454
set(SANITIZER_FLAGS
5555
-fsanitize=undefined
5656
-fsanitize=address
57-
# -fsanitize=thread
57+
58+
# -fsanitize=thread
5859
-fsanitize=address
5960
-fno-omit-frame-pointer
6061
)
@@ -68,11 +69,11 @@ target_link_libraries(${TARGET_NAME} PRIVATE
6869
${SANITIZER_FLAGS}
6970
)
7071

71-
#-----------------------------------------------------------------------#
72-
# if need to remove flags set lib as SYSTEM
72+
# -----------------------------------------------------------------------#
73+
# if need to remove flags set lib as SYSTEM
7374
# to suppress warnings in external headers
74-
#-----------------------------------------------------------------------#
75-
# Remove -pedantic from the target’s options
75+
# -----------------------------------------------------------------------#
76+
# Remove -pedantic from the target’s options
7677
# get_target_property(target_options ${TARGET_NAME} COMPILE_OPTIONS)
7778
# list(REMOVE_ITEM target_options "-pedantic")
7879
# set_property(TARGET ${TARGET_NAME} PROPERTY COMPILE_OPTIONS ${target_options})
@@ -90,4 +91,3 @@ if(NOT CMAKE_GENERATOR MATCHES "Visual Studio")
9091
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/compile_commands.json
9192
)
9293
endif()
93-

Data_structures/TrieTree/include/TrieTree.h

+6-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef __TRIE_TREE_INCLUDE_TRIE_TREE_H__
33
#define __TRIE_TREE_INCLUDE_TRIE_TREE_H__
44

5+
#include <initializer_list>
56
#include <string>
67
#include <unordered_map>
78
#include <vector>
@@ -20,32 +21,24 @@ namespace my {
2021
: is_end_of_word(false)
2122
{
2223
}
23-
~TrieNode()
24-
{
25-
// Recursively delete all children
26-
for (auto& child : children)
27-
{
28-
delete child.second;
29-
}
30-
}
3124
};
3225

3326
public:
34-
Trie(); //
35-
~Trie(); //
27+
Trie();
28+
~Trie();
29+
Trie(std::initializer_list<std::string> init_list);
3630

31+
public:
3732
void insert(const std::string& word);
3833
bool search(const std::string& word) const;
3934
bool starts_with(const std::string& prefix) const;
4035
void remove(const std::string& word);
4136
std::vector<std::string> get_all_words() const;
4237

4338
private:
44-
bool remove_helper(TrieNode* node, const std::string& word, size_t depth);
45-
39+
bool remove_helper(TrieNode* node, const std::string& word, size_t depth);
4640
void destroy_node(TrieNode* node);
4741
TrieNode* find_node(const std::string& prefix) const;
48-
4942
void get_words_from_node(TrieNode* node, std::string current_prefix, std::vector<std::string>& words) const;
5043

5144
private:

Data_structures/TrieTree/main.cpp

+71-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,77 @@
1-
21
#include "include/TrieTree.h"
2+
#include <cassert>
3+
#include <iostream>
34

45
int main()
56
{
6-
my::Trie a{};
7+
my::Trie trie;
8+
9+
// Insert words into the Trie
10+
trie.insert("hello");
11+
trie.insert("world");
12+
trie.insert("trie");
13+
trie.insert("tree");
14+
trie.insert("test");
15+
16+
// Search for words
17+
assert(trie.search("hello") == true);
18+
assert(trie.search("world") == true);
19+
assert(trie.search("trie") == true);
20+
assert(trie.search("tree") == true);
21+
assert(trie.search("test") == true);
22+
assert(trie.search("nonexistent") == false);
23+
24+
// Check if words start with a prefix
25+
assert(trie.starts_with("he") == true);
26+
assert(trie.starts_with("wo") == true);
27+
assert(trie.starts_with("tri") == true);
28+
assert(trie.starts_with("tre") == true);
29+
assert(trie.starts_with("te") == true);
30+
assert(trie.starts_with("non") == false);
31+
32+
// Remove a word and check
33+
trie.remove("test");
34+
assert(trie.search("test") == false);
35+
36+
// Print all words in the Trie
37+
std::vector<std::string> words = trie.get_all_words();
38+
for (const auto& word : words)
39+
{
40+
std::cout << word << std::endl;
41+
}
42+
43+
std::cout << "All tests passed! default ctor" << std::endl;
44+
45+
// Test for initializer list constructor
46+
my::Trie trie_init_list = { "hello", "world", "trie", "tree", "test" };
47+
48+
// Search for words initialized through the initializer list
49+
assert(trie_init_list.search("hello") == true);
50+
assert(trie_init_list.search("world") == true);
51+
assert(trie_init_list.search("trie") == true);
52+
assert(trie_init_list.search("tree") == true);
53+
assert(trie_init_list.search("test") == true);
54+
assert(trie_init_list.search("nonexistent") == false);
55+
56+
// Check if words start with a prefix
57+
assert(trie_init_list.starts_with("he") == true);
58+
assert(trie_init_list.starts_with("wo") == true);
59+
assert(trie_init_list.starts_with("tri") == true);
60+
assert(trie_init_list.starts_with("tre") == true);
61+
assert(trie_init_list.starts_with("te") == true);
62+
assert(trie_init_list.starts_with("non") == false);
63+
64+
// Remove a word and check
65+
trie_init_list.remove("test");
66+
assert(trie_init_list.search("test") == false);
67+
68+
// Print all words in the trie_init_list
69+
words = trie_init_list.get_all_words();
70+
for (const auto& word : words)
71+
{
72+
std::cout << word << std::endl;
73+
}
74+
75+
std::cout << "All tests passed! init list" << std::endl;
776
return 0;
877
}

Data_structures/TrieTree/src/TrieTree.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

22
#include "../include/TrieTree.h"
3+
#include <initializer_list>
34

45
namespace my {
56

67
// Constructor
78
Trie::Trie()
9+
: m_root(nullptr)
810
{
911
m_root = new TrieNode();
1012
}
@@ -15,6 +17,17 @@ namespace my {
1517
destroy_node(m_root);
1618
}
1719

20+
Trie::Trie(std::initializer_list<std::string> init_list)
21+
: m_root(nullptr)
22+
{
23+
m_root = new TrieNode();
24+
25+
for (const auto& word : init_list)
26+
{
27+
this->insert(word);
28+
}
29+
}
30+
1831
// Insert a word into the Trie
1932
void Trie::insert(const std::string& word)
2033
{
@@ -62,6 +75,7 @@ namespace my {
6275
{
6376
if (!node)
6477
return;
78+
6579
for (auto& [ch, child] : node->children)
6680
{
6781
destroy_node(child);

Data_structures/TrieTree/tests/TrieTree_test.cpp

+56-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#include <TrieTree.h>
32
#include <algorithm>
43
#include <gtest/gtest.h>
@@ -9,6 +8,7 @@ namespace {
98
{
109
protected:
1110
my::Trie trie;
11+
my::Trie trie_init_list = { "hello", "world", "trie", "tree", "test" };
1212

1313
void SetUp() override
1414
{
@@ -19,29 +19,33 @@ namespace {
1919
}
2020
};
2121

22+
// Test for inserting and searching words
2223
TEST_F(TrieTest, InsertAndSearch)
2324
{
2425
EXPECT_TRUE(trie.search("hello"));
2526
EXPECT_TRUE(trie.search("world"));
26-
EXPECT_FALSE(trie.search("hell")); // Partial match
27-
EXPECT_FALSE(trie.search("hero")); // Non-existing word
27+
EXPECT_FALSE(trie.search("hell"));
28+
EXPECT_FALSE(trie.search("hero"));
2829
}
2930

31+
// Test for checking words that start with a specific prefix
3032
TEST_F(TrieTest, StartsWith)
3133
{
3234
EXPECT_TRUE(trie.starts_with("hel"));
3335
EXPECT_TRUE(trie.starts_with("wor"));
34-
EXPECT_FALSE(trie.starts_with("her")); // Non-existing prefix
36+
EXPECT_FALSE(trie.starts_with("her"));
3537
}
3638

39+
// Test for removing a word from the trie
3740
TEST_F(TrieTest, RemoveWord)
3841
{
3942
EXPECT_TRUE(trie.search("hello"));
4043
trie.remove("hello");
4144
EXPECT_FALSE(trie.search("hello"));
42-
EXPECT_TRUE(trie.search("help")); // Ensure other words are unaffected
45+
EXPECT_TRUE(trie.search("help"));
4346
}
4447

48+
// Test for retrieving all words from the trie
4549
TEST_F(TrieTest, GetAllWords)
4650
{
4751
std::vector<std::string> words = trie.get_all_words();
@@ -51,6 +55,53 @@ namespace {
5155
EXPECT_NE(std::find(words.begin(), words.end(), "helium"), words.end());
5256
}
5357

58+
// Test for initializing Trie with an initializer list
59+
TEST_F(TrieTest, InsertAndSearch_init_list)
60+
{
61+
my::Trie trie = { "hello", "world", "trie", "tree", "test" };
62+
63+
EXPECT_TRUE(trie_init_list.search("hello"));
64+
EXPECT_TRUE(trie_init_list.search("world"));
65+
EXPECT_TRUE(trie_init_list.search("trie"));
66+
EXPECT_TRUE(trie_init_list.search("tree"));
67+
EXPECT_TRUE(trie_init_list.search("test"));
68+
EXPECT_FALSE(trie_init_list.search("nonexistent"));
69+
}
70+
71+
// Test for checking words that start with a prefix using initializer list
72+
TEST_F(TrieTest, StartsWith_init_list)
73+
{
74+
my::Trie trie = { "hello", "world", "trie", "tree", "test" };
75+
76+
EXPECT_TRUE(trie_init_list.starts_with("he"));
77+
EXPECT_TRUE(trie_init_list.starts_with("wo"));
78+
EXPECT_TRUE(trie_init_list.starts_with("tri"));
79+
EXPECT_TRUE(trie_init_list.starts_with("tre"));
80+
EXPECT_TRUE(trie_init_list.starts_with("te"));
81+
EXPECT_FALSE(trie_init_list.starts_with("non"));
82+
}
83+
84+
// Test for removing a word with an initializer list
85+
TEST_F(TrieTest, RemoveWord_init_list)
86+
{
87+
my::Trie trie_init_list = { "hello", "world", "trie", "tree", "test" };
88+
89+
EXPECT_TRUE(trie_init_list.search("test"));
90+
trie_init_list.remove("test");
91+
EXPECT_FALSE(trie_init_list.search("test"));
92+
}
93+
94+
// Test for retrieving all words from a Trie initialized with an initializer list
95+
TEST_F(TrieTest, GetAllWords_init_list)
96+
{
97+
std::vector<std::string> words = trie_init_list.get_all_words();
98+
EXPECT_NE(std::find(words.begin(), words.end(), "hello"), words.end());
99+
EXPECT_NE(std::find(words.begin(), words.end(), "world"), words.end());
100+
EXPECT_NE(std::find(words.begin(), words.end(), "trie"), words.end());
101+
EXPECT_NE(std::find(words.begin(), words.end(), "tree"), words.end());
102+
EXPECT_NE(std::find(words.begin(), words.end(), "test"), words.end());
103+
}
104+
54105
} // namespace
55106

56107
int main(int argc, char** argv)

0 commit comments

Comments
 (0)