java樹形結(jié)構(gòu)代碼 java樹形結(jié)構(gòu)遞歸實現(xiàn)

用java 編寫一個程序,在命令行中以樹狀結(jié)構(gòu)展現(xiàn)特定的文件夾及其子文件(夾)!

當然在理論上是可以實現(xiàn)的,可以將所有的子文件都以樹形結(jié)構(gòu)出來,但是文件很多的時候就會非常糾結(jié)

公司專注于為企業(yè)提供網(wǎng)站設計制作、成都網(wǎng)站制作、微信公眾號開發(fā)、購物商城網(wǎng)站建設,小程序設計,軟件定制網(wǎng)站等一站式互聯(lián)網(wǎng)企業(yè)服務。憑借多年豐富的經(jīng)驗,我們會仔細了解各客戶的需求而做出多方面的分析、設計、整合,為客戶設計出具風格及創(chuàng)意性的商業(yè)解決方案,創(chuàng)新互聯(lián)更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務。

我理解中的樹形結(jié)構(gòu)大概是這樣(不知道這樣的圖形是不是你想要的)

a

|

------------------

| | |

b c d

以下是代碼,找了系統(tǒng)盤下子文件較少的文件夾 C:/Windows/AppPatch,當然也可以換成你自己的路徑來測試

import java.io.File;

public class FileTree {

/**

* @param args

*/

public static void main(String[] args) {

try{

File file = new File("C:\\Windows\\AppPatch");

if(file.isDirectory()){

String[] fileList = file.list();

String fileName = file.getName();

int allLength = 0;

for(int i=0;ifileList.length;i++){

allLength += (fileList[i]+" ").length();

}

for(int i=0;iallLength/2;i++){

System.out.print(" ");

}

System.out.println(fileName);

for(int i=0;iallLength/2;i++){

System.out.print(" ");

}

for(int i=0;ifileName.length()/2;i++){

System.out.print(" ");

}

System.out.println("|");

for(int i=0;iallLength;i++){

System.out.print("-");

}

System.out.println("");

for(int i=0;ifileList.length;i++){

int tmpLength = fileList[i].length();

int subLength = tmpLength/2;

int lastLength = tmpLength - subLength - 1;

for(int j=0;jsubLength;j++){

System.out.print(" ");

}

System.out.print("|");

for(int j=0;jlastLength;j++){

System.out.print(" ");

}

System.out.print(" ");

}

System.out.println("");

for(int i=0;ifileList.length;i++){

System.out.print(fileList[i]+" ");

}

}

else{

System.out.println("對不起,你提供的路徑不是文件夾");

}

}

catch (Exception e) {

e.printStackTrace();

}

}

}

這時可以發(fā)現(xiàn)輸出每一個子文件/子文件夾的名字已經(jīng)比較長,要是再想輸出這些子文件夾里面的文件,那幅圖個人覺得相當糾結(jié),也許是我水平?jīng)]夠吧或是我理解錯了你說的樹形結(jié)構(gòu)

希望以上代碼對你有幫助

用Java實現(xiàn)一個樹形結(jié)構(gòu),并對其進行遍歷

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進行簡單的二叉樹實現(xiàn),另有遍歷,當然遍歷是自然順序。

//如有需要請自行修改吧。

javaweb里面樹形結(jié)構(gòu)(tree)

這個是java中的forEach循環(huán),和

for(int?i?=0?;i??10?;i++){...}

還是有點區(qū)別的。有問題可以繼續(xù) 問。

如何用Java實現(xiàn)樹形結(jié)構(gòu)啊?

package tree;

import java.util.LinkedList;

import java.util.List;

/**

* 功能:把一個數(shù)組的值存入二叉樹中,然后進行3種方式的遍歷

*

* 參考資料0:數(shù)據(jù)結(jié)構(gòu)(C語言版)嚴蔚敏

*

* 參考資料1:

*

* 參考資料2:

*

* @author ocaicai@yeah點虐 @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é)點

*

* @author ocaicai@yeah點虐 @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é)點

for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {

nodeList.add(new Node(array[nodeIndex]));

}

// 對前l(fā)astParentIndex-1個父節(jié)點按照父節(jié)點與孩子節(jié)點的數(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é)點:因為最后一個父節(jié)點可能沒有右孩子,所以單獨拿出來處理

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é)點

*/

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é)點

*/

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é)點

*/

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é)點

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樹形結(jié)構(gòu)代碼 java樹形結(jié)構(gòu)遞歸實現(xiàn)
文章轉(zhuǎn)載:http://muchs.cn/article12/ddcgcgc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、定制開發(fā)域名注冊、搜索引擎優(yōu)化服務器托管、App設計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設