Java binary tree sort and traversal file display text format file tree
- 2020-04-01 04:25:10
- OfStack
Java binary tree sorting algorithm
The description of sort binary tree is also a recursive description, so the construction of sort binary tree is also recursive:
Three characteristics of sort binary tree:
1: the value of all the left children of the current node is less than the value of the current node;
2: the value of all right children of the current node is greater than the value of the current node;
3: the child node also satisfies the above two points
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());
}
}
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);
}
}
The Java traversal file displays a tree of files in text format
Write a code in Java to change the file tree, print out the structure, similar to the CMD input command tree results.
I thought it was easy, but when I did it, I realized it was hard. If you are interested, you can try it.
package test.io;
//Look for on the net, hear or old word bamboo original creation. The code was simple, but I had a lot of trouble digesting it
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(" Menu list ");
Folder f1 = new Folder(" The start menu ");
root.addChild(f1);
Folder f1_1 = new Folder(" The program ");
f1.addChild(f1_1);
Folder f1_1_1 = new Folder(" The attachment ");
f1_1.addChild(f1_1_1);
Folder f1_1_1_1 = new Folder(" entertainment ");
f1_1_1.addChild(f1_1_1_1);
Folder f1_1_1_2 = new Folder(" entertainment 2");
f1_1_1.addChild(f1_1_1_2);
Folder f1_2 = new Folder(" Auxiliary tool ");
f1.addChild(f1_2);
System.out.println(root.toString(" ", "$"));
}
}
//**************************************
//I modified it after digestion. Printable file structure
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();
}
}
//*****************************************
//And I still don't think it's convenient to understand, and in a few days I might forget,
//Or to write their own, although the idea copy, but I think their understanding is very convenient.
//Take notes,
import java.io.*;
public class Tree {
File root = null;
public Tree(File f){
this.root = f;
}
public void showTree(File root, String childLeftStr, String junction){
//Print node information
System.out.println(junction + root.getName());
//If there are children and the number of children is not zero
if(!root.isFile()&&root.listFiles().length!=0){
File[] files = root.listFiles();
//Construct child node
Tree[] children = new Tree[files.length];
for(int i=0; i<files.length; i++){
children[i] = new Tree(files[i]);
}
//Print child node
for(int i=0; i<children.length-1; i++){
//For all the child nodes, print out the structure information on the left,
System.out.print(childLeftStr);
//Recursively call showTree, notice that the parameters change and the depth of the file increases
So will the structural information of its children
//Add, if not the last child, then the structure of the information to add "information,".
showTree(children[i].root,childLeftStr+" │ ", " ├ ");
}
//The last child needs special treatment
//Print structure information
System.out.print(childLeftStr);
//If it is the last child, the structure information needs to be added "".
//Node shapes have been adjusted to " └ "
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,"", "");
}
}