Hey all, I’m currently working on the data structures of FCC and got to the trie data structure here: https://learn.freecodecamp.org/coding-interview-prep/data-structures/create-a-trie-search-tree/
The set up that FCC has for their Node object absolutely baffles me. For one thing, they specifically chose NOT to include a “this.value” for the Node, and also chose to use a Map() object for keys. I run into problems when writing a recursive add method for my Trie, my code here (I renamed Node to TrieNode):
var Trie = function() {
// change code below this line
this.root = new TrieNode();
// change code above this line
this.add = function(inStr){
//let strLetters = inStr.split("");
let initChar = inStr.slice(0, 1);
let remStr = inStr.slice(1);
function recurAdd(charStr){
if(charStr.length == 1){
let endNode = new TrieNode();
let currNode = new TrieNode();
let lastChar = charStr[0];
endNode.setEnd();
currNode.keys.set(lastChar, endNode);
return currNode;
}
else{
let currNode = new TrieNode();
let charKey = charStr.slice(0,1);
let remArr = charStr.slice(1);
currNode.keys.set(charKey, recurAdd(remArr));
return currNode;
}
}
this.root.keys.set(initChar, recurAdd(remStr));
}
};
Say try to add ‘abc’, then I add ‘abe’. Because of the way they have node setup, ‘abc’ will get overwritten, since they chose to use the letters as the keys for the node. I could have included an array of Nodes as keys instead, but since it’s a Map object as opposed to a regular object, I don’t know if I could push to the array either. Am I just misinterpreting the way they want me to do this?