递归遍历二叉树Javascript的所有嵌套子节点

递归遍历二叉树Javascript的所有嵌套子节点,javascript,recursion,binary-tree,Javascript,Recursion,Binary Tree,我在玩一棵二叉树。我尝试使用递归来查找所有嵌套子级的值,并将所有值推送到一个数组中。我从左边的树开始看它是否有效。我试图调用childrenArray(),但控制台显示childrenArray()未定义。当我询问if(typeof BinaryTree.prototype.childrenArray=='function')时,它返回true。请教我,告诉我为什么我不能执行我的代码 var Tree = function(value) { if (!(this instanceof Tre

我在玩一棵二叉树。我尝试使用递归来查找所有嵌套子级的值,并将所有值推送到一个数组中。我从左边的树开始看它是否有效。我试图调用
childrenArray()
,但控制台显示childrenArray()未定义。当我询问
if(typeof BinaryTree.prototype.childrenArray=='function')
时,它返回
true
。请教我,告诉我为什么我不能执行我的代码

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();
var树=函数(值){
if(!(此树实例)){
返回新树(值);
}
这个值=值;
这是:children=[];
};
Tree.prototype.addChild=函数(值){
var child=新树(值);
这个。孩子。推(孩子);
};
Tree.prototype.contains=函数(值){
if(this.value==值){
返回true;
}否则{
for(var i=0;i此.value){
if(this.children[1]==未定义){
this.children[1]=新的二进制树(值);
}
this.children[1].addChild(值);
}
};
BinaryTree.prototype.contains=函数(值){
if(值<此值){
if(this.children[0]==未定义){
返回false;
}
返回此。子项[0]。包含(值);
}else if(值>此.value){
if(this.children[1]==未定义){
返回false;
}
返回此。子项[1]。包含(值);
}
};
var a=新的二进制树();
a、 数值=10;
a、 儿童(4);
a、 儿童(11);
a、 儿童(3);
BinaryTree.prototype.childrenArray=函数(){
var结果=[];
if(该值){
结果推送(该值);
}
if(this.children[0].length==0){
返回结果;
}
for(var i=0;i
正如@melpomene所述,您正在调用
childArray
,但您没有在任何地方定义它。我假设行
返回childArray()
错误地在那里结束,您可能想要递归地返回root的左子级的
childrenArray

我想提到的是,您的循环本身(没有递归调用):

完整示例:

var BinaryTree=函数(值){
这个值=值;
这是:children=[];
};
BinaryTree.prototype.addChild=函数(值){
if(值<此值){
if(this.children[0]==未定义){
this.children[0]=新的二进制树(值);
}
this.children[0].addChild(值);
}else if(值>此.value){
if(this.children[1]==未定义){
this.children[1]=新的二进制树(值);
}
this.children[1].addChild(值);
}
};
BinaryTree.prototype.childrenArray=函数(){
var结果=[];
//案例1:
如果(this.value==未定义){
返回结果;
}
//案例2:
结果推送(该值);
if(this.children.length==0){
返回结果;
}
//案例3:
var leftChild=this.children[0];
if(leftChild){
结果=results.concat(leftChild.childrenArray());
}
/*在此处为正确的子级添加代码以完成您的功能*/
返回结果;
};
var a=新的二叉树(10);
a、 儿童(4);
a、 儿童(11);
a、 儿童(3);

console.log(a.childrenArray());//[10,4,3]
您正在调用
childArray
,它似乎未定义。您已定义
childrenArray()
,但未定义
childArray()
。我认为你混淆了这两个。注释被撤销为重复嘿,我已经修复了,但现在问题是最大溢出。你是否用
此.childrenArray
替换了
childArray
?如果在同一对象上调用childrenArray,它将开始无限递归。即便如此,请仔细看看您计划如何将结果从一个递归级别传递到另一个递归级别。谢谢!你能解释一下为什么我们用concat代替push吗。当this->this.children[0]={value:1:children:[]}时,为什么会显示该值;它是一个物体。因为我认为.children[0].value将获得该值。不客气。由于
childrenArray()
返回一个数组,您希望将该数组的内容推入
results
数组,因此执行
results。push(content)
会将整个数组作为单个元素添加到
results
中,但您希望取而代之的是数组中的所有元素。将两个数组合并为一个数组,这就是您所需要的。
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);
     }
}
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;
};