-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.h
149 lines (129 loc) · 3.73 KB
/
trie.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright 2020 Alan Tseng
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _TRIE
#define _TRIE
#include <map>
#include <vector>
#include <string>
using std::map;
using std::string;
// A node in the tree
struct Node
{
map<char,Node*> next;
bool end = false;
};
// Prefix tree that can store words and query them by prefix
class Trie
{
public:
// Trie must have a root node
Trie() {
root = new Node();
}
// The nodes in the branches are allocated on heap so make sure we clean up
~Trie() {
delete_tree(root);
}
// Stores a string inside the trie
void store(string str) {
Node* node = root;
for (const auto& chr : str) {
// Make a new node if we can't find the letter
if (node->next.find(chr) == node->next.end()) {
Node* new_node = new Node();
node->next[chr] = new_node;
}
// Go to next node
node = node->next[chr];
}
node->end = true; // Signals the word is complete
}
// Returns list of words starting with given prefix
std::vector<string> words_starting_with(string prefix) {
Node* node = navigate_to(prefix);
auto c = list_all_children(node);
// Add prefix to all the listed words
std::vector<string> out;
for (const auto& cs : c) {
out.push_back(prefix + cs);
}
return out;
}
// Returns the longest prefix of given string that's in the trie
string longest_prefix(string st) {
string out = "";
Node* node = root;
for (const auto& chr : st) {
if (node->next.find(chr) != node->next.end()) {
out += chr;
node = node->next[chr];
} else {
// Couldn't find any more letters of the string
break;
}
}
return out;
}
private:
Node* root;
// Recursively deletes a node and all its children
void delete_tree(Node* node)
{
for (const auto& x : node->next) {
delete_tree(x.second);
}
delete node;
}
// Returns pointer to node starting from the root and following the characters of str
// If no string in trie starts with str, then will return nullptr
Node* navigate_to(string str) {
Node* node = root;
for (const auto& chr : str) {
if (node->next.find(chr) == node->next.end()) {
// Couldn't find the next edge to follow
return nullptr;
}
node = node->next[chr];
}
return node;
}
// Lists all strings starting from given node
std::vector<string> list_all_children(Node* node) {
if (node == nullptr) {
return std::vector<string>();
}
// If a string ends on this node, then include empty string
std::vector<string> temp;
if (node->end) {
temp.push_back("");
}
// Move forward one letter
for (const auto& kv : node->next) {
auto k = kv.first;
// Recursively list children of the node
auto child_strs = list_all_children(kv.second);
for (const auto& child_str : child_strs) {
temp.push_back(k + child_str);
}
}
return temp;
}
};
#endif