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 Listchildren = 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