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

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

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,"", "");
 }
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 40分钟在线观看免费 | 插得爽 | 欧美色图亚洲天堂 | 日日操天天爽 | 国产精品视频1区 | 精品欧美一区二区精品久久 | 国产 日韩 欧美视频二区 | 美女的让男人桶爽免费看 | 虎四免费入口 | 欧美一区二区三区久久久 | 国产精品调教 | 国模大胆一区二区三区 | 精品亚洲视频在线观看 | 男人把大ji巴放进男人免费视频 | 精品国产剧情在线观看 | 国产一级毛片国语版 | 污漫日本E同人 | 激情男人天堂 | 国产精品视频二区不卡 | 高清视频在线播放 | 日本五级床片全都免费播放 | 国产日韩欧美一区 | 男人天堂999 | 亚洲乱码一区二区三区国产精品 | 99精品国产成人一区二区在线 | 国内精品久久久久影院网站 | 爽爽窝窝午夜精品一区二区 | 紧身裙女教师miad711在线 | 国产成人精品日本亚洲网址 | 99毛片| 日本福利视频网站 | 特黄视频 | 91精品国产色综合久久不卡蜜 | 被调教的校花 | 大乳奶水bbw | 久久久久影视 | 亚洲精品国产在线观看 | 黄片毛片 | 5151hh四虎国产精品 | 和岳m的小说 | 男人叼女人的痛爽视频免费 |