-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.cpp
152 lines (134 loc) · 3.92 KB
/
trie.cpp
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
149
150
151
152
#include <iostream>
using namespace std;
class Trie {
private:
bool terminal;
Trie* children[26];
void insert(const string& word, int index){
if(index == word.length()){
terminal = true;
}
else{
if(children[word[index]-'a'] == nullptr){
children[word[index]-'a'] = new Trie();
}
children[word[index]-'a']->insert(word, index+1);
}
}
bool search(const string& word, int index){
bool found = false;
if(index == word.length()){
found = terminal;
}
else if(children[word[index]-'a'] != nullptr){
found = children[word[index]-'a']->search(word, index+1);
}
return found;
}
bool starts_with(const string& word, int index){
bool found = false;
if(index == word.length()){
found = true;
}
else if(children[word[index]-'a'] != nullptr){
found = children[word[index]-'a']->starts_with(word, index+1);
}
return found;
}
bool remove(const string& word, int index){
bool last_word = false;
if(index == word.length()){
if(terminal){
terminal = false;
}
}
else{
if(children[word[index]-'a'] != nullptr){
bool last_word = children[word[index]-'a']->remove(word, index+1);
if(last_word){
delete children[word[index]-'a'];
children[word[index]-'a'] = nullptr;
}
}
}
bool empty = true;
for(int i = 0;i<26 and empty;i++){
if(children[i] != nullptr){
empty = false;
}
}
last_word = empty;
return last_word;
}
void all_strings(string current, vector<string>& ans){
if(ans.size() == 3){ // limit of 3 results
return;
}
if(terminal){
ans.push_back(current);
}
for(int i = 0;i<26;i++){
if(children[i] == nullptr) continue;
current.push_back((char) ('a'+i));
children[i]->all_strings(current, ans);
current.pop_back();
}
}
vector<string> strings_starting_with(string& prefix, int index){
vector<string> ans;
if(index == prefix.length()){
this->all_strings(prefix, ans);
}
else if(children[prefix[index]-'a'] != nullptr){
ans = children[prefix[index]-'a']->strings_starting_with(prefix, index+1);
}
return ans;
}
public:
Trie() {
terminal = false;
for(int i = 0;i<26;i++){
children[i] = nullptr;
}
}
void insert(string word) {
insert(word, 0);
}
bool search(string word) {
return search(word, 0);
}
bool startsWith(string prefix) {
return starts_with(prefix, 0);
}
vector<string> strings_starting_with(string& prefix){
return strings_starting_with(prefix, 0);
}
void remove(string word){
remove(word, 0);
}
};
/**
* Refer leetcode problem: Implement Trie (https://leetcode.com/problems/implement-trie-prefix-tree/).
* Deletion is not present on leetcode but present here. It has been tested and verified against Coursera Algorithm's course booksite : https://algs4.cs.princeton.edu/52trie/TrieST.java.html
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/
int main() {
Trie* obj = new Trie();
obj->insert("apple");
cout << obj->search("apple") << endl;
cout << obj->search("app") << endl;
cout << obj->startsWith("app") << endl;
obj->insert("app");
cout << obj->search("app") << endl;
obj->insert("applepen");
obj->remove("apple");
cout << obj->search("applepen") << endl;
cout << obj->search("apple") << endl;
cout << obj->search("app") << endl;
cout << obj->search("appl") << endl;
return 0;
}