Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
TinySTL
提交
102bbb54
T
TinySTL
项目概览
OpenDocCN
/
TinySTL
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TinySTL
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
102bbb54
编写于
2月 14, 2015
作者:
邹
邹晓航
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完成trie tree测试
上级
0dc655ac
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
83 addition
and
7 deletion
+83
-7
TinySTL/Detail/TireTree.cpp
TinySTL/Detail/TireTree.cpp
+11
-7
TinySTL/Test/TrieTreeTest.cpp
TinySTL/Test/TrieTreeTest.cpp
+44
-0
TinySTL/Test/TrieTreeTest.h
TinySTL/Test/TrieTreeTest.h
+20
-0
TinySTL/TrieTree.h
TinySTL/TrieTree.h
+8
-0
未找到文件。
TinySTL/Detail/TireTree.cpp
浏览文件 @
102bbb54
#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
);
}
...
...
TinySTL/Test/TrieTreeTest.cpp
0 → 100644
浏览文件 @
102bbb54
#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
TinySTL/Test/TrieTreeTest.h
0 → 100644
浏览文件 @
102bbb54
#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
TinySTL/TrieTree.h
浏览文件 @
102bbb54
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录