本文目錄一覽:
- 1、急求:java/jsp 如何實現橫向樹形結構的顯示(如圖)?
- 2、如何用Java實現樹形結構啊?
- 3、java樹狀結構圖如果控制許可權,我是把數據全部存在一張表中,子節點存有父節點的id號,樹狀圖已經出來
急求:java/jsp 如何實現橫向樹形結構的顯示(如圖)?
你是要動態現實目錄的層次結構嗎?
我只能採用遞歸幫你顯示目錄,不能實現樹狀顯示
package com.lx;
import java.io.File;
public class FileList {
public static void main(String[] args) {
File f = new File(“e:/資料”);
System.out.println(f.getName());
tree(f, 1);
}
private static void tree(File f, int level) {
String preStr = “”;
for (int i = 0; i level; i++) {
preStr += ” “;
}
File[] childs = f.listFiles();
for (int i = 0; i childs.length; i++) {
System.out.println(preStr + childs[i].getName());
if (childs[i].isDirectory()) {
tree(childs[i], level + 1);
}
}
}
}
如何用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樹狀結構圖如果控制許可權,我是把數據全部存在一張表中,子節點存有父節點的id號,樹狀圖已經出來
我想你這個要實現的所謂的許可權應該是,只要用戶點擊某個節點就查詢其是否有該節點的許可權吧?如果有許可權就展示有許可權操作的內容,沒有就提示沒有許可權?我不知道我理解的對不對。
或者是另一種情況,就是當前登錄的用戶有哪些菜單的許可權就在樹形圖中只展示他有許可權的菜單。
前者需要通過ajax去資料庫判斷,當然如果對無刷新要求不高可以直接跳action去資料庫判斷;後者在輸出樹狀圖的時候就把許可權判斷好,後面就不需要判斷了。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/279437.html