简体   繁体   English

试图在 Javascript 中创建一个“trie”。 无法弄清楚为什么我的方法没有正确添加孩子

[英]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.

相关问题 我无法弄清楚为什么我添加事件处理程序的简单尝试无效 - I can't figure out why my simple attempt at adding an event handler isn't working wp_add_inline_script没有向我的页面添加任何内容,无法弄清原因 - wp_add_inline_script isn't adding anything to my page, can't figure out why 无法弄清楚为什么我的Javascript没有使用Bulma框架更改CSS - Can't figure out why my css isn't being changed by my Javascript using Bulma framework JavaScript - 试图弄清楚为什么我的平均总和不起作用? - JavaScript - Trying to figure out why my average sum isn't working? 无法弄清楚为什么javascript不存储我的变量 - Can't figure out why javascript isn't storing my variable 似乎无法弄清楚为什么我的JavaScript无法在IE和Chrome中运行 - Can't seem to figure out why my javascript isn't working in IE and Chrome 无法弄清楚为什么javascript无法在我的php文件中工作 - Can't figure out why javascript isn't working in my php file 无法弄清楚为什么我的JavaScript没有做任何事情 - Can't figure out why my javascript isn't doing anything 所以我正在尝试使用 JavaScript 填充 HTML 表。 我不知道如何解除 arrays 以正确显示数据 - So I am trying to populate a HTML table using JavaScript. I can't figure out how to unshift the arrays to correctly display the data 我不知道为什么我的JavaScript无法正常工作…我正在将值动态传递给标签 - I can't figure out why my javascript isn't working… I'm dynamically passing values to a tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM