作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试从我从 SOLR
获得的方面为类别和子类别等构建层次结构树。输入的形式是:
['445',
79,
'398',
73,
'710',
32,
'398|760',
28,
'398|760|779',
28,
'445|446',
10]
其中单引号中的数据代表类别,后面的数字代表频率。
给定上面的数组,我需要的输出格式应该是:
[
{
"id": 445,
"count": 79,
"children": [
{
"id": 446,
"count": 10
}
]
},
{
"id": 398,
"count": 73,
"children": [
{
"id": 760,
"count": 28,
"children": [
{
"id": 779,
"count": 28
}
]
}
]
},
{
"id": 710,
"count": 32
}
]
我正在尝试用相同的方法构建树 - 以获得有效的解决方案,但无法做到这一点。有人会知道如何让它工作 - 或任何其他时间有效的解决方案吗?
谢谢!
最佳答案
除了没有子 Node 的 Node 仍将具有 node.children 属性外,这应该可以执行您想要的操作,因此您可以使用 node.children.length
查看树中任何 Node 有多少个子 Node 。
var o=["445", 79, "398", 73, "710", 32, "398|760", 28, "398|760|779", 28, "445|446", 26, "710|1045", 25, "445|452", 24, "381", 19, "445|943", 19, "398|730", 18, "398|730|607", 18, "367", 16, "445|446|451", 15, "351", 14, "351|363", 14, "351|363|365", 14, "381|395", 14, "381|395|566", 14, "445|526", 14, "445|526|769", 14, "367|372", 12, "710|1045|1119", 11, "398|410", 10, "398|483", 9, "445|452|743", 8, "367|372|377", 7, "398|483|757", 7, "445|446|792", 7, "445|452|744", 7, "445|452|719", 6, "398|410|411", 5];
var nodeMap={};
var nodeLevels=[];
for(var i=0;i<o.length;i+=2)
{
var catLineage=o[i].split('|');
var cat=catLineage[catLineage.length-1];
var depth=catLineage.length;
while(depth>nodeLevels.length){nodeLevels.push([]);}
nodeMap[cat]={id:cat,count:o[i+1],depth:depth,parents:catLineage.slice(0,catLineage.length-1)};
nodeLevels[depth-1].push(cat);
}
var tree=[];
var treeNodeLookup={};
for(var i=0;i<nodeLevels.length;i++)
{
for(var j=0;j<nodeLevels[i].length;j++)
{
var nodeId=nodeLevels[i][j];
var nodeDepth=nodeMap[nodeId].depth;
var nodeCount=nodeMap[nodeId].count;
var parents=nodeMap[nodeId].parents;
var pointer={children:tree};
if(parents.length>0){pointer=treeNodeLookup[parents[0]];}
var node={id:nodeId,count:nodeCount,children:[]};
pointer.children.push(node);
treeNodeLookup[nodeId]=pointer.children[pointer.children.length-1];
}
}
console.log(tree);
使用 console.log(JSON.stringify(tree));
我的输出是:
[{"id":"445","count":79,"children":[{"id":"446","count":26,"children":[]},{"id":"452","count":24,"children":[]},{"id":"943","count":19,"children":[]},{"id":"526","count":14,"children":[]},{"id":"451","count":15,"children":[]},{"id":"769","count":14,"children":[]},{"id":"743","count":8,"children":[]},{"id":"792","count":7,"children":[]},{"id":"744","count":7,"children":[]},{"id":"719","count":6,"children":[]}]},{"id":"398","count":73,"children":[{"id":"760","count":28,"children":[]},{"id":"730","count":18,"children":[]},{"id":"410","count":10,"children":[]},{"id":"483","count":9,"children":[]},{"id":"779","count":28,"children":[]},{"id":"607","count":18,"children":[]},{"id":"757","count":7,"children":[]},{"id":"411","count":5,"children":[]}]},{"id":"710","count":32,"children":[{"id":"1045","count":25,"children":[]},{"id":"1119","count":11,"children":[]}]},{"id":"381","count":19,"children":[{"id":"395","count":14,"children":[]},{"id":"566","count":14,"children":[]}]},{"id":"367","count":16,"children":[{"id":"372","count":12,"children":[]},{"id":"377","count":7,"children":[]}]},{"id":"351","count":14,"children":[{"id":"363","count":14,"children":[]},{"id":"365","count":14,"children":[]}]}]
关于javascript - 在javascript中构建层次结构树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32475935/
我正在尝试将多个水平链接的 Button 和 TextView 垂直链接为 View 集,但仍保持平面 View 层次结构。这是我的初始布局和代码:
到目前为止,我已经在Google BigQuery上训练了几种模型,目前我需要查看模型的外观(即架构,损失函数等)。 有没有办法获取这些信息? 最佳答案 仔细阅读文档后,我可以说该功能尚不存在。我什至
本文实例讲述了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)。分享给大家供大家参考,具体如下: 前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个
我是一名优秀的程序员,十分优秀!