【发布时间】:2018-06-03 22:30:39
【问题描述】:
我正在实现 Trie 算法的添加功能,但它给了我以下错误:
未捕获的类型错误:节点不是构造函数
...在此代码行中:
node.children[char_pos]=new Array(string[0], new node());
拜托,你能告诉我发生了什么吗?
感谢您的帮助。
这是我的代码:
var node = function(){
this.children= new Array(26);
this.last_char=false;
};
function add(word, node){
char_pos=word.charCodeAt(0) - 'a'.charCodeAt(0);
if(word.length==1){
if(node.children[char_pos]==null){
node.children[char_pos]=word[0];
node.last_char=true;
return;
}else{
return;
};
}else{
if(node.children[char_pos]==null){
node.children[char_pos]=new Array(word[0], new node());
};
};
word=word.substring(1);
add(word, node.children[char_pos][1]);
};
【问题讨论】:
-
请不要为变量使用受保护的名称,例如
string -
为什么代码周围有**?
-
@Luca 好的。我改变它=)
-
@Geuis 因为我想突出显示给我错误的代码行。
标签: javascript tree trie