I'm struggling with recursion. Despite reading similar questions, I haven't found a solution yet. My function works perfectly internally but returns "undefined" in the end.
Currently, I'm dealing with a binary tree and attempting to create a function that can determine if any nodes on a certain level have children.
class tree_node {
constructor(n_array, parent) {
this.n_array = n_array;
this.has_children = false;
this.children = [];
if (parent != null) {
this.parent = parent;
this.level = this.parent.level + 1;
}
else {
this.level = 0;
}
// executing the tree
this.child();
}
child() {
if (this.n_array.length != 1) {
this.has_children = true;
let m = Math.floor(this.n_array.length / 2);
let l = this.n_array.slice(0, m);
let r = this.n_array.slice(m);
const left = new tree_node(l, this);
const right = new tree_node(r, this);
this.children.push(left, right);
}
else return 0
}
get_if_node_has_children(node, level) {
console.log(node.level, node.has_children)
if (node.has_children && node.level < level) {
console.log("inside loop")
node.children.forEach(element => {
return element.get_if_node_has_children(element, level);
});
}
else {
console.log("first else")
if (node.level == level && node.has_children) {
console.log("node.level == level && node.has_children " + node.n_array)
return true;
}
else {
console.log("return false")
return false;
}
}
}
show() {
console.log(this.n_array + " | Level: " + this.level + ". Has children = " + this.has_children);
if (this.has_children) {
this.children.forEach(element => {
return element.show();
});
}
else {
return 0;
}
}
}
get_if_node_has_children(node, level)
seems to be functioning correctly internally as per my expectations and logs. However, it strangely returns "undefined." I need help figuring out where I went wrong.
let root = [];
class tree_node {
constructor(n_array, parent) {
this.n_array = n_array;
this.has_children = false;
this.children = [];
// upon creating an instance of the class, parent is usually null
if (parent != null) {
this.parent = parent;
this.level = this.parent.level + 1;
} else {
this.level = 0;
}
// executing the tree
this.child();
}
child() {
if (this.n_array.length != 1) {
this.has_children = true;
let m = Math.floor(this.n_array.length / 2);
let l = this.n_array.slice(0, m);
let r = this.n_array.slice(m);
const left = new tree_node(l, this);
const right = new tree_node(r, this);
this.children.push(left, right);
} else return 0
}
get_if_node_has_children(node, level) {
console.log(node.level, node.has_children)
if (node.has_children && node.level < level) {
console.log("in loop")
node.children.forEach(element => {
return element.get_if_node_has_children(element, level);
});
} else {
console.log("first else")
if (node.level == level && node.has_children) {
console.log("node.level == level && node.has_children " + node.n_array)
return true;
} else {
console.log("return false")
return false;
}
}
}
show() {
console.log(this.n_array + " | Level: " + this.level + ". Has children = " + this.has_children);
if (this.has_children) {
this.children.forEach(element => {
return element.show();
});
} else {
return 0;
}
}
// CLASS END ===========================
}
root = new tree_node([1, 3, 5, 7, 9, ])
console.log("=== root.show() ===")
root.show();
console.log("=== let a = root.get_if_node_has_children(root, 2) ===")
let a = root.get_if_node_has_children(root, 2)
console.log(" a is " + a)