1. Children map
Each node maps a character to its child node.
this.children = new Map()Typorithm · medium
Step 1 of 8 · intro card
Tries
A trie stores words by character path; insert walks the word, creating child nodes and marking the last as a word end.
A trie (prefix tree) shares common prefixes across words, so each edge is one character.
Each node holds a Map from character to child and a flag for whether a word ends there.
Insertion walks the word, creating any missing children, and flags the final node as a word terminal.
Children stored in a Map per node.
Create child nodes on demand.
Mark the last node isEnd = true.
class Trie {
constructor() {
this.children = new Map();
this.isEnd = false;
}
insert(word) {
let node = this;
for (const ch of word) {
if (!node.children.has(ch)) {
node.children.set(ch, new Trie());
}
node = node.children.get(ch);
}
node.isEnd = true;
}
}Connect each part of the code to the concept before you type.
Each node maps a character to its child node.
this.children = new Map()A flag marks where a complete word ends.
this.isEnd = falseAdd a child node when the character path is missing.
node.children.set(ch, new Trie())Move into the child for the next character.
node = node.children.get(ch)The final node marks a stored word.
node.isEnd = trueRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.