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

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 
 

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 children = new ArrayList();
 public void addChild(Folder f) {
  children.add(f);
 }
 public List getChildren() {
  return children;
 }
 public void setChildren(List 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 
 

你可能感兴趣的:(Java的二叉树排序以及遍历文件展示文本格式的文件树)