本文目錄一覽:
- 1、java怎麼對樹形結構進行遍歷
- 2、如何用Java實現樹形結構啊?
- 3、java 如何實現樹、圖結構?
- 4、用Java實現一個樹形結構,並對其進行遍歷
- 5、Java怎麼實現輸出是一個tree結構
- 6、樹在java中的應用有哪些
java怎麼對樹形結構進行遍歷
java”import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;
public class Demo{
public static void main(String[] args) throws Exception {
TreeSetInteger ts = new TreeSetInteger();
for(int i = 0; i 10; i++){
ts.add(new Random().nextInt(999));
}
for(IteratorInteger it = ts.iterator(); it.hasNext();){
System.out.println(it.next());
}
}
}
如何用Java實現樹形結構啊?
package tree;
import java.util.LinkedList;
import java.util.List;
/**
* 功能:把一個數組的值存入二叉樹中,然後進行3種方式的遍歷
*
* 參考資料0:數據結構(C語言版)嚴蔚敏
*
* 參考資料1:
*
* 參考資料2:
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
public class BinTreeTraverse2 {
private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static ListNode nodeList = null;
/**
* 內部類:節點
*
* @author ocaicai@yeah.net @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();
// 將一個數組的值依次轉換為Node節點
for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {
nodeList.add(new Node(array[nodeIndex]));
}
// 對前lastParentIndex-1個父節點按照父節點與孩子節點的數字關係建立二叉樹
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);
}
// 最後一個父節點:因為最後一個父節點可能沒有右孩子,所以單獨拿出來處理
int lastParentIndex = array.length / 2 – 1;
// 左孩子
nodeList.get(lastParentIndex).leftChild = nodeList
.get(lastParentIndex * 2 + 1);
// 右孩子,如果數組的長度為奇數才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).rightChild = nodeList
.get(lastParentIndex * 2 + 2);
}
}
/**
* 先序遍歷
*
* 這三種不同的遍歷結構都是一樣的,只是先後順序不一樣而已
*
* @param node
* 遍歷的節點
*/
public static void preOrderTraverse(Node node) {
if (node == null)
return;
System.out.print(node.data + ” “);
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}
/**
* 中序遍歷
*
* 這三種不同的遍歷結構都是一樣的,只是先後順序不一樣而已
*
* @param node
* 遍歷的節點
*/
public static void inOrderTraverse(Node node) {
if (node == null)
return;
inOrderTraverse(node.leftChild);
System.out.print(node.data + ” “);
inOrderTraverse(node.rightChild);
}
/**
* 後序遍歷
*
* 這三種不同的遍歷結構都是一樣的,只是先後順序不一樣而已
*
* @param node
* 遍歷的節點
*/
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個索引處的值即為根節點
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);
}
}
java 如何實現樹、圖結構?
你如果要樹形展示,在JSP上只能用樹控制項,類似dtree.js這種第三方JS包
如果是樹體系,JAVA還是面向對象,只能用代碼描述出一棵樹,包括各個屬性,能通過數據體現一個樹的體系(子父編號關聯),但無法直觀的看出圖形來
用Java實現一個樹形結構,並對其進行遍歷
import java.util.Iterator;
import java.util.Random;
import java.util.TreeSet;
public class Demo{
public static void main(String[] args) throws Exception {
TreeSetInteger ts = new TreeSetInteger();
for(int i = 0; i 10; i++){
ts.add(new Random().nextInt(999));
}
for(IteratorInteger it = ts.iterator(); it.hasNext();){
System.out.println(it.next());
}
}
}
//上面是利用TreeSet進行簡單的二叉樹實現,另有遍歷,當然遍歷是自然順序。
//如有需要請自行修改吧。
Java怎麼實現輸出是一個tree結構
樹節點類:
package cn.com.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.com.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.com.tree.Node;
import cn.com.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());
}
}
樹在java中的應用有哪些
首先:樹與線性表、棧、隊列等線性結構不同,樹是一種非線性結構。一棵樹只有一個根節點,如果一棵樹有了多個根節點,那它已經不再是一棵樹了,而是多棵樹的集合,也被稱為森林。
其次:java中樹的應用主要有:菜單樹,還有許可權樹,商品分類列表也是樹結構。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/184008.html