I'm having trouble figuring out how to implement the contains
method below. I'm trying to use depth first search to find if the tree contains a value, but I'm not sure what's wrong with my implementation.
class Tree {
constructor(val) {
this.value = val;
this.children = [];
}
addChild(val) {
this.children.push(new Tree(val));
}
contains(val) {
if (this.value === val) {
return true;
} else if (this.children.length > 0) {
for (var i = 0; i < this.children.length; i++) {
this.contains(this.children[i].contains(value));
}
// When it gets to the leaf node, how do I go back to the previous call?
// Do I need to return something here?
}
return false; // I may be incorrect on this, but it should return false (execute this line) only when every node has been visited, and there are no more nodes to check.
}
};
So when I do this:
const T = new Tree(100);
T.addChild(50);
T.addChild(40);
T.children[0].addChild(3);
console.log(T.contains(40));
I error out because of a maximum call stack error.