树结构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());

}

敏磨族}

}

//上面是利用TreeSet进行简单的二叉树实现,另有遍历,当然遍历是自然顺序。

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

用java 编写一个程序,在命令行中以树状结构展现特定的文件夹及其子文件(夹)!

当然在理论上是可以实现的,可以将所有的子文件都以树形结构出来,但是文件很多的时候就会非常纠结

我理解中的树形结构大概是这样(不知道这样的图形是不是你想要的)

a

|

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

| | |

b c d

以下是代码,找了系统盘下子文件较少的文件夹 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怎么实现输出是一个tree结构

树节点类:

package cn点抗 .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点抗 .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点抗 .tree.Node;  

import cn点抗 .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实现树形结构啊?

package tree;

import java.util.LinkedList;

import java.util.List;

/**

* 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历

*

* 参考资料0:数据结构(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;

/**

* 内部类:节点

*

* @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();

// 将一个数组的值依次转换为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代码 java 树
路径分享:https://www.cdcxhl.com/article42/ddpdphc.html

成都网站建设公司_创新互联,为您提供域名注册响应式网站网站营销电子商务做网站

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

商城网站建设