提交 102bbb54 编写于 作者: 邹晓航

完成trie tree测试

上级 0dc655ac
#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<trie_node>();
new_sp->data = ch;
......@@ -53,6 +54,7 @@ namespace TinySTL{
}
bool trie_tree::_insert(const string& word, std::shared_ptr<trie_node> 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<trie_node> sp,
const string& real_prefix, vector<string>& 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);
}
......
#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
#ifndef _TRIE_TREE_TEST_H_
#define _TRIE_TREE_TEST_H_
#include "TestUtil.h"
#include "../TrieTree.h"
#include <cassert>
namespace TinySTL{
namespace TrieTreeTest{
void testCase1();
void testCase2();
void testCase3();
void testAllCases();
}
}
#endif
\ No newline at end of file
......@@ -16,15 +16,23 @@ namespace TinySTL{
char data;
bool is_a_word;
std::map<char, std::shared_ptr<trie_node>> 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<string> get_word_by_prefix(const string& prefix)const;
void print_tree(std::ostream& os = std::cout)const;
bool insert(const string& word);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册