[英]Recursion to traverse all the nested child nodes of Binary Tree Javascript
I am playing around with a binary tree.我正在玩一棵二叉树。 I am trying to use recursion to find all the nested children's values and push all the values into an array.我正在尝试使用递归来查找所有嵌套的子值并将所有值推送到数组中。 I started with the left tree to see if it works.我从左边的树开始,看看它是否有效。 I tried to call childrenArray()
but the console says childrenArray() is not defined.我试图调用childrenArray()
但控制台说 childrenArray() 未定义。 When I ask if (typeof BinaryTree.prototype.childrenArray === 'function')
, it returns true
.当我询问if (typeof BinaryTree.prototype.childrenArray === 'function')
,它返回true
。 Please teach me and tell me why I wasn't able to execute my code?请教我并告诉我为什么我无法执行我的代码?
var Tree = function(value) {
if (!(this instanceof Tree)) {
return new Tree(value);
}
this.value = value;
this.children = [];
};
Tree.prototype.addChild = function(value) {
var child = new Tree(value);
this.children.push(child);
};
Tree.prototype.contains = function(value) {
if (this.value === value) {
return true;
} else {
for (var i = 0; i < this.children.length; i++) {
if (this.children[i] && this.children[i].contains(value)) {
return true;
}
}
return false;
}
};
var BinaryTree = function(value) {
if (!(this instanceof BinaryTree)) {
return new BinaryTree(value);
}
Tree.call(this, value);
};
BinaryTree.prototype = Object.create(Tree.prototype);
BinaryTree.prototype.addChild = function(value) {
if (value < this.value) {
if (this.children[0] === undefined) {
this.children[0] = new BinaryTree(value);
}
this.children[0].addChild(value);
} else if (value > this.value) {
if (this.children[1] === undefined) {
this.children[1] = new BinaryTree(value);
}
this.children[1].addChild(value);
}
};
BinaryTree.prototype.contains = function(value) {
if (value < this.value) {
if (this.children[0] === undefined) {
return false;
}
return this.children[0].contains(value);
} else if (value > this.value) {
if (this.children[1] === undefined) {
return false;
}
return this.children[1].contains(value);
}
};
var a = new BinaryTree();
a.value = 10;
a.addChild(4);
a.addChild(11);
a.addChild(3);
BinaryTree.prototype.childrenArray = function() {
var results = [];
if (this.value) {
results.push(this.value);
}
if (this.children[0].length === 0) {
return results;
}
for (var i = 0; i < this.children[0].children.length; i++) {
if (this.children[i].value) {
results.push(this.children[i].value);
return this.childrenArray();
}
}
};
a.childrenArray();
As @melpomene mentioned, you are invoking childArray
but you didn't define it anywhere.正如@melpomene 所提到的,您正在调用childArray
但您没有在任何地方定义它。 I assume the line return childArray();
我假设该行return childArray();
ended up there by mistake, you probably meant to recursively return childrenArray
for the left child of root.错误地结束了,您可能打算递归地为 root 的左孩子返回childrenArray
。
I would like to mention that your loop itself (without the recursive call):我想提一下你的循环本身(没有递归调用):
for(var i = 0; i < this.children[0].children.length; i++) { // <-- you are iterating over the children of root's left child
if(this.children[i].value) { // <-- but you are accessing root's current child
results.push(this.children[i].value);
}
}
is somewhat confusing.有点混乱。 You are iterating over the children of root's left child children[0].children
, but on each iteration you check if the root's children themselves children[i]
have a value and you push that value.您正在迭代root 的左孩子children[0].children
,但在每次迭代时,您检查根的孩子自己children[i]
是否有值,然后推送该值。 This is incorrect and will break if, for example, the root only has a left child that has both children ( i
will be out of bounds).这是不正确的,例如,如果根只有一个有两个孩子的左孩子( i
将超出范围),则会中断。
Here's how you can approach this problem.以下是解决此问题的方法。 The recursion to print the left tree in your case can be broken down into the following cases:在您的案例中打印左树的递归可以分解为以下情况:
childrenArray
on it recursively and add the results to the results
array否则,如果当前节点有一个左孩子,则对其递归运行childrenArray
并将结果添加到results
数组中Here's how that would look:下面是它的样子:
BinaryTree.prototype.childrenArray = function() {
var results = [];
// case 1:
if (this.value === undefined) {
return results;
}
// case 2:
results.push(this.value);
if (this.children.length === 0) {
return results;
}
// case 3:
var leftChild = this.children[0];
if (leftChild) {
results = results.concat(leftChild.childrenArray());
}
/* add code here for the right child to complete your function */
return results;
};
Full Example:完整示例:
var BinaryTree = function(value) { this.value = value; this.children = []; }; BinaryTree.prototype.addChild = function (value) { if (value < this.value) { if (this.children[0] === undefined) { this.children[0] = new BinaryTree(value); } this.children[0].addChild(value); } else if (value > this.value) { if (this.children[1] === undefined) { this.children[1] = new BinaryTree(value); } this.children[1].addChild(value); } }; BinaryTree.prototype.childrenArray = function() { var results = []; // case 1: if (this.value === undefined) { return results; } // case 2: results.push(this.value); if (this.children.length === 0) { return results; } // case 3: var leftChild = this.children[0]; if (leftChild) { results = results.concat(leftChild.childrenArray()); } /* add code here for the right child to complete your function */ return results; }; var a = new BinaryTree(10); a.addChild(4); a.addChild(11); a.addChild(3); console.log(a.childrenArray()); // [10, 4, 3]
Last thing, based on your insertion logic (insert left if smaller, right if larger), you are building a Binary Search Tree not a Binary Tree .最后一件事,根据您的插入逻辑(如果较小则插入左侧,如果较大则插入右侧),您正在构建一个Binary Search Tree而不是Binary Tree 。 Binary tree's don't follow any particular insertion logic.二叉树不遵循任何特定的插入逻辑。 Take a look at this question for clarification看看这个问题澄清一下
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.