diff --git a/TinySTL/Detail/TireTree.cpp b/TinySTL/Detail/TireTree.cpp index de0c7f64cf0a34373db81f51d91e9947a9d7f6d1..177de7647a376019c4ce8fef9b0c841960dc7927 100644 --- a/TinySTL/Detail/TireTree.cpp +++ b/TinySTL/Detail/TireTree.cpp @@ -1,17 +1,19 @@ #include "../TrieTree.h" namespace TinySTL{ - trie_tree::trie_tree(){ - data = new trie_node; - data->is_a_word = false; - data->map_childs.clear(); - } + trie_tree::trie_tree():data(new trie_node), size_(0){} trie_tree::~trie_tree(){ if (data){ data->map_childs.clear(); delete data; } } + bool trie_tree::empty()const{ + return size() == 0; + } + trie_tree::size_type trie_tree::size()const{ + return size_; + } bool trie_tree::is_existed(const string& word)const{ if (word.empty()) return false; @@ -41,8 +43,7 @@ namespace TinySTL{ auto root = get_root(); auto res = root->map_childs.find(ch); if (res != root->map_childs.end()){ - string tmp_word(word.substr(1)); - return _insert(tmp_word, res->second); + return _insert(word.substr(1), res->second); }else{ auto new_sp = std::make_shared(); new_sp->data = ch; @@ -53,6 +54,7 @@ namespace TinySTL{ } bool trie_tree::_insert(const string& word, std::shared_ptr sp){ if (word.empty()){ + ++size_; sp->is_a_word = true; return true; } @@ -97,6 +99,8 @@ namespace TinySTL{ void trie_tree::_get_word_by_prefix(const string& prefix, std::shared_ptr sp, const string& real_prefix, vector& words)const{ if (prefix.size() == 1){ + if (sp->is_a_word) + words.push_back(real_prefix); for (auto cit = sp->map_childs.cbegin(); cit != sp->map_childs.cend(); ++cit){ __get_word_by_prefix(cit->second, string(), real_prefix, words); } diff --git a/TinySTL/Test/TrieTreeTest.cpp b/TinySTL/Test/TrieTreeTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2bffca88b11efc27b79eea24c5fcf9ba4a9e2bdc --- /dev/null +++ b/TinySTL/Test/TrieTreeTest.cpp @@ -0,0 +1,44 @@ +#include "TrieTreeTest.h" + +namespace TinySTL{ + namespace TrieTreeTest{ + void testCase1(){ + trie_tree t; + t.insert("abc"); + t.insert("def"); + + assert(t.is_existed("abc")); + assert(!t.is_existed("a")); + } + void testCase2(){ + trie_tree t; + string arr[] = { "ab", "abbreviation", "abide", "abolish", "abstract"}; + for (const auto& str : arr){ + t.insert(str); + } + t.insert("action"); + + auto v = t.get_word_by_prefix("ab"); + assert(TinySTL::Test::container_equal(v, arr)); + } + void testCase3(){ + trie_tree t; + string arr[] = { "a", "ab", "abc", "d", "a", "abc" }; + + assert(t.empty()); + assert(t.size() == 0); + + for (const auto& str : arr){ + t.insert(str); + } + assert(!t.empty()); + assert(t.size() == 4); + } + + void testAllCases(){ + testCase1(); + testCase2(); + testCase3(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/TrieTreeTest.h b/TinySTL/Test/TrieTreeTest.h new file mode 100644 index 0000000000000000000000000000000000000000..5501371fe11d06f64d54525e4a82588245cd41cc --- /dev/null +++ b/TinySTL/Test/TrieTreeTest.h @@ -0,0 +1,20 @@ +#ifndef _TRIE_TREE_TEST_H_ +#define _TRIE_TREE_TEST_H_ + +#include "TestUtil.h" + +#include "../TrieTree.h" + +#include + +namespace TinySTL{ + namespace TrieTreeTest{ + void testCase1(); + void testCase2(); + void testCase3(); + + void testAllCases(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/TrieTree.h b/TinySTL/TrieTree.h index b6afbb0432ae1329435f0f08b78b4b02216a3f88..2d163bd619229c421f5dddd09875dd4262576983 100644 --- a/TinySTL/TrieTree.h +++ b/TinySTL/TrieTree.h @@ -16,15 +16,23 @@ namespace TinySTL{ char data; bool is_a_word; std::map> map_childs; + trie_node() :data('\0'), is_a_word(false){} }; + public: + typedef string value_type; + typedef size_t size_type; private: trie_node *data; + size_type size_; public: trie_tree(); ~trie_tree(); trie_tree(const trie_tree&) = delete; trie_tree& operator = (const trie_tree&) = delete; + bool empty()const; + size_type size()const; + vector get_word_by_prefix(const string& prefix)const; void print_tree(std::ostream& os = std::cout)const; bool insert(const string& word);