JS 中 :
var znodes = [{id: 0, pid: -1, nom: "aaaa"}, {id: 1, pid: 0, nom: "a"}, {id: 11, pid: 1, nom: "a1"}, {id: 12, pid: 1, nom: "a2"}, {id: 13, pid: 1, nom: "a3"}, {id: 2, pid: 0, nom: "b"}, {id: 21, pid: 2, nom: "b1"}, {id: 22, pid: 2, nom: "b2"}, {id: 23, pid: 2, nom: "b3"}, {id: 3, pid: 0, nom: "c"}, {id: 31, pid: 3, "C1", "}, {{id: 31, Pid: 3," C1 "," C "}, {} {id: 32, pid: 3, nom: "c2"}, {id: 33, pid: 3, nom: "c3"}, {id: 34, pid: 31, nom: "x"}, {id: 35, pid: 31, nom: "y"}, {id: 36, pid: 31, nom: "z"}, {id: 37, pid: 36, nom: "z1123"}, {id: 38, pid: 37, nom: "Z123123123"}]; fonction Treemenu (a) {this.tree = a || []; this.groups = {};}; TreeMenu.prototype = {init: function (pid) {this.group (); Renvoie this.getdom (this.groups [pid]); }, groupe: 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'; retour html; }}; var html = new Treemenu (zNodes) .Init (0); alert (html);Java:
Test de package; import java.util.arraylist; import java.util.comparator; import java.util.hashmap; import java.util.iterator; import java.util.list; import java.util.map; import java.util.set; import java.util.collections; / ** * 多叉树类 * args) {// 读取层次数据结果集列表 list dataList = virtualDatagenerator.getVirtualResult (); // 节点列表 (散列表 , 用于临时存储节点对象) Hashmap NodeList = new HashMap (); // 根节点 Node root = null; // 根据结果集构造节点列表 (存入散列表 存入散列表 pour (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 ("texte"); node.parentid = (string) datarecord.get ("parentid"); NodeList.put (Node.id, Node); } // 构造无序的多叉树 set 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 ()); // 程序输出结果如下 (无序的树形菜单) ((格式化后的结果) : // {// id: '100000', // text: '廊坊银行总行', // enfants: [// {// id: '110000', // text: '廊坊分行', // enfants: [// {// id: '113000', // text: '廊坊银行开发区支行', //af: '111000', // text: '廊坊银行金光道支行', //af: true //}, // {// id: '112000', // text: '廊坊银行解放道支行', // enfants: [// {// id: '112200', // text: '廊坊银行三大街支行', //af: true //}, // {// id: '112100', // text: '}, // {// id:' 112100 ' } //] //} //] //} //] //} // 程序输出结果如下 (有序的树形菜单) ((格式化后的结果) : // {// id: '100000', // text: '廊坊银行总行', // enfants: [// {// id: '110000', // text: '廊坊分行', // enfants: [// '廊坊银行金光道支行', //af: true //}, // {// id: '112000', // text: '廊坊银行解放道支行', // enfants: [// {// id: '112100', // text: '廊坊银行广阳道支行', //af: true //}, // {// id: '112200', // text: '廊坊银行三大街支行', //af: true //} //. // {// id: '113000', // text: '廊坊银行开发区支行', //af: true //} //] //} //] //}}} / *** 节点类 * / classe nœud {/ ** * 节点编号 * / public String id; / ** * 节点内容 * / Texte de chaîne publique; / ** * 父节点编号 * / public String Parentid; / ** * 孩子节点列表 * / Private Enfants Enfants = New Children (); // 先序遍历 , 拼接 JSON 字符串 public String toString () {String result = "{" + "id: '" + id + "'" + ", text: '" + text + "'"; if (enfants! = null && enfants.getSize ()! = 0) {résultat + = ", enfants:" + enfants.toString (); } else {result + = ", leaf: true"; } Retour Résultat + "}"; } // 兄弟节点横向排序 public void sortchildren () {if (enfants! = Null && enfants.getSize ()! = 0) {enfants.sortchildren (); }} // 添加孩子节点 public void addchild (nœud nœud) {this.children.addchild (nœud); }} / *** 孩子列表类 * / class Children {private list list = new ArrayList (); public int getSize () {return list.size (); } public void addChild (nœud nœud) {list.add (nœud); } // 拼接孩子节点的 JSON 字符串 public String toString () {String result = "["; for (iterator it = list.iterator (); it.hasnext ();) {result + = ((node) it.next ()). toString (); Résultat + = ","; } result = result.substring (0, result.length () - 1); Résultat + = "]"; Résultat de retour; } // 孩子节点排序 public void sortChildren () {// 对本层节点进行排序 // 可根据不同的排序属性 , 传入不同的比较器 , 这里传入 id 比较器 Collection.Sort (list, new nodeIdComparator ()); // 对每个节点的下一层节点进行排序 pour (iterator it = list.iterator (); it.hasnext ();) {((node) it.next ()). SortChildren (); }}} / ** * 节点比较器 * / class nodeIdComparator implémente le comparateur {// 按照节点编号比较 public int compare (objet o1, objet o2) {int j1 = Integer.parseInt (((node) o1) .id); int J2 = Integer.ParseInt (((nœud) 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 ("texte", "廊坊银行解放道支行"); datarecord1.put ("Parentid", "110000"); Hashmap dataReCord2 = new hashmap (); dataRecord2.put ("id", "112200"); dataRecord2.put ("texte", "廊坊银行三大街支行"); dataRecord2.put ("Parentid", "112000"); Hashmap dataReCord3 = new Hashmap (); dataRecord3.put ("id", "112100"); dataRecord3.put ("texte", "廊坊银行广阳道支行"); dataRecord3.put ("parentid", "112000"); Hashmap datarecord4 = new hashmap (); dataRecord4.put ("id", "113000"); dataRecord4.put ("texte", "廊坊银行开发区支行"); dataRecord4.put ("parentid", "110000"); Hashmap dataReCord5 = new Hashmap (); dataRecord5.put ("id", "100000"); dataRecord5.put ("texte", "廊坊银行总行"); dataRecord5.put ("parentid", ""); Hashmap dataReCord6 = new hashmap (); datarecord6.put ("id", "110000"); datarecord6.put ("texte", "廊坊分行"); dataReCord6.put ("Parentid", "100000"); Hashmap dataReCord7 = new Hashmap (); datarecord7.put ("id", "111000"); datarecord7.put ("texte", "廊坊银行金光道支行"); 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); RETOUR DATALIST; }}以上这篇 java 、 js 中实现无限层级的树形结构方法 (类似递归) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。