JS中:
var znodes = [{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: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、 " {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: "z"}、 "Z" {id:37、pid:36、name: "z1123"}、{id:38、pid:37、name: "z123123123"}]; function treemenu(a){this.tree = 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 [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 ''} var html = '/n <ul>/n'; for(var i = 0; i <a.length; i ++){html+= '<li> <a 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); alert(html);Java:
パッケージテスト; java.util.arraylistのインポート;インポートjava.util.comparator; import java.util.hashmap; import java.util.iterator; import java.util.list; import java.util.util.set; import java.util.util.util.comettions; main(string [] args){//读取层次数据结果集列表リストdatalist = virtualdatagenerator.getVirtualResult(); // 节点列表(散列表、用于临时存储节点对象)hashmap nodeList = new Hashmap(); //根节点ノードroot = null; // for(iterator it = datalist.iterator(); it.hasnext();){map datarecord =(map)it.next(); node node = new node(); node.id =(string)datarecord.get( "id"); node.text =(string)datarecord.get( "text"); node.parentId =(string)datarecord.get( "daterid"); nodeList.put(node.id、node); } // 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); }} // //对多叉树进行横向排序root.sortchildren(); //输出有序的树形菜单的JSON // 程序输出结果如下(无序的树形菜单)(格式化后的结果):// {// id: '100000'、// text: '廊坊银行总行'、// children:[// {// id:'110000 '、// text:'廊坊分行 '、// children:[// {// id:' 113000 '、// text:'廊坊银行开发区支行 '、// leaf:true // // leaf:true //}、// {// id:'112000 '、// text:'廊坊银行解放道支行 '、// children:[// id:' 112200 '、// text:'廊坊银行三大街支行 '、// leaf:true //}、// {// id:'112100'、// tex //} //] //} // '112000'、//テキスト: '廊坊银行解放道支行'、// children:[// {// id: '112100'、// text: '廊坊银行广阳道支行'、// leaf:true // {// id: '112200'、//テキスト: '廊坊银行三大街支行' '、// leaf:true //} //] // '廊坊银行开发区支行'、// leaf:true //} //] //} //] //}}/***节点类*/class node {/***节点编号*/public string id; / ** *节点内容 */ public String Text; / ** *父节点编号 */ public string parentid; / ** *孩子节点列表 */ private children children = new Children(); //先序遍历、拼接JSON if(children!= null && children.getSize()!= 0){result + = "、children:" + children.toString(); } else {result += "、leaf:true"; } return result + "}"; } // }} // }}/***孩子列表类*/class children {private list list = new ArrayList(); public int getsize(){return list.size(); } public void addChild(node node){list.add(node); } // for(iterator it = list.iterator(); it.hasnext();){result +=((node)it.next())。toString(); result += "、"; } result = result.substring(0、result.length() - 1); result += "]";返品結果; } // // for(iterator it = list.iterator(); it.hasnext();){((node)it.next())。sortchildren(); }}}/** */ */class nodeIdcomparatorは比較{//按照节点编号比较public int compare(object o1、object o2){int j1 = integer.parseint(((node))o1).id); int j2 = integer.parseint((((node)o2).id); return(j1 <j2?-1:(j1 == j2?0:1)); }}/** */ */class virtualdatagenerator {//构造无序的结果集列表、实际应用中、该数据应该从数据库中查询获得; public static list 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( "parentid"、 "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( "parentid"、 "110000"); Hashmap datarecord5 = new Hashmap(); datarecord5.put( "id"、 "100000"); datarecord5.put( "text"、 "廊坊银行总行"); datarecord5.put( "parentid"、 ""); Hashmap datarecord6 = new Hashmap(); datarecord6.put( "id"、 "110000"); datarecord6.put( "text"、 "廊坊分行"); datarecord6.put( "parentid"、 "100000"); Hashmap datarecord7 = new Hashmap(); 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);データリストを返す; }}以上这篇java、js(类似递归)就是小编分享给大家的全部内容了、希望能给大家一个参考、也希望大家多多支持武林网。