2014-10-27 45 views
0

我有一個分層結構數據,可以視爲樹結構。javascript將分層樹分解爲所有子樹,並按其級別分組每個子樹的節點

首先

我需要這個層次樹拆分成子樹,並得到所有的子樹。 下面的函數是怎麼做,它的工作原理以及

  var hierarchObjects = []; 
      traverseNodes(root); 
      function traverseNodes(root){ 
       hierarchObjects.push(root); 
       for(var i=0; i<root.children.length; ++i) 
       { 
        traverseNodes(root.children[i]); 
       } 
      } 

我要組一個子樹的每個層次的節點,在陣列hierarchObjects。並且子樹的深度是不同的。

例如,

在陣列Level1級別1的子樹的節點放。

將第2級的子樹的節點放在數組Level2中。

那麼我應該怎麼做Second過程?

是否有更有效的方法適用於所有過程?

由於我的數據集有點大,而且有大約1300個子樹,我需要找到一個有效的方法嗎?

我的數據集是一個樹形結構:http://www.csee.umbc.edu/~yongnan/untitled/pathwayHierarchy.json

你可以看到它是一個parent ----- children結構樹。

對於這棵樹,我使用步驟1拆分成子樹。 對於每個子樹,例如如下:

{ 
     "dbId": "111461", 
     "name": "Cytochrome c-mediated apoptotic response", 
     "children": [ 
      { 
       "dbId": "111458", 
       "name": "Formation of apoptosome", 
       "children": [], 
       "size": 1 
      }, 
      { 
       "dbId": "111459", 
       "name": "Activation of caspases through apoptosome-mediated cleavage", 
       "children": [], 
       "size": 1 
      } 
     ] 
    } 

該子樹,它只是有兩個孩子爲1級,因此,返回陣列應採用凋亡體的[[形成通過凋亡體介導的裂解的胱天蛋白酶的激活]

{ 
     "dbId": "111471", 
     "name": "Apoptotic factor-mediated response", 
     "children": [ 
      { 
       "dbId": "111461", 
       "name": "Cytochrome c-mediated apoptotic response", 
       "children": [ 
        { 
         "dbId": "111458", 
         "name": "Formation of apoptosome", 
         "children": [], 
         "size": 1 
        }, 
        { 
         "dbId": "111459", 
         "name": "Activation of caspases through apoptosome-mediated cleavage", 
         "children": [], 
         "size": 1 
        } 
       ] 
      }, 
      { 
       "dbId": "111469", 
       "name": "SMAC-mediated apoptotic response", 
       "children": [ 
        { 
         "dbId": "111463", 
         "name": "SMAC binds to IAPs ", 
         "children": [], 
         "size": 1 
        }, 
        { 
         "dbId": "111464", 
         "name": "SMAC-mediated dissociation of IAPcaspase complexes ", 
         "children": [], 
         "size": 1 
        } 
       ] 
      } 
     ] 
    } 

對於此數據集,其結果可能是

[ [細胞色素C介導的細胞凋亡反應,SMAC介導的細胞凋亡應答], [通過凋亡體介導的裂解的凋亡體的形成,活化胱天蛋白酶的,SMAC結合與IAP ,IAPcaspase的SMAC介導的解離複合物] ]

現在,我嘗試使用廣度優先算法做一步。我知道效率不是很好。

謝謝!

+0

你能不能讓根對象的實例和期望的輸出? – juvian 2014-10-27 20:27:37

+0

@juvian補充說,謝謝 – yongnan 2014-10-27 20:47:14

+0

這樣的事情? :http://jsfiddle.net/fmhrpdbf/ – juvian 2014-10-27 23:46:28

回答

0

這應該做的伎倆,除非你正在處理100萬左右的節點或非常深的樹木,應該是相當快:

var data={ 
    //your data 
} 


var arr=[]; // array that holds an array of names for each sublevel 

function traverse(data, level){ 
    if(arr[level]==undefined) arr[level]=[]; // if its the first time reaching this sub-level, create array 
    arr[level].push(data.name); // push the name in the sub-level array 
    for(var index=0;index<data.children.length;index++){ // for each node in children 
     traverse(data.children[index], level+1); // travel the node, increasing the current sub-level 
    } 
} 

traverse(data, 0); // start recursive function 
console.log(arr) 

完全小提琴:http://jsfiddle.net/juvian/fmhrpdbf/1/

相關問題