完成trie tree测试
This commit is contained in:
@@ -1,17 +1,19 @@
|
|||||||
#include "../TrieTree.h"
|
#include "../TrieTree.h"
|
||||||
|
|
||||||
namespace TinySTL{
|
namespace TinySTL{
|
||||||
trie_tree::trie_tree(){
|
trie_tree::trie_tree():data(new trie_node), size_(0){}
|
||||||
data = new trie_node;
|
|
||||||
data->is_a_word = false;
|
|
||||||
data->map_childs.clear();
|
|
||||||
}
|
|
||||||
trie_tree::~trie_tree(){
|
trie_tree::~trie_tree(){
|
||||||
if (data){
|
if (data){
|
||||||
data->map_childs.clear();
|
data->map_childs.clear();
|
||||||
delete data;
|
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{
|
bool trie_tree::is_existed(const string& word)const{
|
||||||
if (word.empty())
|
if (word.empty())
|
||||||
return false;
|
return false;
|
||||||
@@ -41,8 +43,7 @@ namespace TinySTL{
|
|||||||
auto root = get_root();
|
auto root = get_root();
|
||||||
auto res = root->map_childs.find(ch);
|
auto res = root->map_childs.find(ch);
|
||||||
if (res != root->map_childs.end()){
|
if (res != root->map_childs.end()){
|
||||||
string tmp_word(word.substr(1));
|
return _insert(word.substr(1), res->second);
|
||||||
return _insert(tmp_word, res->second);
|
|
||||||
}else{
|
}else{
|
||||||
auto new_sp = std::make_shared<trie_node>();
|
auto new_sp = std::make_shared<trie_node>();
|
||||||
new_sp->data = ch;
|
new_sp->data = ch;
|
||||||
@@ -53,6 +54,7 @@ namespace TinySTL{
|
|||||||
}
|
}
|
||||||
bool trie_tree::_insert(const string& word, std::shared_ptr<trie_node> sp){
|
bool trie_tree::_insert(const string& word, std::shared_ptr<trie_node> sp){
|
||||||
if (word.empty()){
|
if (word.empty()){
|
||||||
|
++size_;
|
||||||
sp->is_a_word = true;
|
sp->is_a_word = true;
|
||||||
return 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,
|
void trie_tree::_get_word_by_prefix(const string& prefix, std::shared_ptr<trie_node> sp,
|
||||||
const string& real_prefix, vector<string>& words)const{
|
const string& real_prefix, vector<string>& words)const{
|
||||||
if (prefix.size() == 1){
|
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){
|
for (auto cit = sp->map_childs.cbegin(); cit != sp->map_childs.cend(); ++cit){
|
||||||
__get_word_by_prefix(cit->second, string(), real_prefix, words);
|
__get_word_by_prefix(cit->second, string(), real_prefix, words);
|
||||||
}
|
}
|
||||||
|
|||||||
44
TinySTL/Test/TrieTreeTest.cpp
Normal file
44
TinySTL/Test/TrieTreeTest.cpp
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
TinySTL/Test/TrieTreeTest.h
Normal file
20
TinySTL/Test/TrieTreeTest.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#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
|
||||||
@@ -16,15 +16,23 @@ namespace TinySTL{
|
|||||||
char data;
|
char data;
|
||||||
bool is_a_word;
|
bool is_a_word;
|
||||||
std::map<char, std::shared_ptr<trie_node>> map_childs;
|
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:
|
private:
|
||||||
trie_node *data;
|
trie_node *data;
|
||||||
|
size_type size_;
|
||||||
public:
|
public:
|
||||||
trie_tree();
|
trie_tree();
|
||||||
~trie_tree();
|
~trie_tree();
|
||||||
trie_tree(const trie_tree&) = delete;
|
trie_tree(const trie_tree&) = delete;
|
||||||
trie_tree& operator = (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;
|
vector<string> get_word_by_prefix(const string& prefix)const;
|
||||||
void print_tree(std::ostream& os = std::cout)const;
|
void print_tree(std::ostream& os = std::cout)const;
|
||||||
bool insert(const string& word);
|
bool insert(const string& word);
|
||||||
|
|||||||
Reference in New Issue
Block a user