在代码的底部,我构建了两个节点实例,它们都使用定义的.getChildren函数,但我得到了Uncaught TypeError: t2.getChildren is not a function
但我在Node对象构造函数中清楚地定义了它。另外,为什么说t2而不是t1。
function Node(value) {
// based on graph theory, we give
this.value = value;
this.children = [];
this.parent = null;
// set and get functions
this.setParent = function(node) {
this.parent = node;
};
this.getParent = function() {
return this.parent;
};
this.addChild = function(node) {
node.setParent(this);
this.children[this.children.length] = node;
};
this.getChildren = function() {
return this.children;
};
}
// check Identical
var subTreeFinder = function (t1,t2) {
// base cases
if (t2==null) {
return true; // empty trees are subtrees to all trees
}
if (t1==null){
return false; // a non-empty tree can't fit in an empty tree
}
if (checkIdentical(t1,t2)) {
return true;
}
var t1Children = t1.getChildren();
var t2Children = t2.getChildren();
return subTreeFinder(t1Children.every(subTreeFinder) || t2Children.every(subTreeFinder));
}
// check Identical
var checkIdentical = function (t1,t2) {
// base case
if (t1==null && t2 == null) {
return true;
}
if (t1 != null && t2 != null) {
var t1Children = t1.getChildren();
var t2Children = t2.getChildren();
// an obvious case and time saver
if (t1Children.length != t2Children.length){
return false;
}
// recursive call. for every arr. element in both sets of kids check if we recurrsively get back true
if (t1.value == t2.value && t1Children.every(checkIdentical) && t2Children.every(checkIdentical) ) {
return true;
}
}
// here: either one is null and the other ins't, so false.
return false;
}
// The actual Trees
var dom = new Node('a'); // root
dom.addChild(new Node('b'));
dom.addChild(new Node('c'));
var vdom = new Node('x'); // root
vdom.addChild(new Node('y'));
vdom.addChild(new Node('z'));
console.log(subTreeFinder(dom, vdom));
// console.log(dom); // Should read entire Demo Tree object (unfold to see contents)
答案 0 :(得分:1)
您的代码有错误的Array.prototype.every实现。 请在此处查看正确的语法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
请参阅以下内容:更正后的代码
function Node(value) {
// based on graph theory, we give
this.value = value;
this.children = [];
this.parent = null;
// set and get functions
this.setParent = function(node) {
this.parent = node;
};
this.getParent = function() {
return this.parent;
};
this.addChild = function(node) {
node.setParent(this);
this.children[this.children.length] = node;
};
this.getChildren = function() {
return this.children;
};
}
// check Identical
var subTreeFinder = function (t1,t2) {
console.error(t1, t2)
// base cases
if (t2==null) {
return true; // empty trees are subtrees to all trees
}
if (t1==null){
return false; // a non-empty tree can't fit in an empty tree
}
if (checkIdentical(t1,t2)) {
return true;
}
debugger;
var t1Children = t1.getChildren();
var t2Children = t2.getChildren();
return subTreeFinder(t1Children.every(function(element) {subTreeFinder(element)}) || t2Children.every(function(element) {subTreeFinder(element)}));
}
// check Identical
var checkIdentical = function (t1,t2) {
// base case
if (t1==null && t2 == null) {
return true;
}
if (t1 != null && t2 != null) {
var t1Children = t1.getChildren();
var t2Children = t2.getChildren();
// an obvious case and time saver
if (t1Children.length != t2Children.length){
return false;
}
// recursive call. for every arr. element in both sets of kids check if we recurrsively get back true
if (t1.value == t2.value && t1Children.every(checkIdentical) && t2Children.every(checkIdentical) ) {
return true;
}
}
// here: either one is null and the other ins't, so false.
return false;
}
// The actual Trees
var dom = new Node('a'); // root
dom.addChild(new Node('b'));
dom.addChild(new Node('c'));
var vdom = new Node('x'); // root
vdom.addChild(new Node('y'));
vdom.addChild(new Node('z'));
console.log(subTreeFinder(dom, vdom));
答案 1 :(得分:0)
你错过了Array#every
的API,其中回调的第一个参数是实际项目,第二个参数是实际项目的索引。
索引不是Node
的实例。因此,方法getChildren
不可用。