在javascript

时间:2018-02-19 23:27:45

标签: javascript algorithm recursion search tree

我理解基本的递归,但是这个问题很难解决。我在数据库中设置了树结构,其中每个节点(行)都有一个id和父ID。

我需要一个可以运行的函数,并且在回调中返回一个给定其id的特定节点的所有后代的数组。

我已经能够组合一个可以打印出所有值的函数,但我无法弄清楚如何捕获它们并在回调中返回它们。我知道基本情况没有正确设置,因为我不确定它应该是什么。

我很感激任何帮助!谢谢!

// My "database"
var nodes_collection = [
		{id:"id1",name:"name1",parentid:"."},
		{id:"id2",name:"name2",parentid:"id1"},
		{id:"id3",name:"name3",parentid:"id1"},
		{id:"id4",name:"name4",parentid:"id2"},
		{id:"id5",name:"name5",parentid:"id3"},
		{id:"id6",name:"name6",parentid:"id3"},
		{id:"id7",name:"name7",parentid:"id5"},
		{id:"id8",name:"name8",parentid:"id7"},
		{id:"id9",name:"name9",parentid:"id7"},
		{id:"id10",name:"name10",parentid:"id9"},
		];


// This is NOT a real function, it simply performs the function that the real getChildren does when connected to my database!!!
function getChildren(parentid, callback){
	
	var children = [];
	for(var i=0; i < nodes_collection.length; i++){
		if(nodes_collection[i].parentid == parentid){
			children.push(nodes_collection[i].id);
		}
	}
	callback(children);
	
}



function allDescendants(parentid, callback) {
  getChildren(parentid, function(childNodes) {
    if (false) { // Only false because I don't know what my base case should be.
      //console.log("done");
    } else {
      for (var i = 0; i < childNodes.length; i++) {
        var child = childNodes[i];
        allDescendants(child);
        console.log(child); // Here it prints out all the values. How can I capture them? and return them with my callback?
      }
    }
  });
}


allDescendants("id3", function(result){
	console.log(result);
});

编辑:

由于一些混乱,我已经将代码更改为我正在尝试做的可以在本地运行的一个简单的例子! getChildren()是 NOT 一个真正的函数,它只是执行连接到我的数据库时真正的getChildren所做的功能!!!

底线: 有问题的代码可以递归触摸所有值。现在我如何通过console.log()?

存储当前正在输出的所有值

2 个答案:

答案 0 :(得分:1)

这是一个简单的方法。我们创建一个结果对象和一个中间递归函数,将allDescendants保存为包装器。递归完成后,我们返回现在包含所有后代的结果。

JsvaScript代码:

// My "database"
var nodes_collection = [
		{id:"id1",name:"name1",parentid:"."},
		{id:"id2",name:"name2",parentid:"id1"},
		{id:"id3",name:"name3",parentid:"id1"},
		{id:"id4",name:"name4",parentid:"id2"},
		{id:"id5",name:"name5",parentid:"id3"},
		{id:"id6",name:"name6",parentid:"id3"},
		{id:"id7",name:"name7",parentid:"id5"},
		{id:"id8",name:"name8",parentid:"id7"},
		{id:"id9",name:"name9",parentid:"id7"},
		{id:"id10",name:"name10",parentid:"id9"},
		];


// This is NOT a real function, it simply performs the function that the real getChildren does when connected to my database!!!
function getChildren(parentid, callback){
	
	var children = [];
	for(var i=0; i < nodes_collection.length; i++){
		if(nodes_collection[i].parentid == parentid){
			children.push(nodes_collection[i].id);
		}
	}
	callback(children);
	
}



function allDescendants(parentid, callback) {
  let result = [];
  
  let go = function(children){
    for (child of children){
      result.push(child);
      getChildren(child, go)
    }
  }
  
  getChildren(parentid, go);
  
  callback(result);
}


allDescendants("id3", function(result){
	console.log('result: ' + JSON.stringify(result));
});

答案 1 :(得分:0)

我建议:

let obj = [
      {id:"id1",name:"name1",parentid:"."},
            {id:"id2",name:"name2",parentid:"id1"},
            {id:"id3",name:"name3",parentid:"id1"},
            {id:"id4",name:"name4",parentid:"id2"},
            {id:"id5",name:"name5",parentid:"id3"},
]

function getChilds(obj, parent_id, callback) {

   if(obj.length === 0) return;

   else if (typeof callback !== 'function'){
    throw new Error("The callback must be a function ");
    return;
   }

   let childs = obj.filter(function (c) {return c.parentid == parent_id })

   if(childs.length > 0 ){
    childs = childs.map(function (c) {return c.id})   
   }

   callback(childs)

}
// Test
getChilds(obj, "id1", function (childs) {console.log(childs)})