java、js中實現(xiàn)無限層級的樹形結(jié)構(gòu)方法(類似遞歸)
更新時間:2016年11月05日 15:57:01 投稿:jingxian
下面小編就為大家?guī)硪黄猨ava、js中實現(xiàn)無限層級的樹形結(jié)構(gòu)方法(類似遞歸)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
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: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"} ]; function treeMenu(a){ this.tree=a||[]; this.groups={}; }; treeMenu.prototype={ init:function(pid){ this.group(); return 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'; return html; } }; var html=new treeMenu(zNodes).init(0); alert(html);
java:
package test; 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; /** * 多叉樹類 */ public class MultipleTree { public static void main(String[] args) { // 讀取層次數(shù)據(jù)結(jié)果集列表 List dataList = VirtualDataGenerator.getVirtualResult(); // 節(jié)點列表(散列表,用于臨時存儲節(jié)點對象) HashMap nodeList = new HashMap(); // 根節(jié)點 Node root = null; // 根據(jù)結(jié)果集構(gòu)造節(jié)點列表(存入散列表) 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("parentId"); nodeList.put(node.id, node); } // 構(gòu)造無序的多叉樹 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()); // 程序輸出結(jié)果如下(無序的樹形菜單)(格式化后的結(jié)果): // { // id : '100000', // text : '廊坊銀行總行', // children : [ // { // id : '110000', // text : '廊坊分行', // children : [ // { // id : '113000', // text : '廊坊銀行開發(fā)區(qū)支行', // leaf : true // }, // { // id : '111000', // text : '廊坊銀行金光道支行', // leaf : true // }, // { // id : '112000', // text : '廊坊銀行解放道支行', // children : [ // { // id : '112200', // text : '廊坊銀行三大街支行', // leaf : true // }, // { // id : '112100', // text : '廊坊銀行廣陽道支行', // leaf : true // } // ] // } // ] // } // ] // } // 程序輸出結(jié)果如下(有序的樹形菜單)(格式化后的結(jié)果): // { // 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 : '廊坊銀行開發(fā)區(qū)支行', // leaf : true // } // ] // } // ] // } } } /** * 節(jié)點類 */ class Node { /** * 節(jié)點編號 */ public String id; /** * 節(jié)點內(nèi)容 */ public String text; /** * 父節(jié)點編號 */ public String parentId; /** * 孩子節(jié)點列表 */ private Children children = new Children(); // 先序遍歷,拼接JSON字符串 public String toString() { String result = "{" + "id : '" + id + "'" + ", text : '" + text + "'"; if (children != null && children.getSize() != 0) { result += ", children : " + children.toString(); } else { result += ", leaf : true"; } return result + "}"; } // 兄弟節(jié)點橫向排序 public void sortChildren() { if (children != null && children.getSize() != 0) { children.sortChildren(); } } // 添加孩子節(jié)點 public void addChild(Node node) { this.children.addChild(node); } } /** * 孩子列表類 */ class Children { private List list = new ArrayList(); public int getSize() { return list.size(); } public void addChild(Node node) { list.add(node); } // 拼接孩子節(jié)點的JSON字符串 public String toString() { String result = "["; for (Iterator it = list.iterator(); it.hasNext();) { result += ((Node) it.next()).toString(); result += ","; } result = result.substring(0, result.length() - 1); result += "]"; return result; } // 孩子節(jié)點排序 public void sortChildren() { // 對本層節(jié)點進行排序 // 可根據(jù)不同的排序?qū)傩?,傳入不同的比較器,這里傳入ID比較器 Collections.sort(list, new NodeIDComparator()); // 對每個節(jié)點的下一層節(jié)點進行排序 for (Iterator it = list.iterator(); it.hasNext();) { ((Node) it.next()).sortChildren(); } } } /** * 節(jié)點比較器 */ class NodeIDComparator implements Comparator { // 按照節(jié)點編號比較 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)); } } /** * 構(gòu)造虛擬的層次數(shù)據(jù) */ class VirtualDataGenerator { // 構(gòu)造無序的結(jié)果集列表,實際應(yīng)用中,該數(shù)據(jù)應(yīng)該從數(shù)據(jù)庫中查詢獲得; 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", "廊坊銀行開發(fā)區(qū)支行"); 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); return dataList; } }
以上這篇java、js中實現(xiàn)無限層級的樹形結(jié)構(gòu)方法(類似遞歸)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決mybatis使用char類型字段查詢oracle數(shù)據(jù)庫時結(jié)果返回null問題
這篇文章主要介紹了mybatis使用char類型字段查詢oracle數(shù)據(jù)庫時結(jié)果返回null問題的解決方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06