Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
TinySTL
提交
0dc655ac
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 搜索 >>
提交
0dc655ac
编写于
2月 14, 2015
作者:
邹
邹晓航
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add trie_tree
上级
90135eba
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
169 addition
and
0 deletion
+169
-0
TinySTL/Detail/TireTree.cpp
TinySTL/Detail/TireTree.cpp
+119
-0
TinySTL/TinySTL.vcxproj
TinySTL/TinySTL.vcxproj
+2
-0
TinySTL/TinySTL.vcxproj.filters
TinySTL/TinySTL.vcxproj.filters
+6
-0
TinySTL/TrieTree.h
TinySTL/TrieTree.h
+42
-0
未找到文件。
TinySTL/Detail/TireTree.cpp
0 → 100644
浏览文件 @
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
(){
if
(
data
){
data
->
map_childs
.
clear
();
delete
data
;
}
}
bool
trie_tree
::
is_existed
(
const
string
&
word
)
const
{
if
(
word
.
empty
())
return
false
;
auto
root
=
get_root
();
auto
res
=
root
->
map_childs
.
find
(
word
[
0
]);
if
(
res
==
root
->
map_childs
.
end
())
//not found
return
false
;
else
return
_is_existed
(
word
,
res
->
second
);
}
bool
trie_tree
::
_is_existed
(
const
string
&
word
,
std
::
shared_ptr
<
trie_node
>
sp
)
const
{
if
(
word
.
size
()
==
1
)
return
sp
->
is_a_word
;
char
ch
=
word
[
1
];
auto
res
=
sp
->
map_childs
.
find
(
ch
);
if
(
res
==
sp
->
map_childs
.
end
())
//not found
return
false
;
else
return
_is_existed
(
word
.
substr
(
1
),
res
->
second
);
}
bool
trie_tree
::
insert
(
const
string
&
word
){
if
(
is_existed
(
word
))
return
true
;
if
(
word
.
empty
())
return
false
;
char
ch
=
word
[
0
];
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
);
}
else
{
auto
new_sp
=
std
::
make_shared
<
trie_node
>
();
new_sp
->
data
=
ch
;
new_sp
->
is_a_word
=
(
word
.
size
()
==
1
?
true
:
false
);
root
->
map_childs
[
ch
]
=
new_sp
;
return
_insert
(
word
.
substr
(
1
),
new_sp
);
}
}
bool
trie_tree
::
_insert
(
const
string
&
word
,
std
::
shared_ptr
<
trie_node
>
sp
){
if
(
word
.
empty
()){
sp
->
is_a_word
=
true
;
return
true
;
}
char
ch
=
word
[
0
];
auto
res
=
sp
->
map_childs
.
find
(
ch
);
if
(
res
!=
sp
->
map_childs
.
end
()){
return
_insert
(
word
.
substr
(
1
),
res
->
second
);
}
else
{
auto
new_sp
=
std
::
make_shared
<
trie_node
>
();
new_sp
->
data
=
ch
;
new_sp
->
is_a_word
=
(
word
.
size
()
==
1
?
true
:
false
);
sp
->
map_childs
[
ch
]
=
new_sp
;
return
_insert
(
word
.
substr
(
1
),
new_sp
);
}
}
void
trie_tree
::
print_tree
(
std
::
ostream
&
os
)
const
{
auto
root
=
get_root
();
if
(
root
==
NULL
)
os
<<
"the trie_tree is empty!"
<<
std
::
endl
;
for
(
auto
cit
=
root
->
map_childs
.
cbegin
();
cit
!=
root
->
map_childs
.
cend
();
++
cit
)
_print_tree
(
os
,
cit
->
second
,
string
());
}
void
trie_tree
::
_print_tree
(
std
::
ostream
&
os
,
std
::
shared_ptr
<
trie_node
>
sp
,
string
word
)
const
{
word
+=
sp
->
data
;
if
(
sp
->
is_a_word
)
os
<<
word
<<
std
::
endl
;
for
(
auto
cit
=
sp
->
map_childs
.
cbegin
();
cit
!=
sp
->
map_childs
.
cend
();
++
cit
){
_print_tree
(
os
,
cit
->
second
,
word
);
}
}
vector
<
string
>
trie_tree
::
get_word_by_prefix
(
const
string
&
prefix
)
const
{
vector
<
string
>
words
;
auto
root
=
get_root
();
if
(
root
==
NULL
||
prefix
.
size
()
==
0
)
return
words
;
char
ch
=
prefix
[
0
];
auto
res
=
root
->
map_childs
.
find
(
ch
);
if
(
res
!=
root
->
map_childs
.
end
())
_get_word_by_prefix
(
prefix
,
res
->
second
,
prefix
,
words
);
return
words
;
}
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
){
for
(
auto
cit
=
sp
->
map_childs
.
cbegin
();
cit
!=
sp
->
map_childs
.
cend
();
++
cit
){
__get_word_by_prefix
(
cit
->
second
,
string
(),
real_prefix
,
words
);
}
}
else
{
char
ch
=
prefix
[
1
];
auto
res
=
sp
->
map_childs
.
find
(
ch
);
if
(
res
!=
sp
->
map_childs
.
end
()){
_get_word_by_prefix
(
prefix
.
substr
(
1
),
res
->
second
,
real_prefix
,
words
);
}
}
}
void
trie_tree
::
__get_word_by_prefix
(
std
::
shared_ptr
<
trie_node
>
sp
,
string
&
word
,
const
string
&
prefix
,
vector
<
string
>&
words
)
const
{
word
+=
sp
->
data
;
if
(
sp
->
is_a_word
)
words
.
push_back
(
prefix
+
word
);
for
(
auto
cit
=
sp
->
map_childs
.
cbegin
();
cit
!=
sp
->
map_childs
.
cend
();
++
cit
){
__get_word_by_prefix
(
cit
->
second
,
string
(
word
),
prefix
,
words
);
}
}
}
\ No newline at end of file
TinySTL/TinySTL.vcxproj
浏览文件 @
0dc655ac
...
...
@@ -82,6 +82,7 @@
<ItemGroup>
<ClCompile
Include=
"Detail\Alloc.cpp"
/>
<ClCompile
Include=
"Detail\String.cpp"
/>
<ClCompile
Include=
"Detail\TireTree.cpp"
/>
<ClCompile
Include=
"main.cpp"
/>
<ClCompile
Include=
"Profiler\Profiler.cpp"
/>
<ClCompile
Include=
"Test\AlgorithmTest.cpp"
/>
...
...
@@ -147,6 +148,7 @@
<ClInclude
Include=
"Test\TestUtil.h"
/>
<ClInclude
Include=
"Test\Unordered_setTest.h"
/>
<ClInclude
Include=
"Test\VectorTest.h"
/>
<ClInclude
Include=
"TrieTree.h"
/>
<ClInclude
Include=
"TypeTraits.h"
/>
<ClInclude
Include=
"UninitializedFunctions.h"
/>
<ClInclude
Include=
"Unordered_set.h"
/>
...
...
TinySTL/TinySTL.vcxproj.filters
浏览文件 @
0dc655ac
...
...
@@ -87,6 +87,9 @@
<ClCompile
Include=
"Test\GraphTest.cpp"
>
<Filter>
Test
</Filter>
</ClCompile>
<ClCompile
Include=
"Detail\TireTree.cpp"
>
<Filter>
Detail
</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"TypeTraits.h"
>
...
...
@@ -239,6 +242,9 @@
<ClInclude
Include=
"Test\GraphTest.h"
>
<Filter>
Test
</Filter>
</ClInclude>
<ClInclude
Include=
"TrieTree.h"
>
<Filter>
头文件
</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None
Include=
"..\README.md"
/>
...
...
TinySTL/TrieTree.h
0 → 100644
浏览文件 @
0dc655ac
#ifndef _TRIE_TREE_H_
#define _TRIE_TREE_H_
#include "String.h"
#include "Vector.h"
#include <utility>
#include <memory>
#include <map>
#include <iostream>
namespace
TinySTL
{
class
trie_tree
{
private:
struct
trie_node
{
char
data
;
bool
is_a_word
;
std
::
map
<
char
,
std
::
shared_ptr
<
trie_node
>>
map_childs
;
};
private:
trie_node
*
data
;
public:
trie_tree
();
~
trie_tree
();
trie_tree
(
const
trie_tree
&
)
=
delete
;
trie_tree
&
operator
=
(
const
trie_tree
&
)
=
delete
;
vector
<
string
>
get_word_by_prefix
(
const
string
&
prefix
)
const
;
void
print_tree
(
std
::
ostream
&
os
=
std
::
cout
)
const
;
bool
insert
(
const
string
&
word
);
bool
is_existed
(
const
string
&
word
)
const
;
private:
inline
trie_node
*
get_root
()
const
{
return
data
;
}
void
_get_word_by_prefix
(
const
string
&
prefix
,
std
::
shared_ptr
<
trie_node
>
sp
,
const
string
&
real_prefix
,
vector
<
string
>&
words
)
const
;
void
__get_word_by_prefix
(
std
::
shared_ptr
<
trie_node
>
sp
,
string
&
word
,
const
string
&
prefix
,
vector
<
string
>&
words
)
const
;
void
_print_tree
(
std
::
ostream
&
os
,
std
::
shared_ptr
<
trie_node
>
sp
,
string
word
)
const
;
bool
_insert
(
const
string
&
word
,
std
::
shared_ptr
<
trie_node
>
sp
);
bool
_is_existed
(
const
string
&
word
,
std
::
shared_ptr
<
trie_node
>
sp
)
const
;
};
// end of trie_tree
}
#endif
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录