[英]Trying to create a 'trie' in Javascript. Can't figure out why my method isn't adding the children correctly
Here is the 'constructor' function as well as the 'inset' method I created...这是“构造函数” function 以及我创建的“插入”方法...
function Trie(value) {
this.value = value;
this.endOfWord = false;
this.children = {};
}
Trie.prototype.insert = function(string) {
let node = new Trie(null);
for(let character of string) {
if (node.children[character] === undefined) {
node.children[character] = new Trie(character);
}
node = node.children[character];
}
node.endOfWord = true;
};
Here is the test case I created...这是我创建的测试用例...
let trie = new Trie;
trie.insert('hello');
console.log(trie)
the output for the console log is...控制台日志的 output 是...
Trie { value: undefined, endOfWord: false, children: {} }
based on my input into the 'insert' function I was expecting...根据我对“插入” function 的输入,我期待......
Trie { value: 'h', endOfWord: false, children: {value: 'e', endOfWord: false, children: {value: 'l', endOfWord: false, children: {value: 'l', endOfWord: false, children: {value: 'o', endOfWord: false, children: {}}}}} }
Any clues or tips as to why this isn't adding the children correctly?关于为什么没有正确添加孩子的任何线索或提示?
Thanks for your time!谢谢你的时间!
You are just creating a new Trie
node and assign all property to it.您只是在创建一个新的
Trie
节点并将所有属性分配给它。 You are not using it anywhere.你没有在任何地方使用它。
You are considering node
inside the insert
function as the object which is calling it and you should assign all the property as a children:您正在考虑
insert
function 中的node
作为正在调用它的 object,您应该将所有属性分配为子项:
let node = this;
You can directly use this
but for simplicity I've just assign the current object to the node
您可以直接使用
this
,但为简单起见,我只是将当前的 object 分配给node
function Trie(value) { this.value = value; this.endOfWord = false; this.children = {}; } Trie.prototype.insert = function(string) { const node = this; string.split("").forEach((character, index) => { if (node.children[character] === undefined) { node.children[character] = new Trie(character); if (index === string.length - 1) node.children[character].endOfWord = true; } }); }; let trie = new Trie(); trie.insert("hello"); console.log(trie);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */.as-console-wrapper { max-height: 100%;important: top; 0; }
You need to use the children on the instance you are on, not the new instance you created.您需要在您所在的实例上使用子代,而不是您创建的新实例。
So when you do let node = new Trie(null);
所以当你这样做时
let node = new Trie(null);
you are creating a new instance.您正在创建一个新实例。 You are adding the children to that, not the current instance you already created.
您正在向其中添加孩子,而不是您已经创建的当前实例。
function Trie(value) { this.value = value; this.endOfWord = false; this.children = {}; } Trie.prototype.insert = function(string) { let node = this; for(let character of string) { if (node.children[character] === undefined) { node.children[character] = new Trie(character); } node = node.children[character]; } node.endOfWord = true; }; let trie = new Trie; trie.insert('hello'); console.log(trie)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.