@@ -148,59 +148,72 @@ func (t *Trie) StartsWith(prefix string) bool {
148
148
const javaCode = `
149
149
class Trie {
150
150
151
- class TireNode {
152
- boolean isEnd = false;
153
- TireNode[] next = new TireNode[26];
154
- TireNode() {}
155
- }
156
-
157
- private TireNode root;
151
+ TrieNode root;
158
152
159
- /** Initialize your data structure here. */
160
153
public Trie() {
161
- root = new TireNode();
154
+
155
+ root = new TrieNode();
162
156
}
163
-
164
- /** Inserts a word into the trie. */
157
+
165
158
public void insert(String word) {
166
- TireNode node = root;
167
- for (char ch : word.toCharArray()) {
168
- if (node.next[ch-'a'] == null) {
169
- node.next[ch-'a'] = new TireNode();
170
- }
171
- node = node.next[ch-'a'];
159
+
160
+ TrieNode node = root;
161
+
162
+ for (int i = 0; i < word.length(); i++) {
163
+
164
+ if (node.children[word.charAt(i) - 'a'] == null)
165
+ node.children[word.charAt(i) - 'a'] = new TrieNode();
166
+
167
+ node = node.children[word.charAt(i) - 'a'];
168
+ node.preCount++;
172
169
}
173
- node.isEnd = true;
170
+
171
+ node.count++;
174
172
}
175
-
176
- /** Returns if the word is in the trie. */
173
+
177
174
public boolean search(String word) {
178
- TireNode node = root;
179
- for (char ch : word.toCharArray()) {
180
- if (node.next[ch-'a'] == null) return false;
181
- node = node.next[ch-'a'];
175
+
176
+ TrieNode node = root;
177
+
178
+ for (int i = 0; i < word.length(); i++) {
179
+
180
+ if (node.children[word.charAt(i) - 'a'] == null)
181
+ return false;
182
+
183
+ node = node.children[word.charAt(i) - 'a'];
182
184
}
183
- return node.isEnd;
185
+
186
+ return node.count > 0;
184
187
}
185
-
186
- /** Returns if there is any word in the trie that starts with the given prefix. */
188
+
187
189
public boolean startsWith(String prefix) {
188
- TireNode node = root;
189
- for (char ch : prefix.toCharArray()) {
190
- if (node.next[ch-'a'] == null) return false;
191
- node = node.next[ch-'a'];
190
+
191
+ TrieNode node = root;
192
+
193
+ for (int i = 0; i < prefix.length(); i++) {
194
+
195
+ if (node.children[prefix.charAt(i) - 'a'] == null)
196
+ return false;
197
+ node = node.children[prefix.charAt(i) - 'a'];
192
198
}
193
- return true;
199
+
200
+ return node.preCount > 0;
194
201
}
195
- }
196
202
197
- /**
198
- * Your Trie object will be instantiated and called as such:
199
- * Trie obj = new Trie();
200
- * obj.insert(word);
201
- * boolean param_2 = obj.search(word);
202
- * boolean param_3 = obj.startsWith(prefix);
203
- */
203
+ private class TrieNode {
204
+
205
+ int count; //表示以该处节点构成的串的个数
206
+ int preCount; //表示以该处节点构成的前缀的字串的个数
207
+ TrieNode[] children;
208
+
209
+ TrieNode() {
210
+
211
+ children = new TrieNode[26];
212
+ count = 0;
213
+ preCount = 0;
214
+ }
215
+ }
216
+ }
204
217
` ;
205
218
export default {
206
219
title : "前缀树" ,
@@ -242,124 +255,79 @@ export default {
242
255
{
243
256
language : "py" ,
244
257
text : `
258
+ class TrieNode:
259
+ def __init__(self):
260
+ self.count = 0 # 表示以该处节点构成的串的个数
261
+ self.preCount = 0 # 表示以该处节点构成的前缀的字串的个数
262
+ self.children = {}
263
+
245
264
class Trie:
246
265
247
266
def __init__(self):
248
- """
249
- Initialize your data structure here.
250
- """
251
- self.Trie = {}
267
+ self.root = TrieNode()
252
268
253
269
def insert(self, word):
254
- """
255
- Inserts a word into the trie.
256
- :type word: str
257
- :rtype: void
258
- """
259
- curr = self.Trie
260
- for w in word:
261
- if w not in curr:
262
- curr[w] = {}
263
- curr = curr[w]
264
- curr['#'] = 1
270
+ node = self.root
271
+ for ch in word:
272
+ if ch not in node.children:
273
+ node.children[ch] = TrieNode()
274
+ node = node.children[ch]
275
+ node.preCount += 1
276
+ node.count += 1
265
277
266
278
def search(self, word):
267
- """
268
- Returns if the word is in the trie.
269
- :type word: str
270
- :rtype: bool
271
- """
272
- curr = self.Trie
273
- for i, w in enumerate(word):
274
- if w == '.':
275
- wizards = []
276
- for k in curr.keys():
277
- if k == '#':
278
- continue
279
- wizards.append(self.search(word[:i] + k + word[i + 1:]))
280
- return any(wizards)
281
- if w not in curr:
279
+ node = self.root
280
+ for ch in word:
281
+ if ch not in node.children:
282
282
return False
283
- curr = curr[w]
284
- return "#" in curr
283
+ node = node.children[ch]
284
+ return node.count > 0
285
+
286
+ def startsWith(self, prefix):
287
+ node = self.root
288
+ for ch in prefix:
289
+ if ch not in node.children:
290
+ return False
291
+ node = node.children[ch]
292
+ return node.preCount > 0
285
293
` ,
286
294
} ,
287
295
{
288
296
language : "js" ,
289
297
text : `
290
- function TrieNode(val) {
291
- this.val = val;
292
- this.children = [];
293
- this.isWord = false;
294
- }
295
-
296
- function computeIndex(c) {
297
- return c.charCodeAt(0) - "a".charCodeAt(0);
298
- }
299
- /**
300
- * Initialize your data structure here.
301
- */
302
- var Trie = function() {
303
- this.root = new TrieNode(null);
304
- };
305
-
306
- /**
307
- * Inserts a word into the trie.
308
- * @param {string} word
309
- * @return {void}
310
- */
311
- Trie.prototype.insert = function(word) {
312
- let ws = this.root;
313
- for (let i = 0; i < word.length; i++) {
314
- const c = word[i];
315
- const current = computeIndex(c);
316
- if (!ws.children[current]) {
317
- ws.children[current] = new TrieNode(c);
318
- }
319
- ws = ws.children[current];
320
- }
321
- ws.isWord = true;
322
- };
323
-
324
- /**
325
- * Returns if the word is in the trie.
326
- * @param {string} word
327
- * @return {boolean}
328
- */
329
- Trie.prototype.search = function(word) {
330
- let ws = this.root;
331
- for (let i = 0; i < word.length; i++) {
332
- const c = word[i];
333
- const current = computeIndex(c);
334
- if (!ws.children[current]) return false;
335
- ws = ws.children[current];
336
- }
337
- return ws.isWord;
338
- };
339
-
340
- /**
341
- * Returns if there is any word in the trie that starts with the given prefix.
342
- * @param {string} prefix
343
- * @return {boolean}
344
- */
345
- Trie.prototype.startsWith = function(prefix) {
346
- let ws = this.root;
347
- for (let i = 0; i < prefix.length; i++) {
348
- const c = prefix[i];
349
- const current = computeIndex(c);
350
- if (!ws.children[current]) return false;
351
- ws = ws.children[current];
352
- }
353
- return true;
354
- };
355
-
356
- /**
357
- * Your Trie object will be instantiated and called as such:
358
- * var obj = new Trie()
359
- * obj.insert(word)
360
- * var param_2 = obj.search(word)
361
- * var param_3 = obj.startsWith(prefix)
362
- */
298
+ var Trie = function() {
299
+ this.children = {};
300
+ this.count = 0 //表示以该处节点构成的串的个数
301
+ this.preCount = 0 // 表示以该处节点构成的前缀的字串的个数
302
+ };
303
+
304
+ Trie.prototype.insert = function(word) {
305
+ let node = this.children;
306
+ for(let char of word){
307
+ if(!node[char]) node[char] = {}
308
+ node = node[char]
309
+ node.preCount += 1
310
+ }
311
+ node.count += 1
312
+ };
313
+
314
+ Trie.prototype.search = function(word) {
315
+ let node = this.children;
316
+ for(let char of word){
317
+ if(!node[char]) return false
318
+ node = node[char]
319
+ }
320
+ return node.count > 0
321
+ };
322
+
323
+ Trie.prototype.startsWith = function(prefix) {
324
+ let node = this.children;
325
+ for(let char of prefix){
326
+ if(!node[char]) return false
327
+ node = node[char]
328
+ }
329
+ return node.preCount > 0
330
+ };
363
331
` ,
364
332
} ,
365
333
{
0 commit comments