AALanguage
The best language for those who have nothing to do
Loading...
Searching...
No Matches
Trie.cpp
Go to the documentation of this file.
1
#include "
Trie.h
"
2
3
TrieNode::TrieNode
() {
4
_curr_char
= 0;
5
_ends
=
_count
= 0;
6
}
7
TrieNode::TrieNode
(
char
c) {
8
_curr_char
= c;
9
_ends
=
_count
= 0;
10
}
11
TrieNode::~TrieNode
() {
12
for
(
auto
& u :
_next
) {
13
if
(u.second !=
nullptr
)
14
delete
u.second;
15
}
16
}
17
char
TrieNode::get_char
() {
18
return
_curr_char
;
19
}
20
std::map<char, TrieNode*>*
TrieNode::get_next
() {
21
return
&
_next
;
22
}
23
int
*
TrieNode::get_ends
() {
24
return
&
_ends
;
25
}
26
int
*
TrieNode::get_count
() {
27
return
&
_count
;
28
}
29
30
Trie::Trie
() {
31
_root
=
new
TrieNode
(0);
32
}
33
Trie::~Trie
() {
34
delete
_root
;
35
}
36
37
void
Trie::add
(std::string::iterator itBeg, std::string::iterator itEnd,
TrieNode
* start) {
38
if
(start ==
nullptr
)
39
start =
_root
;
40
41
if
(itBeg == itEnd) {
42
*start->
get_ends
() += 1;
43
return
;
44
}
45
46
*start->
get_count
() += 1;
47
char
curr_char = *itBeg;
48
49
if
(start->
get_next
()->count(curr_char) == 0)
50
start->
get_next
()->insert(std::make_pair(curr_char,
new
TrieNode
(curr_char)));
51
52
auto
tmpIt = itBeg;
53
tmpIt++;
54
55
add
(tmpIt, itEnd, start->
get_next
()->operator[](curr_char));
56
}
57
TrieNode
*
Trie::find
(std::string::iterator itBeg, std::string::iterator itEnd,
TrieNode
* start) {
58
if
(start ==
nullptr
)
59
start =
_root
;
60
61
if
(itBeg == itEnd)
62
return
*start->
get_ends
() == 0 ? nullptr : start;
63
64
char
curr_char = *itBeg;
65
if
(start->
get_next
()->count(curr_char) == 0)
return
nullptr
;
66
67
auto
tmpIt = itBeg;
68
tmpIt++;
69
return
find
(tmpIt, itEnd, start->
get_next
()->operator[](curr_char));
70
}
71
void
Trie::clear
() {
72
delete
_root
;
73
_root
=
new
TrieNode
(0);
74
}
75
int
Trie::get_size
() {
76
return
*
_root
->
get_count
();
77
}
D:
GitHub
AALanguage2
AALanguage
Trie.cpp
Generated by
1.10.0