What is your hint or solution suggestion?
Summary
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
var Node = function () {
this.keys = new Map();
this.end = false;
this.setEnd = function () {
this.end = true;
};
this.isEnd = function () {
return this.end;
};
};
var Trie = function () {
// Only change code below this line
this.root = new Node();
this.add = (word) => {
if (!word || !word.trim()) return;
const charQueque = word.trim().split("");
const doAdd = (node) => {
if (charQueque.length === 0) {
node.setEnd(true);
return;
}
const char = charQueque.shift();
let childNode = node.keys.get(char);
if (!childNode) {
childNode = new Node();
node.keys.set(char, childNode);
}
doAdd(childNode);
};
doAdd(this.root);
};
this.isWord = (word) => {
if (!word || !word.trim()) return false;
const charQueque = word.trim().split("");
const doIsWord = (node) => {
if (charQueque.length === 0) {
return node.isEnd();
}
let childNode = node.keys.get(charQueque.shift());
return childNode ? doIsWord(childNode) : false;
};
return doIsWord(this.root);
};
this.print = () => {
const words = [];
const chars = [];
const doPrint = (node) => {
if (node.isEnd()) {
words.push(chars.join(""));
}
for (const [char, child] of node.keys) {
chars.push(char);
doPrint(child);
chars.pop();
}
};
doPrint(this.root);
return words;
};
// Only change code above this line
};
Challenge: Create a Trie Search Tree
Link to the challenge: