【问题标题】:javascript Split hierarchical tree into all the sub-trees and group the nodes of each sub-tree by its leveljavascript 将分层树拆分为所有子树,并按其级别对每个子树的节点进行分组
【发布时间】:2014-12-23 03:03:54
【问题描述】:

我有一个层次结构的数据,可以看成树形结构。

第一

我需要把这个层次树拆分成子树,得到所有的子树。 下面这个函数是我做的,效果很好

            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的子树的节点放入数组Level1中。

将level2的子树的节点放入数组Level2

那么Second进程该怎么办呢?

是否有更有效的方法来处理所有过程?

因为我的数据集有点大,大约有1300个子树,我需要找到一个有效的方法吗?

我的数据集是一个树形结构:http://www.csee.umbc.edu/~yongnan/untitled/pathwayHierarchy.json

你可以看到它是一个parent-----children结构树。

对于这棵树,我使用步骤 1 拆分为子树。 对于每个子树,示例如下:

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
            }
        ]
    }

对于这个子树,level1只有两个孩子,所以返回数组应该是[[Formation of apoptosome,Activation of caspase through apoptosome-mediated cleavage]]

2

 {
        "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与IAPs结合,SMAC介导的IAPcaspase复合物解离] ]

现在,我正在尝试使用广度优先算法来执行 Second 步骤。我知道效率不是很好。

谢谢!

【问题讨论】:

  • 你能举一个根对象和所需输出的例子吗?
  • @juvian 补充一下,谢谢
  • 这样的? :jsfiddle.net/fmhrpdbf
  • 谢谢,虽然速度还是很慢
  • BFS 只遍历每个节点一次,它的主要问题是需要内存,所以没有理由有一个更快的。此外,至少在我的浏览器中,从链接中的数据生成输出需要 1 毫秒:jsfiddle.net/juvian/fmhrpdbf/1 检查控制台需要多长时间

标签: javascript algorithm tree


【解决方案1】:

这应该可以解决问题,除非您处理大约 1m 节点或非常深的树,否则应该非常快:

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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2021-09-19
    相关资源
    最近更新 更多