JS中:
var znodes = [{id:0,pid:-1,name:“ aaaa”},{id:1,pid:0,name:a“ a”},{id:11,pid:1,name:a1“ a1”},{ {id:2,pId:0,name:"B"}, {id:21,pId:2,name:"B1"}, {id:22,pId:2,name:"B2"}, {id:23,pId:2,name:"B3"}, {id:3,pId:0,name:"C"}, {id:31,pId:3,name:"C1"}, {id:32,pID:3,名稱:“ C2”},{id:33,pid:3,name:“ c3”},{id:34,pid:31,name:“ x”},{id:35,pid:35,pid:31,name:“ name:y” y“ y”},ID:36,{:36,PID:31,PID:31,名稱:31,名稱: {id:37,pid:36,名稱:“ z1123”},{id:38,pid:37,name:“ z123123123”}]; function treemenu(a){this.tree = a | a || []; this.groups = {};}; treemenu.prototype = {init:function(pid){this.group();返回this.getdom(this.groups [pid]); },group:function(){for(var i = 0; i <this.tree.length; i ++){if(this.groups [this.tree [i] .pid])){this.groups [this.tree [tree [i] .pid] .pid] .push(this.tree [i]); } else {this.groups [this.tree [i] .pid] = []; this.groups [this.tree [i] .pid] .push(this.tree [i]); }}},getDom:function(a){if(!a){return''} var html ='/n <ul>/n'; for(var i = 0; i <a.length; i ++){html+='<li> <a href =“ href =”#“>”>'+a [i] .name+'</a>'; html+= this.getDom(this.groups [a [i] .id]); html+='</li>/n'; }; html+='</ul>/n';返回html; }}; var html = new treemenu(znodes).init(0); arter(html);爪哇:
軟件包測試;導入java.util.arraylist; import java.util.comparator; import java.util.hashmap; import java.util.iterator; import java.util.util.list; import java.util.map; import java.util.map; import Java.util.set; import Java.util.util.collection java.util.collection; main(string [] args){//讀取層次數據結果集列表列表datalist = virtualdatagenerator.getVirtualResult(); // 節點列表(散列表,用於臨時存儲節點對象)hashmap nodelist = new hashmap(); //根節點節點root = null; // 根據結果集構造節點列表(存入散列表)for(iterator it = datalist.iterator(); it.hasnext();){map dataRecord =(map)it.next();節點節點= new node(); node.id =(string)datarecord.get(“ id”); node.text =(string)datarecord.get(“ text”); node.parentid =(string)dataRecord.get(“ parendId”); nodelist.put(node.id,node); } //構造無序的多叉樹設置entryset = nodelist.entryset(); for(iterator it = entryset.iterator(); it.hasnext();){node node =(node)((map.entry)it.next())。 getValue(); if(node.parentid == null || node.parentid.equals(“”)){root = node; } else {((node)nodelist.get(node.parentid))。 addchild(node); }} //輸出無序的樹形菜單的json字符串system.out.println(root.toString()); //對多叉樹進行橫向排序root.sortchildren(); //輸出有序的樹形菜單的json字符串system.out.println(root.toString()); ((// 程序輸出結果如下(無序的樹形菜單)(格式化後的結果) '廊坊銀行金光道支行',// laf:true //},// {// {// id:'112000',//文本:'廊坊銀行解放道支行',//子女:[// {// {// {// {// {// 112200',// text:'廊坊銀行三大街支行',// laf',// laf:true //} //} //] //} //(():// {// {// ID:'100000',// text:'廊坊銀行總行',//兒童:[// {// {// ID:'110000',// text:'110000',// text:'廊坊分行',// // id:'112000',//文本:'廊坊銀行解放道支行',//兒童:[// {// {// ID:'112100',//文字:'廊坊銀行廣陽道支行',// laf:true //},// {// {// {// {// {112200 '廊坊銀行開發區支行',// leaf:true //} //] //} //] //}}}}}}/***節點類*/class node {/***節點編號*節點編號*/public String ID; / ** *節點內容 */公共字符串文本; / ** *父節點編號 */ public String parentId; / ** *孩子節點列表 */私人兒童=新孩子(); //先序遍歷,拼接json字符串public String toString(){字符串結果=“ {” +“ ID:'” + ID +“' +” +“,text:'” + text +“''''''; if(children!= null && childris.getSize()!= 0){結果 + =“,兒童:” + childris.tostring(); } else {結果 +=“,葉:true”; }返回結果 +“}”; } //兄弟節點橫向排序public void sortchildren(){if(childris! = null && childris.getSize()! = 0){children.sortchildren(); }} //添加孩子節點public void addChild(node node){this.children.addchild(node); }}/***孩子列表類*/類兒童{私人列表list = new arraylist(); public int getsize(){return list.size(); } public void addChild(node node){list.add(node); } //拼接孩子節點的json字符串public string tostring(){字符串結果=“ [”; for(iterator it = list.iterator(); it.hasnext();){結果 +=((node)it.next())。 toString();結果 +=“,”; }結果= result.substring(0,result.length() - 1);結果 +=“]”;返回結果; } //孩子節點排序public void sortChildren(){//對本層節點進行排序//可根據不同的排序屬性,傳入不同的比較器//對每個節點的下一層節點進行排序for(iterator it = list.iterator(); it.hasnext();){((node)it.next())。 sortChildren(); }}}/** *節點比較器 */類NodeIdComparator實現了比較器{//按照節點編號比較public int comparare(對象O1,Object O2){int j1 = integer.parseint.parseint((((node)o1).id).id); int j2 = integer.parseint((((node)o2).id);返回(j1 <j2?-1 :( j1 == j2?0:1)); }}/** *構造虛擬的層次數據 */class virtualDatagenerator {//構造無序的結果集列表,實際應用中,該數據應該從數據庫中查詢獲得;,該數據應該從數據庫中查詢獲得;公共靜態列表getVirtualResult(){list datalist = new arraylist(); hashmap dataRecord1 = new hashmap(); datarecord1.put(“ id”,“ 112000”); dataRecord1.put(“ text”,“”,“廊坊銀行解放道支行”); dataRecord1.put(“ parentId”,“ 110000”); hashmap dataRecord2 = new hashmap(); datarecord2.put(“ id”,“ 112200”); datarecord2.put(“ text”,“”,“廊坊銀行三大街支行”); dataRecord2.put(“ parendId”,“ 112000”); hashmap dataRecord3 = new hashmap(); datarecord3.put(“ id”,“ 112100”); dataRecord3.put(“ text”,“”,“廊坊銀行廣陽道支行”); dataRecord3.put(“ parentId”,“ 112000”); hashmap dataRecord4 = new hashmap(); datarecord4.put(“ id”,“ 113000”); datarecord4.put(“ text”,“”,“廊坊銀行開發區支行”); dataRecord4.put(“ parendId”,“ 110000”); hashmap dataRecord5 = new hashmap(); datarecord5.put(“ id”,“ 100000”); datarecord5.put(“ text”,“”,“廊坊銀行總行”); dataRecord5.put(“ parendId”,“”); hashmap dataRecord6 = new hashmap(); datarecord6.put(“ id”,“ 110000”); dataRecord6.put(“ text”,“”,“廊坊分行”); dataRecord6.put(“ parendId”,“ 100000”); hashmap dataRecord7 = new hashmap(); datarecord7.put(“ id”,“ 111000”); dataRecord7.put(“ text”,“”,“廊坊銀行金光道支行”); dataRecord7.put(“ parendId”,“ 110000”); datalist.add(dataRecord1); datalist.add(dataRecord2); datalist.add(dataRecord3); datalist.add(dataRecord4); datalist.add(dataRecord5); datalist.add(dataRecord6); datalist.add(dataRecord7);返回數據師; }}}以上這篇java js中實現無限層級的樹形結構方法(類似遞歸)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。 ,也希望大家多多支持武林網。