一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|

服務(wù)器之家 - 編程語言 - JAVA教程 - java、js中實(shí)現(xiàn)無限層級的樹形結(jié)構(gòu)方法(類似遞歸)

java、js中實(shí)現(xiàn)無限層級的樹形結(jié)構(gòu)方法(類似遞歸)

2020-07-01 11:20jingxian JAVA教程

下面小編就為大家?guī)硪黄猨ava、js中實(shí)現(xiàn)無限層級的樹形結(jié)構(gòu)方法(類似遞歸)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

js中:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
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é)點(diǎn)列表(散列表,用于臨時(shí)存儲節(jié)點(diǎn)對象)
 HashMap nodeList = new HashMap();
 // 根節(jié)點(diǎn)
 Node root = null;
 // 根據(jù)結(jié)果集構(gòu)造節(jié)點(diǎn)列表(存入散列表)
 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()); 
 // 對多叉樹進(jìn)行橫向排序
 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é)點(diǎn)類
*/
class Node {
 /**
 * 節(jié)點(diǎn)編號
 */
 public String id;
 /**
 * 節(jié)點(diǎn)內(nèi)容
 */
 public String text;
 /**
 * 父節(jié)點(diǎn)編號
 */
 public String parentId;
 /**
 * 孩子節(jié)點(diǎn)列表
 */
 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é)點(diǎn)橫向排序
 public void sortChildren() {
 if (children != null && children.getSize() != 0) {
  children.sortChildren();
 }
 }
 
 // 添加孩子節(jié)點(diǎn)
 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é)點(diǎn)的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é)點(diǎn)排序
 public void sortChildren() {
 // 對本層節(jié)點(diǎn)進(jìn)行排序
 // 可根據(jù)不同的排序?qū)傩裕瑐魅氩煌谋容^器,這里傳入ID比較器
 Collections.sort(list, new NodeIDComparator());
 // 對每個(gè)節(jié)點(diǎn)的下一層節(jié)點(diǎn)進(jìn)行排序
 for (Iterator it = list.iterator(); it.hasNext();) {
  ((Node) it.next()).sortChildren();
 }
 }
}
 
/**
 * 節(jié)點(diǎn)比較器
 */
class NodeIDComparator implements Comparator {
 // 按照節(jié)點(diǎn)編號比較
 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é)果集列表,實(shí)際應(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中實(shí)現(xiàn)無限層級的樹形結(jié)構(gòu)方法(類似遞歸)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 白丝女仆被啪到深夜漫画 | 免费看3d小舞被躁视频网站 | 亚洲 综合 自拍 精品 在线 | 亚洲香蕉综合在人在线视看 | 99热导航| 麻豆天美精东果冻传媒在线 | 国产理论片在线观看 | 手机看片福利 | 4hu影院永久在线播放 | 四虎传媒 | 扒开双腿疯狂进出爽爽动态图 | 国产精品边做边接电话在线观看 | 国内会所按摩推拿国产 | 高清免费毛片 | 久草热8精品视频在线观看 久草草在线视视频 | 亚洲国产精品久久精品成人网站 | 日本免费一区二区三区a区 日本免费三片在线观看 | 青青草成人在线 | 精品无人区乱码1区2区3区免费 | 亚洲精品国产成人7777 | 欧美一级特黄特色大片免费 | 日韩精品一二三区 | 青草视频免费观看在线观看 | 国产亚洲精品一区二区在线播放 | 亚洲剧情在线观看 | 我的奶头被客人吸的又肿又红 | 久久午夜一区二区 | 欧美黄站 | 亚洲精品九色在线网站 | 欧美成人二区 | 福利国产精品 | 四虎永久免费在线观看 | 97精品国产自在现线免费观看 | 鄂州一家三口完整版免费 | 免费被黄网站在观看 | 欧美一区二区三区高清不卡tv | a免费看| 免费观看在线 | 国产欧美日韩在线不卡第一页 | 亚洲 欧美 国产 综合 在线 | 国产人成激情视频在线观看 |