JS : :
var zNodes = [{id: 0, pid: -1, nombre: "aaaa"}, {id: 1, pid: 0, nombre: "a"}, {id: 11, pid: 1, nombre: "a1"}, {id: 12, pid: 1, nombre: "a2"}, {id: 13, pid: 1, name: " {id: 2, pid: 0, nombre: "b"}, {id: 21, pid: 2, nombre: "b1"}, {id: 22, pid: 2, nombre: "b2"}, {id: 23, pid: 2, name: "b3"}, {id: 3, pid: 0, nombre: "c"}, {31, 3, 3, name, name: name: name: c1 ", nombre: name: name: name: name: name: n name, name, nombre: nombre: name: name: name: name: n name: c1" {id: 32, pid: 3, nombre: "c2"}, {id: 33, pid: 3, nombre: "c3"}, {id: 34, pid: 31, nombre: "x"}, {id: 35, pid: 31, nombre: "y"}, {id: 36, pid: 31, nombre: "z"},, {id: 37, pid: 36, nombre: "z1123"}, {id: 38, pid: 37, nombre: "Z123123123"}]; function treeMenu (a) {this.tree = a || []; this.groups = {};}; treemenu.prototype = {init: function (pid) {this.group (); devolver esto.getdom (this.groups [PID]); }, grupo: 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'; devolver html; }}; var html = new TreeMenu (Znodes) .init (0); alerta (html);Java:
prueba de paquete; 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;/*** 多叉树类*/pública Class múltiple {Class de múltiple { Main (String [] args) {// 读取层次数据结果集列表 List dataList = virtualDatagenerator.getVirtualResult (); // 节点列表(散列表 , 用于临时存储节点对象) Hashmap nodelist = new HashMap (); // 根节点 nodo root = null; // 根据结果集构造节点列表(存入散列表) para (iterator it = dataList.iterator (); it.hasnext ();) {map dataCord = (map) it.next (); Nodo nodo = nuevo nodo (); node.id = (string) dataCord.get ("id"); node.text = (string) dataCord.get ("text"); node.parentId = (string) dataCord.get ("ParentId"); nodelist.put (node.id, nodo); } // 构造无序的多叉树 Establecer entrySet = nodelist.EntrySet (); for (iterator it = entryset.iterator (); it.hasnext ();) {nodo nodo = (nodo) ((map.entry) it.next ()). getValue (); if (node.parentid == null || node.parentid.equals ("")) {root = node; } else {((nodo) nodelist.get (node.parentid)). addChild (nodo); }} // 输出无序的树形菜单的 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: '廊坊银行解放道支行', // niños: [// {// id: '112200', // text: '廊坊银行三大街支行', // hoja: true //}, // {// id: '112100', // text: '廊坊银行广阳道支行', // leaf: true //} //] } //] //} // 程序输出结果如下(有序的树形菜单)(格式化后的结果) : // {// id: '100000', // text: '廊坊银行总行', // niños: [// {// id: '110000', // text: '廊坊分行', // children: [// {// id: '111000', // text: '廊坊银行金光道支行', // leaf: true //}, // {{{{112000 '. // texto: '廊坊银行解放道支行', // niños: [// {// id: '112100', // text: '廊坊银行广阳道支行', // hoja: true //}, // {// id: '112200', // text: '廊坊银行三大街支行' // hoja: true //} //] //}, // {// id: '113000', // text: '廊坊银行开发区支行, // hoja, //} //] //} //] //}}}/*** 节点类*/class node {/***节点编号*/public String id; / ** * 节点内容 */ Texto de cadena pública; / ** * 父节点编号 */ public String ParentId; / ** * 孩子节点列表 */ niños privados niños = nuevos niños (); // 先序遍历 , 拼接 JSON 字符串 public String toString () {String result = "{" + "id: '" + id + "'" + ", texto: '" + texto + "'"; if (children! = null && children.getSize ()! = 0) {resultado + = ", niños:" + children.toString (); } else {resultado += ", hoja: true"; } resultado de retorno + "}"; } // 兄弟节点横向排序 public void sortChildren () {if (children! = Null && children.getSize ()! = 0) {children.sortChildren (); }} // 添加孩子节点 public void addchild (nodo nodo) {this.children.addchild (nodo); }}/*** 孩子列表类*/class Children {Private List List = new ArrayList (); public int getsize () {return list.size (); } public void addchild (nodo de nodo) {list.add (nodo); } // 拼接孩子节点的 json 字符串 public string toString () {string dult = "["; for (iterator it = list.iterator (); it.hasnext ();) {resultado += ((nodo) it.next ()). toString (); resultado += ","; } resultado = resultado.substring (0, result.length () - 1); resultado += "]"; resultado de retorno; } // 孩子节点排序 public void sortchildren () {// 对本层节点进行排序 // 可根据不同的排序属性 , 传入不同的比较器 , 这里传入 id 比较器 colección.sort (list, new NodeIdComParator ()); // 对每个节点的下一层节点进行排序 para (iterator it = list.iterator (); it.hasnext ();) {((nodo) it.next ()). SortChildren (); }}}/** * 节点比较器 */class nodeidComparator implementa comparador {// 按照节点编号比较 public int compare (objeto o1, objeto o2) {int j1 = integer.parseInt (((nodo) o1) .id); int j2 = integer.ParseInt (((nodo) o2) .id); return (j1 <j2? -1: (j1 == J2? 0: 1)); }}/** * 构造虚拟的层次数据 */class virtualDataGenerator {// 构造无序的结果集列表 , 实际应用中 , 该数据应该从数据库中查询获得; public static list getVirtualResult () {list dataList = new ArrayList (); Hashmap dataCord1 = new HashMap (); dataRecord1.put ("id", "112000"); dataCord1.put ("Text", "廊坊银行解放道支行"); datArecord1.put ("ParentId", "110000"); Hashmap dataCord2 = new HashMap (); dataRecord2.put ("id", "112200"); dataRecord2.put ("text", "廊坊银行三大街支行"); dataCord2.put ("ParentId", "112000"); Hashmap dataCord3 = new HashMap (); datArecord3.put ("id", "112100"); datArecord3.put ("texto", "廊坊银行广阳道支行"); datArecord3.put ("ParentId", "112000"); Hashmap dataCord4 = new HashMap (); datArecord4.put ("id", "113000"); dataRecord4.put ("texto", "廊坊银行开发区支行"); datArecord4.put ("ParentId", "110000"); Hashmap dataCord5 = new HashMap (); dataRecord5.put ("id", "100000"); dataRecord5.put ("texto", "廊坊银行总行"); dataRecord5.put ("ParentId", ""); Hashmap dataCord6 = new HashMap (); dataRecord6.put ("id", "110000"); dataCord6.put ("Text", "廊坊分行"); datArecord6.put ("ParentId", "100000"); Hashmap dataCord7 = new HashMap (); dataCord7.put ("id", "111000"); dataCord7.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); devolver datalista; }}以上这篇 java 、 js 中实现无限层级的树形结构方法 (类似递归) 就是小编分享给大家的全部内容了 , 希望能给大家一个参考 也希望大家多多支持武林网。 也希望大家多多支持武林网。