Java的二叉樹排序以及遍歷文件展示文本格式的文件樹
Java二叉樹排序算法
排序二叉樹的描述也是一個(gè)遞歸的描述, 所以排序二叉樹的構(gòu)造自然也用遞歸的:
排序二叉樹的3個(gè)特征:
1:當(dāng)前node的所有左孩子的值都小于當(dāng)前node的值;
2:當(dāng)前node的所有右孩子的值都大于當(dāng)前node的值;
3:孩子節(jié)點(diǎn)也滿足以上兩點(diǎn)
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寫一個(gè)代碼變歷文件樹,打印出結(jié)構(gòu),類似在cmd輸入命令tree的結(jié)果。
本來覺得很簡單,做的時(shí)候才知道有點(diǎn)難。要是感興趣, 你也可以試試。
package test.io;
//在網(wǎng)上找的,聽說還是老字竹原創(chuàng)。代碼簡潔,但是我費(fèi)了好大的功副消化
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(" ", "$"));
}
}
//**************************************
//經(jīng)過消化之后我修改的??纱蛴∥募Y(jié)構(gòu)
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();
}
}
//*****************************************
//然后我還是覺得理解起來不方便, 過幾天說不定就忘記了,
//還是自己寫一個(gè), 雖然思想照抄, 但我覺得自己的理解起來很方便。
//帶注釋,
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 當(dāng)前正在被掃描的根文件
* @param childLeftStr 如果該文件有孩子,childLeftStr
* 表示孩子節(jié)點(diǎn)的左面應(yīng)該打印出來的結(jié)構(gòu)性信息
* 拿上面的例子來說,根結(jié)點(diǎn)test的孩子的左面的
* 結(jié)構(gòu)信息為"" 空,結(jié)點(diǎn)"目錄11"的孩子的結(jié)構(gòu)信息為"││",
* @param junction 結(jié)點(diǎn)圖標(biāo),如果是該結(jié)點(diǎn)是它父親的最后一個(gè)結(jié)點(diǎn),
* 則為"└",否則為"├".
*/
public void showTree(File root, String childLeftStr, String junction){
//打印結(jié)點(diǎn)的信息
System.out.println(junction + root.getName());
//如果有孩子, 而且孩子的數(shù)目不為0
if(!root.isFile()&&root.listFiles().length!=0){
File[] files = root.listFiles();
//構(gòu)造孩子結(jié)點(diǎn)
Tree[] children = new Tree[files.length];
for(int i=0; i<files.length; i++){
children[i] = new Tree(files[i]);
}
//打印孩子結(jié)點(diǎn)
for(int i=0; i<children.length-1; i++){
//對所有的孩子結(jié)點(diǎn),先打印出左邊的結(jié)構(gòu)信息,
System.out.print(childLeftStr);
//遞歸調(diào)用showTree, 注意參數(shù)有所變化,文件加的深度增加的時(shí)候
,它的孩子的結(jié)構(gòu)信息也會
//增加,如果不是最后一個(gè)孩子,則結(jié)構(gòu)信息需加上"│"。
showTree(children[i].root,childLeftStr+"│", "├");
}
//最后一個(gè)孩子需要特殊處理
//打印結(jié)構(gòu)信息
System.out.print(childLeftStr);
//如果是最后一個(gè)孩子,則結(jié)構(gòu)信息需加上" "。
//結(jié)點(diǎn)形狀也調(diào)整為"└"
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,"", "");
}
}
相關(guān)文章
Spring實(shí)戰(zhàn)之注入嵌套Bean操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之注入嵌套Bean操作,結(jié)合實(shí)例形式分析了嵌套Bean相關(guān)配置與使用操作技巧,需要的朋友可以參考下2019-11-11
spring cloud gateway全局過濾器實(shí)現(xiàn)向request header中放數(shù)據(jù)
這篇文章主要介紹了spring cloud gateway全局過濾器實(shí)現(xiàn)向request header中放數(shù)據(jù)的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Intellij Idea部署OpenCV 4.0.0環(huán)境
這篇文章主要為大家詳細(xì)介紹了Intellij Idea部署OpenCV 4.0.0環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Spring MVC---數(shù)據(jù)綁定和表單標(biāo)簽詳解
本篇文章主要介紹了Spring MVC---數(shù)據(jù)綁定和表單標(biāo)簽詳解,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
詳解Spring Data JPA動態(tài)條件查詢的寫法
本篇文章主要介紹了Spring Data JPA動態(tài)條件查詢的寫法 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
mybatis插入數(shù)據(jù)后如何返回新增數(shù)據(jù)的id值
當(dāng)往mysql數(shù)據(jù)庫插入一條數(shù)據(jù)時(shí),有時(shí)候需要知道剛插入的信息,下面這篇文章主要給大家介紹了關(guān)于mybatis插入數(shù)據(jù)后如何返回新增數(shù)據(jù)id值的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06

