java树状结构代码 java 树状结构

用java 编写一个程序,在命令行中以树状结构展现特定的文件夹及其子文件(夹)!当然在理论上是可以实现的,可以将所有的子文件都以树形结构出来,但是文件很多的时候就会非常纠结
我理解中的树形结构大概是这样(不知道这样的图形是不是你想要的)
a
|
------------------
|||
bcd
以下是代码,找了系统盘下子文件较少的文件夹 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();
}
}
}
这时可以发现输出每一个子文件/子文件夹的名字已经比较长,要是再想输出这些子文件夹里面的文件 , 那幅图个人觉得相当纠结,也许是我水平没够吧或是我理解错了你说的树形结构
希望以上代码对你有帮助
用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; i10; 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实现树形结构?。?/h2>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 = https://www.04ip.com/post/newData;
}
}
public void createBinTree() {
nodeList = new LinkedListNode();
// 将一个数组的值依次转换为Node节点
for (int nodeIndex = 0; nodeIndexarray.length; nodeIndex) {
nodeList.add(new Node(array[nodeIndex]));
}
// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
for (int parentIndex = 0; parentIndexarray.length / 2 - 1; parentIndex) {
// 左孩子
nodeList.get(parentIndex).leftChild = nodeList
.get(parentIndex * 21);
// 右孩子
nodeList.get(parentIndex).rightChild = nodeList
.get(parentIndex * 22);
}
// 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
int lastParentIndex = array.length / 2 - 1;
// 左孩子
nodeList.get(lastParentIndex).leftChild = nodeList
.get(lastParentIndex * 21);
// 右孩子,如果数组的长度为奇数才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).rightChild = nodeList
.get(lastParentIndex * 22);
}
}
/**
* 先序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @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树状结构代码 java 树状结构】java树状结构代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java 树状结构、java树状结构代码的信息别忘了在本站进行查找喔 。

    推荐阅读