樹節(jié)點(diǎn)類:
成都創(chuàng)新互聯(lián)公司是專業(yè)的鼓樓網(wǎng)站建設(shè)公司,鼓樓接單;提供成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行鼓樓網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
package?cn點(diǎn)抗 .tree;??
public?class?Node?{??
private?Integer?id;??
private?Integer?parentId;??
private?String?name;??
private?String?link;??
public?Integer?getId()?{??
return?id;??
}??
public?void?setId(Integer?id)?{??
this.id?=?id;??
}??
public?Integer?getParentId()?{??
return?parentId;??
}??
public?void?setParentId(Integer?parentId)?{??
this.parentId?=?parentId;??
}??
public?String?getName()?{??
return?name;??
}??
public?void?setName(String?name)?{??
this.name?=?name;??
}??
public?String?getLink()?{??
return?link;??
}??
public?void?setLink(String?link)?{??
this.link?=?link;??
}??
}
輸出樹形菜單類:
package?cn點(diǎn)抗 .tree;??
import?java.util.ArrayList;??
import?java.util.List;??
public?class?Tree?{??
private?StringBuffer?html?=?new?StringBuffer();??
private?ListNode?nodes;??
public?Tree(ListNode?nodes){??
this.nodes?=?nodes;??
}??
public?String?buildTree(){??
html.append("ul");??
for?(Node?node?:?nodes)?{??
Integer?id?=?node.getId();??
if?(node.getParentId()?==?null)?{??
html.append("\r\nli?id='"?+?id?+?"'"?+?node.getName()+?"/li");??
build(node);??
}??
}??
html.append("\r\n/ul");??
return?html.toString();??
}??
private?void?build(Node?node){??
ListNode?children?=?getChildren(node);??
if?(!children.isEmpty())?{??
html.append("\r\nul");??
for?(Node?child?:?children)?{??
Integer?id?=?child.getId();??
html.append("\r\nli?id='"?+?id?+?"'"?+?child.getName()+?"/li");??
build(child);??
}??
html.append("\r\n/ul");??
}???
}??
private?ListNode?getChildren(Node?node){??
ListNode?children?=?new?ArrayListNode();??
Integer?id?=?node.getId();??
for?(Node?child?:?nodes)?{??
if?(id.equals(child.getParentId()))?{??
children.add(child);??
}??
}??
return?children;??
}??
}
測試類:
package?zzj.test;??
import?java.util.ArrayList;??
import?java.util.List;??
import?cn點(diǎn)抗 .tree.Node;??
import?cn點(diǎn)抗 .tree.Tree;??
public?class?Test?{??
/**?
*?@param?args?
*/??
public?static?void?main(String[]?args)?{??
ListNode?nodes?=?new?ArrayListNode();??
Node?node1?=?new?Node();??
node1.setId(1);??
node1.setName("node1");??
node1.setParentId(null);??
node1.setLink(null);??
nodes.add(node1);??
Node?node11?=?new?Node();??
node11.setId(11);??
node11.setName("node11");??
node11.setParentId(1);??
node11.setLink(null);??
nodes.add(node11);??
Node?node111?=?new?Node();??
node111.setId(111);??
node111.setName("node111");??
node111.setParentId(11);??
node111.setLink(null);??
nodes.add(node111);??
Node?node12?=?new?Node();??
node12.setId(12);??
node12.setName("node12");??
node12.setParentId(1);??
node12.setLink(null);??
nodes.add(node12);??
Node?node2?=?new?Node();??
node2.setId(2);??
node2.setName("node2");??
node2.setParentId(null);??
node2.setLink(null);??
nodes.add(node2);??
Node?node21?=?new?Node();??
node21.setId(21);??
node21.setName("node21");??
node21.setParentId(2);??
node21.setLink(null);??
nodes.add(node21);??
Node?node3?=?new?Node();??
node3.setId(3);??
node3.setName("node3");??
node3.setParentId(null);??
node3.setLink(null);??
nodes.add(node3);??
Tree?tree?=?new?Tree(nodes);??
System.out.println(tree.buildTree());??
}??
}
樹與二叉樹實(shí)現(xiàn)差不多,二叉樹類變量里面有兩個節(jié)點(diǎn),通過配置一些參數(shù)讓數(shù)據(jù)庫性能達(dá)到最優(yōu)。
用Java實(shí)現(xiàn)的數(shù)據(jù)樹形封裝。
package tree;
import java.util.LinkedList;
import java.util.List;
/**
* 功能:把一個數(shù)組的值存入二叉樹中,然后進(jìn)行3種方式的遍歷
*
* 參考資料0:數(shù)據(jù)結(jié)構(gòu)(C語言版)嚴(yán)蔚敏
*
* 參考資料1:
*
* 參考資料2:
*
* @author ocaicai@yeah點(diǎn)虐 @date: 2011-5-17
*
*/
public class BinTreeTraverse2 {
private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static ListNode nodeList = null;
/**
* 內(nèi)部類:節(jié)點(diǎn)
*
* @author ocaicai@yeah點(diǎn)虐 @date: 2011-5-17
*
*/
private static class Node {
Node leftChild;
Node rightChild;
int data;
Node(int newData) {
leftChild = null;
rightChild = null;
data = newData;
}
}
public void createBinTree() {
nodeList = new LinkedListNode();
// 將一個數(shù)組的值依次轉(zhuǎn)換為Node節(jié)點(diǎn)
for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {
nodeList.add(new Node(array[nodeIndex]));
}
// 對前l(fā)astParentIndex-1個父節(jié)點(diǎn)按照父節(jié)點(diǎn)與孩子節(jié)點(diǎn)的數(shù)字關(guān)系建立二叉樹
for (int parentIndex = 0; parentIndex array.length / 2 - 1; parentIndex++) {
// 左孩子
nodeList.get(parentIndex).leftChild = nodeList
.get(parentIndex * 2 + 1);
// 右孩子
nodeList.get(parentIndex).rightChild = nodeList
.get(parentIndex * 2 + 2);
}
// 最后一個父節(jié)點(diǎn):因?yàn)樽詈笠粋€父節(jié)點(diǎn)可能沒有右孩子,所以單獨(dú)拿出來處理
int lastParentIndex = array.length / 2 - 1;
// 左孩子
nodeList.get(lastParentIndex).leftChild = nodeList
.get(lastParentIndex * 2 + 1);
// 右孩子,如果數(shù)組的長度為奇數(shù)才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).rightChild = nodeList
.get(lastParentIndex * 2 + 2);
}
}
/**
* 先序遍歷
*
* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已
*
* @param node
* 遍歷的節(jié)點(diǎn)
*/
public static void preOrderTraverse(Node node) {
if (node == null)
return;
System.out.print(node.data + " ");
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}
/**
* 中序遍歷
*
* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已
*
* @param node
* 遍歷的節(jié)點(diǎn)
*/
public static void inOrderTraverse(Node node) {
if (node == null)
return;
inOrderTraverse(node.leftChild);
System.out.print(node.data + " ");
inOrderTraverse(node.rightChild);
}
/**
* 后序遍歷
*
* 這三種不同的遍歷結(jié)構(gòu)都是一樣的,只是先后順序不一樣而已
*
* @param node
* 遍歷的節(jié)點(diǎn)
*/
public static void postOrderTraverse(Node node) {
if (node == null)
return;
postOrderTraverse(node.leftChild);
postOrderTraverse(node.rightChild);
System.out.print(node.data + " ");
}
public static void main(String[] args) {
BinTreeTraverse2 binTree = new BinTreeTraverse2();
binTree.createBinTree();
// nodeList中第0個索引處的值即為根節(jié)點(diǎn)
Node root = nodeList.get(0);
System.out.println("先序遍歷:");
preOrderTraverse(root);
System.out.println();
System.out.println("中序遍歷:");
inOrderTraverse(root);
System.out.println();
System.out.println("后序遍歷:");
postOrderTraverse(root);
}
}
本文標(biāo)題:java代碼實(shí)現(xiàn)tree的簡單介紹
文章轉(zhuǎn)載:http://www.rwnh.cn/article38/ddcpssp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、網(wǎng)站策劃、商城網(wǎng)站、App開發(fā)、軟件開發(fā)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)