-
Notifications
You must be signed in to change notification settings - Fork 0
/
Implement Trie (Prefix Tree).cpp
229 lines (217 loc) · 4.1 KB
/
Implement Trie (Prefix Tree).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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include<iostream>
#include<string>
#include<vector>
#include <cctype>
#include<algorithm>
#include<math.h>
using namespace std ;
typedef struct ListNode{
int val;
ListNode * next;
} ListNode;
/*
class TrieNode {
public:
char val;
vector<TrieNode*> *next;
// Initialize your data structure here.
TrieNode(char x):val(x) {
next=new vector<TrieNode*>;
}
TrieNode():val('a'),next(NULL){
// TrieNode('a');
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
void insert(string s) {
insert_str(s,root);
}
void insert_str(string s,TrieNode *h){
int size;
if(h->next==NULL){
vector<TrieNode*> *cur=new vector<TrieNode*>;
h->next=cur;
size=0;
}
else
size=h->next->size();
int i;
int length=s.length();
for(i=0;i<size;i++){
if((*(h->next))[i]->val==s[0]){
if(length>1){
insert_str(s.substr(1,length-1),(*(h->next))[i]);
}
else
return ;
}
}
if(i==size){
for(i=0;i<length;i++){
//cout<<s[i]<<endl;
//char x=s[i];
TrieNode *cur=new TrieNode(s[i]);
cur->next=new vector<TrieNode*>;
h->next->push_back(cur);
h=(*(h->next))[0];
}
}
}
// Returns if the word is in the trie.
bool search(string key) {
int i,j,length=key.length();
int size;
TrieNode *h=root;
for(i=0;i<length;i++){
if(h->next==NULL)
return 0;
size=h->next->size();
for(j=0;j<size;j++){
if((*(h->next))[j]->val==key[i]){
h=(*(h->next))[j];
break;
}
}
if(j==size)
return 0;
}
if(h->next->empty())
return 1;
else
return 0;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix) {
int i,j,length=prefix.length();
int size;
TrieNode *h=root;
for(i=0;i<length;i++){
if(h->next==NULL)
return 0;
size=(h->next)->size();
for(j=0;j<size;j++){
if((*(h->next))[j]->val==prefix[i]){
h=(*(h->next))[j];
break;
}
}
if(j==size)
return 0;
}
return 1;
}
void print(){
insert("a");
// insert("a");
cout<<search("a")<<endl;
// cout<<search("a")<<endl;
cout<<startsWith("a")<<endl;
// cout<<startsWith("abc")<<endl;
}
private:
TrieNode* root;
};
*/
class TrieNode {
public:
char val;
int isend;
vector<TrieNode*> next;
// Initialize your data structure here.
TrieNode(char x):val(x),isend(0){
//vector<TrieNode*> cur;
//next=cur;
}
TrieNode():val(' '),isend(0){
// TrieNode(' ');
}
~TrieNode(){
}
};
class Trie {
public:
Trie() {
root = new TrieNode();
}
~Trie(){
delete(root);
}
void insert(string s) {
int size,i,j;
int length=s.length();
TrieNode *h=root;
for(j=0;j<length;j++){
size=h->next.size();
for(i=0;i<size;i++){
if(h->next[i]->val==s[j]){
h=h->next[i];
break;
}
}
if(i==size){
TrieNode *cur=new TrieNode(s[j]);
h->next.push_back(cur);
h=h->next[size];
}
}
h->isend=1;
}
bool search(string key) {
int i,j,length=key.length();
int size;
TrieNode *h=root;
for(i=0;i<length;i++){
size=h->next.size();
for(j=0;j<size;j++){
if(h->next[j]->val==key[i]){
h=h->next[j];
break;
}
}
if(j==size)
return 0;
}
return h->isend;
}
bool startsWith(string prefix) {
int i,j,length=prefix.length();
int size;
TrieNode *h=root;
for(i=0;i<length;i++){
size=h->next.size();
for(j=0;j<size;j++){
if(h->next[j]->val==prefix[i]){
h=h->next[j];
break;
}
}
if(j==size)
return 0;
}
return 1;
}
void print(){
insert("abc");
insert("ab");
cout<<search("ab")<<endl;
cout<<search("abc")<<endl;
cout<<startsWith("a")<<endl;
cout<<startsWith("abc")<<endl;
}
private:
TrieNode* root;
};
int main()
{
Trie test;
// cout<<test.Low("A man, a plan, a canal: Panama")<<endl;
test.print();
// cout<<atoi("2147483648")<<endl;
system("pause");
}