2015-09-09 248 views
3

我正在嘗試從SOLR獲得的方面爲類別和子類別等構建分層樹。輸入是以下形式:在javascript中構建層次結構樹

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

我想建立樹明各項─對一個有效的解決方案,但不能這樣做。會有人知道如何讓它工作 - 或任何其他有效的解決方案。

謝謝!

+0

我對這裏預期的用途感到困惑。通常情況下,你會在JavaScript中創建一個用於自動完成目的的trie,例如,如果用戶鍵入了類別名稱,顯然你正在處理類別ID ...你想要的是什麼樣的trie結構?鑑於'398 | 730 | 607'我可以爲你設計一個樹,如{{398:{freq:73,760:{freq:28,779:{freq:28}},730:{freq:18,607:{freq:18} }}}這就是你想要的嗎?或者你是否想要一個基於類別名稱的樹:'{'j':{'a':{'v':{'a':{...}}}}}'? – Ultimater

+0

我編輯了這個問題以展示我正在努力的最終輸出,我想到了使用Trie作爲字典,然後使用trie來構建最終數據結構(並且基數樹不僅用於自動完成)。但是,只要複雜性仍然是O(n),任何其他方法都可以工作。 –

+0

爲什麼不分割數據首先得到樹的頂點,然後得到邊緣?還是總是這樣:你得到第一個邊緣,然後verticies? – Hristo

回答

1

這應該做你想要有與沒有孩子的節點將仍然有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); 

使用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":[]}]}] 
+0

謝謝!看起來很完美 - 空的兒童財產根本不是問題!真的很感謝幫助!我想沒有辦法將它歸結爲O(n)! –

+0

如果您想添加大O複雜度支持,則應該使用節點樹,該節點樹與trie非常不同。我不認爲像JavaScript這樣的高級語言可以通過許多瀏覽器中的一個瀏覽器解釋,或者JavaScript引擎用於node.js的情況,這樣就能夠很好地理解這種性能計算。但是我可以理解,由於強大的輸入,像Java這樣的低級語言如何從它們中受益匪淺。 – Ultimater

+0

二叉樹*(錯字) – Ultimater