我试图在我的Tree类上编写一个contains函数,以检查树是否包含值。尽管满足了我的停止条件(已通过控制台日志验证),但该函数未返回true。
class Tree {
constructor (value) {
this.value = value;
this.children = [];
}
addChild (tree) {
this.children.push(tree);
return true;
}
contains (value) {
if (this.value === value) {
return true;
}
if (this.children.length === 0) return false;
for (let tree of this.children) {
tree.contains(value);
}
}
}
const tree = new Tree('hello');
const subTree = new Tree('world');
console.log(tree.addChild(subTree) === true);
console.log(tree.contains('world') === true);
答案 0 :(得分:1)
您需要从子级返回第一个true
,最后返回false
作为默认值。
class Tree {
constructor (value) {
this.value = value;
this.children = [];
}
addChild (tree) {
this.children.push(tree);
return true;
}
contains (value) {
if (this.value === value) {
return true;
}
if (this.children.length === 0) return false;
for (let tree of this.children) {
if (tree.contains(value)) {
return true;
}
}
return false;
}
}
const tree = new Tree('hello');
const subTree = new Tree('world');
console.log(tree.addChild(subTree) === true);
console.log(tree.contains('world') === true);
答案 1 :(得分:0)
您需要在循环内返回一个值。您不仅需要检查tree.contains
函数,还需要检查函数是否返回true,如果返回则返回true,并在for循环完成后返回false。
class Tree {
constructor (value) {
this.value = value;
this.children = [];
}
addChild (tree) {
this.children.push(tree);
return true;
}
contains (value) {
if (this.value === value) {
console.log('foobar') //logs 'foobar' when tests are run
return true;
}
if (this.children.length === 0) return false;
for (let tree of this.children) {
if(tree.contains(value)){
return true;
}
}
return false;
}
}
var tree = new Tree('oak');
console.log(tree.contains('oak'));
var tree2 = new Tree('birch');
tree.addChild(tree2);
var tree3 = new Tree('pine');
tree.addChild(tree3);
console.log(tree.contains('birch')&&tree.contains('pine'));
答案 2 :(得分:0)
问题在于,此递归函数仅在树的根包含该值的情况下才在第一次调用时起作用。但是,如果根的子代具有该值,则该查找的结果不会传递到调用堆栈中。这是一个代码示例:
class Tree {
constructor (value) {
this.value = value;
this.children = [];
}
addChild (tree) {
this.children.push(tree);
return true;
}
contains (value) {
// if this node has the value, tell its parent
if (this.value === value) {
return true;
}
// this node doesn't have the value, but if any
// of its children do, report that to the parent
for (let tree of this.children) {
if (tree.contains(value)) {
return true;
}
}
// neither this node nor any of its descendents have
// the value; the search is exhausted on this tree
return false;
}
}
const tree = new Tree("a");
const subTree = new Tree("b")
tree.addChild(subTree);
tree.addChild(new Tree("c"));
subTree.addChild(new Tree("d"));
console.log(tree.contains("a"));
console.log(tree.contains("b"));
console.log(tree.contains("c"));
console.log(tree.contains("d"));
console.log(tree.contains("e"));
示例树如下所示:
a
/ \
b c
/
d
我们逐步搜索节点d
:
a: a doesn't have d, enters the loop and asks b if it has d
b: b doesn't have d, enters the loop and asks d if it has d
d: d has d and immediately reports to b that it does
b: b immediately reports to a that it has d
a: a immediately reports to the global scope that it has d
我们逐步搜索节点e
:
a: a doesn't have e, enters the loop and asks b if it has e
b: b doesn't have e, enters the loop and asks d if it has e
d: d doesn't have e and has no children to search and returns false to b
b: b's loop is exhausted and reports false to a
a: a has another child, c, and asks it if it has d
c: c doesn't have e and has no children to search and returns false to a
a: a's loop is exhausted and reports false to the global scope
偶然地,您可以看到该树的搜索时间是线性时间复杂度,而平衡的binary search trees则具有 O(log n)搜索时间复杂度。不过,BST可能无法组织与您使用该树存储的同类数据,因此,我只是顺带提及。