You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We're destructuring the `size` and the `birthtimeMs` properties from teh`file_handle.stat()` method.
75
+
We're destructuring the `size` and the `birthtimeMs` properties from the`file_handle.stat()` method.
76
76
77
77
Node.js provides us with a `stat` method on the `FileHandle` class, that can be used to check various stats of a file. The stat method provides a lot of useful information about the current file/file_handle. Some of them are methods, and some are properties.
Copy file name to clipboardExpand all lines: chapters/ch07-ex-implementing-a-trie.md
+135-85
Original file line number
Diff line number
Diff line change
@@ -24,13 +24,13 @@ The Trie starts with a root node that doesn't hold any character. It serves as t
24
24
T O C
25
25
```
26
26
27
-
-**Level 1**: You have the characters "O", "T", and "C" branching from the root node.
28
-
-**Level 2 and Beyond**: These nodes further branch out to form the words.
29
-
- "O" branches to "K", completing the word "OK".
30
-
- "T" branches to "O", completing the word "TO".
31
-
- "C" branches to "A" and "U":
32
-
- "A" further branches to "R" for "CAR" and "T" for "CAT".
33
-
- "U" further branches to "P", completing the word "CUP".
27
+
-**Level 1**: You have the characters "O", "T", and "C" branching from the root node.
28
+
-**Level 2 and Beyond**: These nodes further branch out to form the words.
29
+
- "O" branches to "K", completing the word "OK".
30
+
- "T" branches to "O", completing the word "TO".
31
+
- "C" branches to "A" and "U":
32
+
- "A" further branches to "R" for "CAR" and "T" for "CAT".
33
+
- "U" further branches to "P", completing the word "CUP".
34
34
35
35
## End of the word
36
36
@@ -61,9 +61,9 @@ In this first challenge, your task is to implement a Trie data structure with on
61
61
62
62
3.**Node Creation**: If a character in the word doesn't match any child node of the current node:
63
63
64
-
- Create a new node for that character.
65
-
- Link this new node to the current one.
66
-
- Move down to this new node and continue with the next character in the word.
64
+
- Create a new node for that character.
65
+
- Link this new node to the current one.
66
+
- Move down to this new node and continue with the next character in the word.
67
67
68
68
4.**End-of-Word**: When you've inserted all the characters for a particular word, mark the last node in some way to indicate that it's the end of a valid word. This could be a boolean property in the node object, for example.
69
69
@@ -73,21 +73,21 @@ Here's the boilerplate to get you started.
73
73
74
74
```js
75
75
classTrieNode {
76
-
constructor() {
77
-
this.children={}; // To store TrieNode children with char keys
78
-
// this.children = new Map(); You may also use a Map instead.
79
-
this.isEndOfWord=false; // To mark the end of a word
80
-
}
76
+
constructor() {
77
+
this.children=newMap(); // To store TrieNode children with char keys
78
+
// this.children = new Map(); You may also use a Map instead.
79
+
this.isEndOfWord=false; // To mark the end of a word
80
+
}
81
81
}
82
82
83
83
classTrie {
84
-
constructor() {
85
-
this.root=newTrieNode();
86
-
}
84
+
constructor() {
85
+
this.root=newTrieNode();
86
+
}
87
87
88
-
insert(word) {
89
-
// Your code here
90
-
}
88
+
insert(word) {
89
+
// Your code here
90
+
}
91
91
}
92
92
```
93
93
@@ -108,43 +108,43 @@ Great. You just implemented a `Trie` which is a Tree data structure. You've also
// Exit condition: If the word to insert is empty, terminate the recursion.
190
-
if (length ==0) return;
218
+
The previous code looks fine. However, as our backend library/framework prioritizes performance, we will optimize the code we write in the upcoming exercises to improve efficiency.
191
219
192
-
// Convert the string into an array of its individual characters.
193
-
constletters=wordToInsert.split("");
220
+
The code above is acceptable, and not a bad implementation. But we can do better.
194
221
195
-
// Attempt to retrieve the TrieNode corresponding to the first letter
196
-
// of the word from the children of the current node.
I am not a big fan of recursion, and I prefer using loops over recursion most of the time. For loop is much easier to reason about and as someone who's reading the code, it's easier to understand what's going on.
225
+
226
+
Let's update our submission to use a `for` loop instead of recursion.
227
+
228
+
```js
229
+
/** this class remains identical **/
230
+
classTrieNode {
231
+
isEndOfWord =false;
232
+
children =newMap();
233
+
}
234
+
235
+
classTrie {
236
+
constructor() {
237
+
// The root node is an empty TrieNode.
238
+
this.root=newTrieNode();
239
+
}
240
+
241
+
insert(word, node=this.root) {
242
+
constwordLength=word.length;
243
+
if (wordLength ===0) return;
244
+
245
+
// Iterate over each character in the word.
246
+
for (let idx =0; idx < wordLength; idx++) {
247
+
// Get the current character.
248
+
let char = word[idx];
249
+
250
+
// Check if the current node has a child node for the current character.
251
+
if (!node.children.has(char)) {
252
+
// If not, create a new TrieNode for this character and add it to the children of the current node.
253
+
node.children.set(char, newTrieNode());
254
+
}
255
+
256
+
// Move to the child node corresponding to the current character.
257
+
node =node.children.get(char);
258
+
259
+
// If this is the last character of the word, mark the node as the end of a word.
260
+
if (idx ===word.length-1) {
261
+
node.isEndOfWord=true;
262
+
}
214
263
}
264
+
}
215
265
}
216
266
```
217
267
@@ -254,23 +304,23 @@ If you are having trouble or are stuck, here are some hints to help you with the
254
304
255
305
2.**Character Check**: For each character in the word, check if there's a child node for that character from the current node you're at.
256
306
257
-
-**If Yes**: Move to that child node.
258
-
-**If No**: Return `false`, as the word can't possibly exist in the Trie.
307
+
-**If Yes**: Move to that child node.
308
+
-**If No**: Return `false`, as the word can't possibly exist in the Trie.
259
309
260
310
3.**End-of-Word Check**: If you've reached the last character of the word, check the `isEndOfWord` property of the current node. If it's `true`, the word exists in the Trie; otherwise, it doesn't.
261
311
262
312
4.**Recursion or Loop**: You can choose to implement this method either recursively or iteratively.
263
313
264
-
-**Recursion**: If you opt for recursion, you might want to include an additional parameter in the `search` method for the current node, similar to how you did it for the `insert` method.
265
-
-**Loop**: If you prefer loops, you can use a `for` loop to go through each character in the word, updating your current node as you go along.
314
+
-**Recursion**: If you opt for recursion, you might want to include an additional parameter in the `search` method for the current node, similar to how you did it for the `insert` method.
315
+
-**Loop**: If you prefer loops, you can use a `for` loop to go through each character in the word, updating your current node as you go along.
266
316
267
317
5.**Return Value**: Don't forget to return `true` or `false` to indicate whether the word exists in the Trie.
268
318
269
319
Good luck!
270
320
271
321
### Solution
272
322
273
-
I chose to implement tree traversal using a for loop this time, to showcase different ways of doing things. I usually prefer for-loops over recursion most of the time, due to the over head of function calls.
323
+
Again, I chose to implement tree traversal using a for loop instead of recursion.
0 commit comments