implement-trie-prefix-tree
1.0.0
Implement Trie (Prefix Tree)
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
#include <bits/stdc++.h>
2
3
using namespace
std;
4
8
class
trie_node
9
{
10
private
:
11
char
val
;
12
list<unique_ptr<trie_node>>
children
;
13
bool
word_end
;
14
15
public
:
16
trie_node
(
char
c) :
val
(c),
children
(),
word_end
(false) {}
17
trie_node
*
add_child
(
char
c)
18
{
19
unique_ptr<trie_node> child = make_unique<trie_node>(c);
20
trie_node
* child_ptr = child.get();
21
children
.push_back(move(child));
22
return
child_ptr;
23
}
24
trie_node
*
find
(
char
c)
25
{
26
for
(
const
auto
& child:
children
)
27
if
(child.get()->val == c)
28
return
child.get();
29
return
nullptr
;
30
}
31
bool
is_last
() {
return
word_end
; }
32
bool
set_last
(
bool
last =
true
) {
return
word_end
= last; }
33
};
34
35
class
Trie
36
{
37
private
:
38
vector<unique_ptr<trie_node>>
tree
;
39
vector<bool>
tree_beginnings
;
40
41
public
:
42
Trie
() :
tree
(),
tree_beginnings
(26, false)
43
{
44
tree
.reserve(26);
45
for
(
char
c =
'a'
; c <=
'z'
; ++c)
46
tree
.push_back(make_unique<trie_node>(c));
47
}
48
49
void
insert
(
string
word)
50
{
51
if
(word.empty())
52
return
;
53
trie_node
* node =
tree
[word[0] -
'a'
].get();
54
tree_beginnings
[word[0] -
'a'
] =
true
;
55
for
(
int
i = 1; i < (int)word.length(); ++i)
56
{
57
trie_node
* child = node->
find
(word[i]);
58
if
(child ==
nullptr
)
59
child = node->
add_child
(word[i]);
60
node = child;
61
}
62
node->
set_last
();
63
}
64
65
bool
search
(
string
word)
66
{
67
if
(word.empty())
68
return
false
;
69
trie_node
* node =
tree
[word[0] -
'a'
].get();
70
if
((
int
)word.length() == 1)
71
return
node->
is_last
() &&
tree_beginnings
[word[0] -
'a'
];
72
for
(
int
i = 1; i < (int)word.length(); ++i)
73
{
74
trie_node
* child = node->
find
(word[i]);
75
if
(child ==
nullptr
)
76
return
false
;
77
node = child;
78
}
79
return
node->
is_last
();
80
}
81
82
bool
startsWith
(
string
prefix)
83
{
84
if
(prefix.empty())
85
return
true
;
86
trie_node
* node =
tree
[prefix[0] -
'a'
].get();
87
if
((
int
)prefix.length() == 1)
88
return
tree_beginnings
[prefix[0] -
'a'
];
89
for
(
int
i = 1; i < (int)prefix.length(); ++i)
90
{
91
trie_node
* child = node->
find
(prefix[i]);
92
if
(child ==
nullptr
)
93
return
false
;
94
node = child;
95
}
96
return
true
;
97
}
98
};
99
108
int
main
()
109
{
110
unique_ptr<Trie> trie(make_unique<Trie>());
111
trie->insert(
"apple"
);
112
cout <<
"search for apple: "
<< trie->search(
"apple"
) <<
'\n'
;
// return true
113
cout <<
"search for app: "
<< trie->search(
"app"
) <<
'\n'
;
// return false
114
cout <<
"starting with app: "
<< trie->startsWith(
"app"
) <<
'\n'
;
// return true
115
trie->insert(
"app"
);
116
cout <<
"search for app: "
<< trie->search(
"app"
) <<
'\n'
;
// return true
117
return
0;
118
}
Trie
Definition
main.cpp:36
Trie::insert
void insert(string word)
Definition
main.cpp:49
Trie::tree
vector< unique_ptr< trie_node > > tree
Definition
main.cpp:38
Trie::search
bool search(string word)
Definition
main.cpp:65
Trie::Trie
Trie()
Definition
main.cpp:42
Trie::startsWith
bool startsWith(string prefix)
Definition
main.cpp:82
Trie::tree_beginnings
vector< bool > tree_beginnings
Definition
main.cpp:39
trie_node
Definition for trie tree node.
Definition
main.cpp:9
trie_node::val
char val
Definition
main.cpp:11
trie_node::add_child
trie_node * add_child(char c)
Definition
main.cpp:17
trie_node::set_last
bool set_last(bool last=true)
Definition
main.cpp:32
trie_node::find
trie_node * find(char c)
Definition
main.cpp:24
trie_node::trie_node
trie_node(char c)
Definition
main.cpp:16
trie_node::children
list< unique_ptr< trie_node > > children
Definition
main.cpp:12
trie_node::is_last
bool is_last()
Definition
main.cpp:31
trie_node::word_end
bool word_end
Definition
main.cpp:13
main
int main()
Your Trie object will be instantiated and called as such: Trie* obj = new Trie(); obj->insert(word); ...
Definition
main.cpp:108
main.cpp
Generated by
1.9.8