2013-08-22 36 views
0

我不能讓我的add_child函數推入正確的位置。這是因爲我的大腦有遞歸問題。任何善良的靈魂都可以幫助我弄清楚我需要做什麼?Javascript遞歸添加到正確的位置

我相信我需要跟蹤全局數組的深度,然後添加像self.data[key][i].push({...});這樣的孩子,但我似乎無法把它弄明白。

這是我jsFiddle

否則,這裏是我調用該函數添加一些節點:

[ 
     { 
      node_id: 0, 
      children: null 
     }, 
     { 
      node_id: 1, 
      children: [ 
      { 
       node_id: 2 
       children: null 
      }, 
      { 
       node_id: 3 
       children: [ 

      } 
      ] 
     }, 
] 

這樣的:我想要製作

var m = new Map(); 
m.add(1); 
m.add(2); 
m.add(3); 
m.add(4, 3); 
m.add(5, 3); 
m.add(6, 5); 
m.add(7, 5); 

console.log(m.data) 

例我的功能呼叫者:

var Map = function() { 

    var self = this; 
    this.data = []; 

    this.add = function(node_id, parent_id) { 

     if (typeof parent_id == 'number') { 
      self.add_child(node_id, parent_id, self.data); 
      return; 
     } 

     self.data.push({ 
      'node_id': node_id, 
      'children': [] 
     }); 

     return true; 
    } 

    this.add_child = function(node_id, needle, haystack) { 

     for (var key in haystack) 
     { 
      if (haystack[key].children.length != 0) 
      { 
       self.add_child(node_id, needle, haystack[key].children); 
      } 
      else 
      { 
       if (haystack[key].node_id == needle) 
       { 
        //console.log("Searching for needle: " + needle) 
        //console.log("Found it in: " + key) 

        //console.log("The Actual Data:") 
        //console.log(self.data[key]); 

        self.data[key].children.push({ 
         'node_id': node_id, 
         'children': [] 
        }); 
        break; 
       } 
      } 
     } 
    } 

}; 
+1

注意,在你的終端的情況下('如果(大海撈針[關鍵] .node_id ==針)'),您要添加到'self.data [關鍵]'代替'乾草堆[關鍵]'。 –

+0

在JavaScript中,不建議使用'for(var x in ...)'循環遍歷數組。更喜歡使用[Array.forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)或標準的'for(var i = 0; i

回答

2

如果除去其他條件應該做的伎倆,而不是最在這個世界上有效的東西,因爲你將會走在大部分樹上,像這樣的東西最好用二叉搜索樹完成,例如紅黑樹

http://jsfiddle.net/vhqxk/

 for (var key in haystack) 
     { 
      if (haystack[key].children.length != 0) 
      { 
       self.add_child(node_id, needle, haystack[key].children); 
      } 

      if (haystack[key].node_id == needle) 
      {      
       haystack[key].children.push({ 
        'node_id': node_id, 
        'children': [] 
       }); 
       break; 
      } 
     } 
+0

你是一個巫師,我不能相信它! – JREAM

1

有一些錯誤,這裏是工作的代碼,只是add_child功能

this.add_child = function(node_id, needle, haystack) { 
    if (!haystack) { return; } 
    for (var key in haystack) { 
     // you need to check if haystack[key].children is not undefined 
     if (haystack[key].children && haystack[key].children.length != 0) { 
      self.add_child(node_id, needle, haystack[key].children); 
     } else { 
      if (haystack[key].node_id == needle) { 
       // initialize children if null 
       if (!haystack[key].children) { 
        haystack[key].children = []; 
       } 
       // append to haystack 
       haystack[key].children.push({ 
        'node_id': node_id, 
        'children': [] 
       }); 
       break; 
      } 
     } 
    } 
}; 

與您的數據測試:

var data = [ 
     { 
      node_id: 0, 
      children: null 
     }, 
     { 
      node_id: 1, 
      children: [ 
      { 
       node_id: 2, 
       children: null 
      }, 
      { 
       node_id: 3, 
       children: [] 

      } 
      ] 
     }, 
]; 

var map = new Map(); 
map.add_child(10, 0, data); 
console.log(JSON.stringify(data)); 
+0

謝謝你的回覆(+1)。但是,當我add_child時,我不想傳入數據變量,所以我選擇了另一個問題。我可以使用一些這個錯誤檢查雖然:) – JREAM