Java的二叉樹排序以及遍曆檔案展示文字格式設定的檔案樹_java

來源:互聯網
上載者:User

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

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的結果。
本來覺得很簡單,做的時候才知道有點難。要是感興趣, 你也可以試試。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.