激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 鲁久久| jizzyouxxxx| 国产日韩在线视频 | 91精品播放| 国产亚洲精品久久久久久网站 | 亚洲婷婷日日综合婷婷噜噜噜 | 成年人免费黄色片 | 日日摸夜夜骑 | 日本成人午夜视频 | 欧美激情性色生活片在线观看 | 久久亚洲国产精品 | 日本不卡二区 | 九九热九九热 | 少妇色诱麻豆色哟哟 | 色爱99| 国产欧美日韩 | 黄污网站在线观看 | 欧美日韩亚洲国产精品 | 亚洲成人在线免费观看 | 亚洲国产精久久久久久久 | 国产精品无码久久久久 | 日韩黄色影视 | 成人爱情偷拍视频在线观看 | 特级黄aaaaaaaaa毛片 | 得得啪在线视频 | 成人综合免费视频 | 国产亚洲综合精品 | 日日爱影院 | 一级黄色性感片 | 精品国产一区二区三区四区在线 | 午夜视频在线观看免费视频 | 亚洲一区二区三区四区精品 | 国产91中文字幕 | 久久国产精品系列 | 色999久久久精品人人澡69 | 草莓视频在线导航 | 91九色视频| 美国一级毛片片aa久久综合 | 欧美激情精品久久久久久黑人 | av电影网在线观看 | 国产亚洲精品综合一区91 |