【Java精品源码栏目提醒】:网学会员Java精品源码为您提供JAVA树形代码 - 其它资料参考,解决您在JAVA树形代码 - 其它资料学习中工作中的难题,参考学习。
解决 ExtJS 无限级树形菜单的问题,可以拓展出更多的应用场景,例如 BI(商业智能)系统中的报表分析中的数据钻取功能,也就是多级数据列表的展示,同时对多级数据列表按照某一列数据进行排序; 或者可以利用本文的思路扩展出其他的更复杂的应用场景。
二、详细设计方案让我们先看一段代码片段:文件一,branchTree.html (Ext 树形控件页面)Ext.onReady function var tree new Ext.tree.TreePanel height: 300 width: 400 animate:true enableDD:true containerScroll: true rootVisible: false frame: true loader: new Ext.tree.TreeLoaderdataUrl:getBranch.do //getBranch.do 请求服务器返回无限级的 JSON 字符串 root : new Ext.tree.AsyncTreeNodeid:0text:根结点 tree.expandAll 文件二,branchTreeJSON.jsp (接收 getBranch.do 请求,返回无限级 JSON 字符串)以上两个程序文件是一次性生成无限级树形菜单所必须的,其中最为关键的部分就是如何生成一个无限级的 JSON 字符串,返回给客户端的 Ext 树形控件。
对于银行分支机构来说,需要返回类似如下的 JSON 串: id : 100000 text : 廊坊银行总行 children : id : 110000 text : 廊坊分行 children : id : 113000text : 廊坊银行开发区支行leaf : trueid : 111000text : 廊坊银行金光道支行leaf : trueid : 112000text : 廊坊银行解放道支行children : id : 112200 text : 廊坊银行三大街支行 leaf : true id : 112100 text : 廊坊银行广阳道支行 leaf : true 同时还可能需要对树中每一个层次的节点按照某一属性(比如分支机构编号)进行排序,以展示出有序的树形菜单。
现在可以把问题概括为:1、 把层次数据转换成 JSON 格式的字符串2、 对树中每一个层次的节点按照某一属性(比如分支机构编号)进行排序下面介绍解决问题的思路:在数据结构这门课中,我们都学过二叉树和 B 树,二叉树属于内存数据结构,B树属于外存数据结构,我们的问题只涉及到内存操作,所以和 B 树无关,但是无限级树形菜单无法用二叉树来表示,因为每个节点下面都会有多个子节点,所以需要设计一种新的数据结构,用来表示这种多叉树结构,同时还要实现横向排序,即对隶属于同一个父节点下面的所有直接子节点按照某一节点属性和规则进行排序,保持兄弟节点横向有序。
如图所示:这棵树构造好之后,就可以通过纵向遍历(深度遍历)递归打印出无限级 JSON字符串了。
为了区别它和 B 树(一种外存多叉树),可以称它为内存多叉树。
概括起来分为三步:1、 构造无序的内存多叉树2、 实现兄弟节点横向排序方法3、 实现深度遍历方法,打印出 JSON 字符串三、源代码实现(
Java 语言版)实现这样一颗树, 树类 、 需要设计三个类: (ExtTree.
java) 节点类 (Node.
java)、孩子列表类(Children.
java);为了方便演示,还需要构造一些假的层次数据,因此还需要建一个构造假数据的类(VirtualDataGenerator.
java),以下代码拷贝出来之后可直接运行测试:001 package test002003004005 import
java.util.ArrayList006007 import
java.util.Comparator008009 import
java.util.HashMap010011 import
java.util.Iterator012013 import
java.util.List014015 import
java.util.Map016017 import
java.util.Set018019 import
java.util.Collections020021022023 /024025 树类026027 /028029 public class ExtTree 030031 public static void mainString args 032033 // 读取层次数据结果集列表034035 List dataList VirtualDataGenerator.getVirtualResult036037038039 // 节点列表(哈希表,用于临时存储节点对象)040041 HashMap nodeList new HashMap042043 // 根节点044045 Node root null046047 // 根据结果集构造节点列表(存入哈希表)048 for Iterator it dataList.iterator049 it.hasNext 050051 Map dataRecord Map it.next052053 Node node new Node054 node.id String055 dataRecord.getid056 node.text String057 dataRecord.gettext058 node.parentId String059 dataRecord.getparentId060061 nodeList.putnode.id node062063 064065 // 构造无序的内存多叉树066067 Set entrySet nodeList.entrySet068 for Iterator it entrySet.iterator069 it.hasNext 070 Node node Node Map.Entry071 it.next.getValue072 if node.parentId null 073 node.parentId.equals 074075 root node076077 else 078 Node079 nodeList.getnode.parentId.children.addChildnode080081 082083 084085 // 输出无序的树形菜单的 JSON 字符串086087 System.out.printlnroot.toString088089 // 对内存多叉树进行横向排序090091 root.sortChildren092093 // 输出有序的树形菜单的 JSON 字符串094095 System.out.printlnroot.toString096097098 // 程序输出结果如下(无序的树形菜单)(格式化后099 的结果):100101 //102103 // id : 100000104105 // text : 廊坊银行总行106107 // children : 108109 // 110111 // id : 110000112113 // text : 廊坊分行114115 // children : 116117 // 118119 // id : // text : 廊坊银行开发区支行122123 // leaf : true124125 // 126127 // 128129 // id : 111000130131 // text : 廊坊银行金光道支行132133 // leaf : true134135 // 136137 // 138139 // id : 112000140141 // text : 廊坊银行解放道支行142143 // children : 144145 // 146147 // id : 112200148 // text : 廊坊银行三大街支行149 150151 // leaf : true152153 // 154155 // 156157 // id : 112100158 // text : 廊坊银行广阳道支行159 160161 // leaf : true162163 // 164165 // 166167 // 168169 // 170171 // 172173 // 174175 //176177178 // 程序输出结果如下(有序的树形菜单)(格式化后179 的结果):180181 //182183 // id : 100000184185 // text : 廊坊银行总行186187 // children : 188189 // 190191 // id : 110000192193 // text : 廊坊分行194195 // children : 196197 // 198199 // id : 111000200201 // text : 廊坊银行金光道支行202203 // leaf : true204205 // 206207 // 208209 // id : 112000210211 // text : 廊坊银行解放道支行212213 // children : 214215 // 216217 // id : 112100218 // text : 廊坊银行广阳道支行219 220221 // leaf : true222223 // 224225 // 226227 // id : 112200228 // text : 廊坊银行三大街支行229 230231 // leaf : true232233 // 234235 // 236237 // 238239 // 240241 // id : // text : 廊坊银行开发区支行244245 // leaf : true246247 // 248249 // 250251 // 252253 // 254255 //256257258259 260261262263 264265266267268269 /270271 节点类272273 /274275 class Node 276277 /278279 节点编号280281 /282283 public String id284285 /286287 节点内容288289 /290291 public String text292293 /294295 父节点编号296297 /298299 public String parentId300301 /302303 孩子节点列表304305 /306307 public Children children new Children308309310311 // 深度遍历,拼接 JSON 字符串312313 public String toString 314315 String result 316317 id : id 318319 text : text 320321322 if children null children.getSize 0323 324 result children : 325 children.toString326327 else 328329 result leaf : true330331 332333334335 return result 336337 338339340341 // 对子节点进行横向排序342343 public void sortChildren 344 if children null children.getSize 0345 346347 children.sortChildren348349 350351 352353 354355356357 /358359 孩子列表类360361 /362363 class Children 364365 public List list new ArrayList366367368369 public int getSize 370371 return list.size372373 374375376377 public void addChildNode node 378379 list.addnode380381 382383384385 // 拼接孩子节点的 JSON 字符串386387 public String toString 388389 String result 390 for Iterator it list.iterator391 it.hasNext 392 result Node393 it.next.toString394395 result 396397 398 result result.substring0 result.length -399 1400401 result 402403 return result404405 406407408409 // 孩子节点排序410411 public void sortChildren 412413 // 对本层节点进行排序414 // 可根据不同的排序属性,传入不同的比较器,这里415 传入 ID 比较器416417 Collections.sortlist new NodeIDComparator418419 // 对每个节点的下一层节点进行排序420 for Iterator it list.iterator421 it.hasNext 422423 Node it.next.sortChildren424425 426427 428429 430431432433 /434435 节点比较器436437 /438439 class NodeIDComparator implements Comparator 440441 // 按照节点编号排序442443 public int compareObject o1 Object o2 444445 int j1 Integer.parseIntNodeo1.id446447 int j2 Integer.parseIntNodeo2.id448449 return j1 j2 -1 : j1 j2 0 : 1450451 452453 454455456457 /458459 构造虚拟的层次数据460461 /462463 class VirtualDataGenerator 464 // 构造无序的结果集列表,实际应用中,该数据应该从数据库中465 查询获得;466467 public static List getVirtualResult 468469 List dataList new ArrayList470471472473 HashMap dataRecord1 new HashMap474475 dataRecord1.putid 112000476477 dataRecord1.puttext 廊坊银行解放道支行478479 dataRecord1.putparentId 110000480481482483 HashMap dataRecord2 new HashMap484485 dataRecord2.putid 112200486487 dataRecord2.puttext 廊坊银行三大街支行488489 dataRecord2.putparentId 112000490491492493 HashMap dataRecord3 new HashMap494495 dataRecord3.putid 112100496497 dataRecord3.puttext 廊坊银行广阳道支行498499 dataRecord3.putparentId 112000500501502503 HashMap dataRecord4 new HashMap504505 dataRecord4.putid dataRecord4.puttext 廊坊银行开发区支行508509 dataRecord4.putparentId 110000510511512513 HashMap dataRecord5 new HashMap
上一篇:
【精品】第07章Java标准类库
下一篇:
英文课程作业:电影库(Piddington library )建立方案分析