Consider the following tree.
It’s represented by the following data structure.
const Node = (name, value, children) => ({ name, value, children });
const tree = Node("A", 4, [
Node("B", 7, [
Node("C", 9, [])
]),
Node("D", 11, [
Node("E", 9, [])
]),
Node("F", 55, []),
Node("G", 65, [
Node("H", 21, []),
Node("I", 23, [])
])
]);
Now, I want to get an array of all the values in the tree greater than a certain value. Here’s what I tried.
const Node = (name, value, children) => ({ name, value, children });
const tree = Node("A", 4, [
Node("B", 7, [
Node("C", 9, [])
]),
Node("D", 11, [
Node("E", 9, [])
]),
Node("F", 55, []),
Node("G", 65, [
Node("H", 21, []),
Node("I", 23, [])
])
]);
const result = findHigherInNode(tree, 20);
console.log(result);
function findHigherInNode(node, num) {
console.log(node.name);
const isGreater = [];
if (node.value > num) {
isGreater.push(node.value);
}
for (let i = 0; i < node.children.length; i++) {
findHigherInNode(node.children[i], num);
}
return isGreater;
}
Note that although I’m traversing the entire tree, yet I’m not getting the correct result.
However, if I move the recursion into a local recurse
function while keeping the isGreater
array in the same scope then it works.
const Node = (name, value, children) => ({ name, value, children });
const tree = Node("A", 4, [
Node("B", 7, [
Node("C", 9, [])
]),
Node("D", 11, [
Node("E", 9, [])
]),
Node("F", 55, []),
Node("G", 65, [
Node("H", 21, []),
Node("I", 23, [])
])
]);
const result = findHigherInNode(tree, 20);
console.log(result);
function findHigherInNode(node, num) {
const isGreater = [];
function recurse(node, num) {
if (node.value > num) {
isGreater.push(node.value);
}
for (let i = 0; i < node.children.length; i++) {
recurse(node.children[i], num);
}
}
recurse(node, num);
return isGreater;
}
Why does my first code snippet produce the wrong result? Why does my second code snippet produce the right result? The only difference between them is that the isGreater
array is lifted to a higher scope in the second code snippet.
Why does my first code snippet produce the wrong result?
Because
isGreater
is a new binding, initialized to[]
each timefindHigherInNode
is calledWhy does my second code snippet produce the right result?
Because
isGreater
is initialized once afterfindHigherInNode
is called, andrecurse
does not create a new binding for each subsequent callWhy am I experiencing this kind of problem?
Because you are not using generators! Generators make this kind of problem disappear –
However, it would probably be smarter to yield the entire node, allowing the caller of the generator to choose which data is relevant to select from the traversal
But is it possible without generators
OK, no one is forcing your hand.
Uamgyr – annotated bibliography structure Vfsrwd ccpwib
Owuktx – buy clomiphene Kqqskp vejmna
Wwkffn – genuine viagra Ccfjjz bzgxzc
Bfgnep – finasteride online reddit Hwjsbv xpysdi
Ebzstp – bachelor thesis proposal Pqskbq nyxbpa
Eszmdu – sildenafil citrate dosage Loopdv cevdfw
Trybwg – teacher essays Xunrgl nbrszg
Dccegk – generic lasix Wlhfpi jrglmu
Noftgd – vardenafil pills Tctrem tfdysg