Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中构造层次结构树_Javascript_Arrays_Node.js_Trie - Fatal编程技术网

在javascript中构造层次结构树

在javascript中构造层次结构树,javascript,arrays,node.js,trie,Javascript,Arrays,Node.js,Trie,我正试图从SOLR中获得的方面为类别和子类别等构建层次结构树。输入的格式如下: ['445', 79, '398', 73, '710', 32, '398|760', 28, '398|760|779', 28, '445|446', 10] 其中单引号中的数据表示类别,后面的数字表示频率 鉴于上述数组,我需要的输出格式应为: [ { "id": 445, "count": 79, "children": [ {

我正试图从
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.children属性外,这应该满足您的需要,这样您就可以通过使用
node.children.length查看树中任何节点有多少子节点了

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);

我对这里的目的相当困惑。通常,您会在JavaScript中创建一个trie,用于自动完成目的,例如如果用户键入了类别名称,那么显然您正在处理类别ID。。。你的目标是什么样的trie结构?给定
398 | 730 | 607
我可以为你设计一棵树,比如
{398:{freq:73760:{freq:28779:{freq:28}},730:{freq:18607:{freq:18}}
这就是你想要的吗?或者你想要一个基于类名的树,比如:
{'j':{'a':{'v':{'a':{…}}
?我已经编辑了这个问题来展示我正在努力实现的最终输出,我想使用Trie作为字典,然后使用Trie来构建最终的数据结构(基数树不仅仅用于自动完成)。但是,只要复杂性保持在O(n),任何其他方法都可以工作。为什么不先分割数据以获得树的顶点,然后再获得边呢?还是总是这样:先是边缘,然后是垂直?原始输入的来源超出了我的控制范围,所以我不能假设,但我当然可以修改它,以获得您提到的格式-但我不明白这将如何帮助缓解复杂性。我已经通过在桶中递归地形成桶来解决了这个问题——但正如可以预料的那样,在时间和空间方面都存在着可怕的权衡!非常感谢!看起来很完美-空的儿童财产根本不是问题!非常感谢你的帮助!我想没有办法把它归结为O(n)!如果您想添加big-O复杂性支持,那么应该使用节点树,这与trie非常不同。我不认为像JavaScript这样的高级语言(由众多浏览器之一解释)或node.js的JavaScript引擎能够对这种性能计算有多大意义。但是我可以理解,由于强大的类型,像Java这样的低级语言将如何从中受益匪浅。
[{"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":[]}]}]