Java的二叉樹排序和遍歷文件展現文本格局的文件樹。本站提示廣大學習愛好者:(Java的二叉樹排序和遍歷文件展現文本格局的文件樹)文章只能為提供參考,不一定能成為您想要的結果。以下是Java的二叉樹排序和遍歷文件展現文本格局的文件樹正文
Java二叉樹排序算法
排序二叉樹的描寫也是一個遞歸的描寫, 所以排序二叉樹的結構天然也用遞歸的:
排序二叉樹的3個特點:
1:以後node的一切左孩子的值都小於以後node的值;
2:以後node的一切右孩子的值都年夜於以後node的值;
3:孩子節點也知足以上兩點
package test.sort;
public class BinaryNode {
private int value;//current value
private BinaryNode lChild;//left child
private BinaryNode rChild;//right child
public BinaryNode(int value, BinaryNode l, BinaryNode r){
this.value = value;
this.lChild = l;
this.rChild = r;
}
public BinaryNode getLChild() {
return lChild;
}
public void setLChild(BinaryNode child) {
lChild = child;
}
public BinaryNode getRChild() {
return rChild;
}
public void setRChild(BinaryNode child) {
rChild = child;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
//iterate all node.
public static void iterate(BinaryNode root){
if(root.lChild!=null){
iterate(root.getLChild());
}
System.out.print(root.getValue() + " ");
if(root.rChild!=null){
iterate(root.getRChild());
}
}
/**
* add child to the current node to construct a tree.
* Time: O( nlog(n) )
* **/
public void addChild(int n){
if(n<value){
if(lChild!=null){
lChild.addChild(n);
}
else{
lChild = new BinaryNode(n, null, null);
}
}
else{
if(rChild!=null){
rChild.addChild(n);
}
else{
rChild = new BinaryNode(n, null, null);
}
}
}
//test case.
public static void main(String[] args){
System.out.println();
int[] arr = new int[]{23,54,1,65,9,3,100};
BinaryNode root = new BinaryNode(arr[0], null, null);
for(int i=1; i<arr.length; i++){
root.addChild(arr[i]);
}
BinaryNode.iterate(root);
}
}
Java遍歷文件展現文本格局的文件樹
用java寫一個代碼變歷文件樹,打印出構造,相似在cmd輸出敕令tree的成果。
原來認為很簡略,做的時刻才曉得有點難。如果感興致, 你也能夠嘗嘗。
package test.io;
//在網上找的,據說照樣老字竹原創。代碼簡練,然則我費了好年夜的功副消化
import java.util.ArrayList;
import java.util.List;
public class Folder {
public Folder(String title) {
this.title = title;
}
private String title;
private List<Folder> children = new ArrayList<Folder>();
public void addChild(Folder f) {
children.add(f);
}
public List<Folder> getChildren() {
return children;
}
public void setChildren(List<Folder> children) {
this.children = children;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String toString(String lftStr, String append) {
StringBuilder b = new StringBuilder();
b.append(append + title);
b.append("/n");
if (children.size() > 0) {
for (int i = 0; i < children.size() - 1; i++) {
b.append(lftStr+ children.get(i).toString(lftStr + "│ ",
"├-"));
}
b.append(lftStr+ children.get(children.size() - 1).toString(lftStr +
" ","└-"));
}
return b.toString();
}
public static void main(String[] args) {
Folder root = new Folder("菜單列表");
Folder f1 = new Folder("開端菜單");
root.addChild(f1);
Folder f1_1 = new Folder("法式");
f1.addChild(f1_1);
Folder f1_1_1 = new Folder("附件");
f1_1.addChild(f1_1_1);
Folder f1_1_1_1 = new Folder("文娛");
f1_1_1.addChild(f1_1_1_1);
Folder f1_1_1_2 = new Folder("文娛2");
f1_1_1.addChild(f1_1_1_2);
Folder f1_2 = new Folder("幫助對象");
f1.addChild(f1_2);
System.out.println(root.toString(" ", "$"));
}
}
//**************************************
//經由消化以後我修正的。可打印文件構造
import java.io.*;
public class DocTree {
File root = null;
public DocTree(File f){
this.root = f;
}
public static void main(String[] args){
File root = new File("c://test");
DocTree tree = new DocTree(root);
System.out.println(tree.toString(" ", ""));
}
public String toString(String leftStr, String append){
StringBuilder b = new StringBuilder();
b.append(append + root.getName());
b.append("/n");
if(!root.isFile()&&root.listFiles().length!=0){
File[] files = root.listFiles();
DocTree[] docTrees = new DocTree[files.length];
for(int i=0; i<docTrees.length; i++){
docTrees[i] = new DocTree(files[i]);
}
for (int i=0; i<files.length-1; i++){
b.append(leftStr + docTrees[i].toString(leftStr+"│", "├"));
}
b.append(leftStr + docTrees[docTrees.length-1].toString(leftStr + " ", "└"));
}
return b.toString();
}
}
//*****************************************
//然後我照樣認為懂得起來不便利, 過幾天說不定就忘卻了,
//照樣本身寫一個, 固然思惟照抄, 但我認為本身的懂得起來很便利。
//帶正文,
import java.io.*;
public class Tree {
File root = null;
public Tree(File f){
this.root = f;
}
/**
test
├1
│├目次1.txt
│├目次11
││├111.txt
││└112.txt
│└12
└test.pdf
*/
/**
* @param root 以後正在被掃描的根文件
* @param childLeftStr 假如該文件有孩子,childLeftStr
* 表現孩子節點的左面應當打印出來的構造性信息
* 拿下面的例子來講,根結點test的孩子的左面的
* 構造信息為"" 空,結點"目次11"的孩子的構造信息為"││",
* @param junction 結點圖標,假如是該結點是它父親的最初一個結點,
* 則為"└",不然為"├".
*/
public void showTree(File root, String childLeftStr, String junction){
//打印結點的信息
System.out.println(junction + root.getName());
//假如有孩子, 並且孩子的數量不為0
if(!root.isFile()&&root.listFiles().length!=0){
File[] files = root.listFiles();
//結構孩子結點
Tree[] children = new Tree[files.length];
for(int i=0; i<files.length; i++){
children[i] = new Tree(files[i]);
}
//打印孩子結點
for(int i=0; i<children.length-1; i++){
//對一切的孩子結點,先打印出右邊的構造信息,
System.out.print(childLeftStr);
//遞歸挪用showTree, 留意參數有所變更,文件加的深度增長的時刻
,它的孩子的構造信息也會
//增長,假如不是最初一個孩子,則構造信息需加上"│"。
showTree(children[i].root,childLeftStr+"│", "├");
}
//最初一個孩子須要特別處置
//打印構造信息
System.out.print(childLeftStr);
//假如是最初一個孩子,則構造信息需加上" "。
//結點外形也調劑為"└"
showTree(children[files.length-1].root, childLeftStr+" ","└");
}
}
public static void main(String[] args) {
File f = new File("C://test");
Tree t = new Tree(f);
t.showTree(f,"", "");
}
}