java、js中实现无限层级的树形结构方法(类似递归)
js中:
varzNodes=[ {id:0,pId:-1,name:"Aaaa"}, {id:1,pId:0,name:"A"}, {id:11,pId:1,name:"A1"}, {id:12,pId:1,name:"A2"}, {id:13,pId:1,name:"A3"}, {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,name:"C2"}, {id:33,pId:3,name:"C3"}, {id:34,pId:31,name:"x"}, {id:35,pId:31,name:"y"}, {id:36,pId:31,name:"z"}, {id:37,pId:36,name:"z1123"}, {id:38,pId:37,name:"z123123123"} ]; functiontreeMenu(a){ this.tree=a||[]; this.groups={}; }; treeMenu.prototype={ init:function(pid){ this.group(); returnthis.getDom(this.groups[pid]); }, group:function(){ for(vari=0;i<this.tree.length;i++){ if(this.groups[this.tree[i].pId]){ this.groups[this.tree[i].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''} varhtml='\n<ul>\n'; for(vari=0;i<a.length;i++){ html+='<li><ahref="#">'+a[i].name+'</a>'; html+=this.getDom(this.groups[a[i].id]); html+='</li>\n'; }; html+='</ul>\n'; returnhtml; } }; varhtml=newtreeMenu(zNodes).init(0); alert(html);
java:
packagetest; importjava.util.ArrayList; importjava.util.Comparator; importjava.util.HashMap; importjava.util.Iterator; importjava.util.List; importjava.util.Map; importjava.util.Set; importjava.util.Collections; /** *多叉树类 */ publicclassMultipleTree{ publicstaticvoidmain(String[]args){ //读取层次数据结果集列表 ListdataList=VirtualDataGenerator.getVirtualResult(); //节点列表(散列表,用于临时存储节点对象) HashMapnodeList=newHashMap(); //根节点 Noderoot=null; //根据结果集构造节点列表(存入散列表) for(Iteratorit=dataList.iterator();it.hasNext();){ MapdataRecord=(Map)it.next(); Nodenode=newNode(); node.id=(String)dataRecord.get("id"); node.text=(String)dataRecord.get("text"); node.parentId=(String)dataRecord.get("parentId"); nodeList.put(node.id,node); } //构造无序的多叉树 SetentrySet=nodeList.entrySet(); for(Iteratorit=entrySet.iterator();it.hasNext();){ Nodenode=(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()); //程序输出结果如下(无序的树形菜单)(格式化后的结果): //{ //id:'100000', //text:'廊坊银行总行', //children:[ //{ //id:'110000', //text:'廊坊分行', //children:[ //{ //id:'113000', //text:'廊坊银行开发区支行', //leaf:true //}, //{ //id:'111000', //text:'廊坊银行金光道支行', //leaf:true //}, //{ //id:'112000', //text:'廊坊银行解放道支行', //children:[ //{ //id:'112200', //text:'廊坊银行三大街支行', //leaf:true //}, //{ //id:'112100', //text:'廊坊银行广阳道支行', //leaf:true //} //] //} //] //} //] //} //程序输出结果如下(有序的树形菜单)(格式化后的结果): //{ //id:'100000', //text:'廊坊银行总行', //children:[ //{ //id:'110000', //text:'廊坊分行', //children:[ //{ //id:'111000', //text:'廊坊银行金光道支行', //leaf:true //}, //{ //id:'112000', //text:'廊坊银行解放道支行', //children:[ //{ //id:'112100', //text:'廊坊银行广阳道支行', //leaf:true //}, //{ //id:'112200', //text:'廊坊银行三大街支行', //leaf:true //} //] //}, //{ //id:'113000', //text:'廊坊银行开发区支行', //leaf:true //} //] //} //] //} } } /** *节点类 */ classNode{ /** *节点编号 */ publicStringid; /** *节点内容 */ publicStringtext; /** *父节点编号 */ publicStringparentId; /** *孩子节点列表 */ privateChildrenchildren=newChildren(); //先序遍历,拼接JSON字符串 publicStringtoString(){ Stringresult="{" +"id:'"+id+"'" +",text:'"+text+"'"; if(children!=null&&children.getSize()!=0){ result+=",children:"+children.toString(); }else{ result+=",leaf:true"; } returnresult+"}"; } //兄弟节点横向排序 publicvoidsortChildren(){ if(children!=null&&children.getSize()!=0){ children.sortChildren(); } } //添加孩子节点 publicvoidaddChild(Nodenode){ this.children.addChild(node); } } /** *孩子列表类 */ classChildren{ privateListlist=newArrayList(); publicintgetSize(){ returnlist.size(); } publicvoidaddChild(Nodenode){ list.add(node); } //拼接孩子节点的JSON字符串 publicStringtoString(){ Stringresult="["; for(Iteratorit=list.iterator();it.hasNext();){ result+=((Node)it.next()).toString(); result+=","; } result=result.substring(0,result.length()-1); result+="]"; returnresult; } //孩子节点排序 publicvoidsortChildren(){ //对本层节点进行排序 //可根据不同的排序属性,传入不同的比较器,这里传入ID比较器 Collections.sort(list,newNodeIDComparator()); //对每个节点的下一层节点进行排序 for(Iteratorit=list.iterator();it.hasNext();){ ((Node)it.next()).sortChildren(); } } } /** *节点比较器 */ classNodeIDComparatorimplementsComparator{ //按照节点编号比较 publicintcompare(Objecto1,Objecto2){ intj1=Integer.parseInt(((Node)o1).id); intj2=Integer.parseInt(((Node)o2).id); return(j1<j2?-1:(j1==j2?0:1)); } } /** *构造虚拟的层次数据 */ classVirtualDataGenerator{ //构造无序的结果集列表,实际应用中,该数据应该从数据库中查询获得; publicstaticListgetVirtualResult(){ ListdataList=newArrayList(); HashMapdataRecord1=newHashMap(); dataRecord1.put("id","112000"); dataRecord1.put("text","廊坊银行解放道支行"); dataRecord1.put("parentId","110000"); HashMapdataRecord2=newHashMap(); dataRecord2.put("id","112200"); dataRecord2.put("text","廊坊银行三大街支行"); dataRecord2.put("parentId","112000"); HashMapdataRecord3=newHashMap(); dataRecord3.put("id","112100"); dataRecord3.put("text","廊坊银行广阳道支行"); dataRecord3.put("parentId","112000"); HashMapdataRecord4=newHashMap(); dataRecord4.put("id","113000"); dataRecord4.put("text","廊坊银行开发区支行"); dataRecord4.put("parentId","110000"); HashMapdataRecord5=newHashMap(); dataRecord5.put("id","100000"); dataRecord5.put("text","廊坊银行总行"); dataRecord5.put("parentId",""); HashMapdataRecord6=newHashMap(); dataRecord6.put("id","110000"); dataRecord6.put("text","廊坊分行"); dataRecord6.put("parentId","100000"); HashMapdataRecord7=newHashMap(); dataRecord7.put("id","111000"); dataRecord7.put("text","廊坊银行金光道支行"); dataRecord7.put("parentId","110000"); dataList.add(dataRecord1); dataList.add(dataRecord2); dataList.add(dataRecord3); dataList.add(dataRecord4); dataList.add(dataRecord5); dataList.add(dataRecord6); dataList.add(dataRecord7); returndataList; } }
以上这篇java、js中实现无限层级的树形结构方法(类似递归)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。