2014-10-27 124 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/