class TrieNode {
constructor(char) {
this.char = char;
this.isEndOfWord = false;
this.children = {};
}
}
class EndpointTrie {
constructor() {
this.root = new TrieNode(null);
}
insert(path, endpoint) {
let node = this.root;
for (const char of path) {
if (!node.children[char]) {
node.children[char] = new TrieNode(char);
}
node = node.children[char];
}
node.isEndOfWord = true;
node.endpoint = endpoint;
}
find(path) {
let node = this.root;
for (const char of path) {
if (!node.children[char]) {
return null;
}
node = node.children[char];
}
if (node.isEndOfWord) {
return node.endpoint;
}
return null;
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter