一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java的二叉樹排序以及遍歷文件展示文本格式的文件樹

Java的二叉樹排序以及遍歷文件展示文本格式的文件樹

2020-03-03 18:28sunxing007 JAVA教程

這篇文章主要介紹了Java的二叉樹排序以及遍歷文件展示文本格式的文件樹,是對二叉樹結構學習的兩個很好的實踐,需要的朋友可以參考下

Java二叉樹排序算法
排序二叉樹的描述也是一個遞歸的描述, 所以排序二叉樹的構造自然也用遞歸的:
排序二叉樹的3個特征:
1:當前node的所有左孩子的值都小于當前node的值;
2:當前node的所有右孩子的值都大于當前node的值;
3:孩子節點也滿足以上兩點

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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的結果。
本來覺得很簡單,做的時候才知道有點難。要是感興趣, 你也可以試試。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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,"", "");
 }
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 1919gogo女厕盗摄| 美女福利视频一区二区 | 日本高免费观看在线播放 | 美女班主任让我爽了一夜视频 | 精品日韩二区三区精品视频 | 欧美老肥妇bbbw | 黑人性xxx| 亚洲好骚综合 | 99久久综合久中文字幕 | 99看视频| 精品日韩欧美一区二区三区 | 国产99视频精品免费视频7 | h卡通第一页 | 白丝校花好湿好紧 | 国产精品青青青高清在线观看 | 国产一区二区三区欧美 | 啪啪免费入口网站 | 国产青草视频在线观看免费影院 | 亚洲欧美日韩另类在线 | 成人免费视频播放 | 特级毛片免费观看视频 | 黑人性xxx| 日本久久啪啪婷婷激情五月 | 久久精品国产视频澳门 | 国产精品一久久香蕉产线看 | 亚洲国产精品综合欧美 | 毛片大全高清免费 | 日韩精品免费一区二区三区 | www免费插插视频 | 日韩在线免费看 | 国产剧情麻豆刘玥视频 | 大好硬好深好爽想要视频 | 法国女佣系列在线播放 | 无限在线观看视频大全免费高清 | 色人阁导航 | 爽好舒服快想要免费看 | 久久AV国产麻豆HD真实 | 亚洲黄视频在线观看 | 国内免费高清视频在线观看 | 成人在线观看免费视频 | 高清在线观看mv的网址免费 |