国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看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:孩子節(jié)點也滿足以上兩點

?
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;
//在網(wǎng)上找的,聽說還是老字竹原創(chuàng)。代碼簡潔,但是我費了好大的功副消化
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)過消化之后我修改的。可打印文件結構
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
  *  表示孩子節(jié)點的左面應該打印出來的結構性信息
  *  拿上面的例子來說,根結點test的孩子的左面的
  *  結構信息為"" 空,結點"目錄11"的孩子的結構信息為"││",
  * @param junction 結點圖標,如果是該結點是它父親的最后一個結點,
  *  則為"└",否則為"├".
  */
 
 public void showTree(File root, String childLeftStr, String junction){
  //打印結點的信息
  System.out.println(junction + root.getName());
  //如果有孩子, 而且孩子的數(shù)目不為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);
    //遞歸調(diào)用showTree, 注意參數(shù)有所變化,文件加的深度增加的時候
,它的孩子的結構信息也會
    //增加,如果不是最后一個孩子,則結構信息需加上"│"。
    showTree(children[i].root,childLeftStr+"│", "├");
   }
   //最后一個孩子需要特殊處理
   //打印結構信息
   System.out.print(childLeftStr);
   //如果是最后一個孩子,則結構信息需加上" "。
   //結點形狀也調(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,"", "");
 }
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 中文字幕成人 | 97久久精品 | 日韩av一区二区在线观看 | 欧美日韩久久精品 | 欧美午夜精品久久久久免费视 | 精品在线播放 | 亚洲免费视频网站 | 自拍视频在线观看 | 久久久夜夜夜 | 日韩三级网 | 黄色av影视 | 福利视频二区 | 久久久久高清视频 | 自拍第一页| 久久99综合久久爱伊人 | 国产精品国产 | 久久这里只有精品8 | 国产毛片av | 成人午夜在线 | 国产精品久久久久永久免费观看 | 亚洲在线电影 | 夜夜操天天操 | 日本三级中国三级99人妇网站 | 91av免费| 久在线视频 | 中文久久| 一级片大片| 久久99国产精一区二区三区 | 精品在线播放 | 日韩av片无码一区二区不卡电影 | 亚洲一区二区美女 | 精品免费在线 | 亚洲男人天堂2018 | 午夜区| 国产欧美精品 | 毛片大片 | 久久人爽| 亚洲激情在线观看 | 日本视频在线 | 亚洲 欧美 另类 综合 偷拍 | 香蕉av影院 |