Java two-fork tree sorting and traversing file display text Format file Tree _java

Source: Internet
Author: User
Tags addchild

java Two-fork tree sorting algorithm
The description of the sorted binary tree is also a recursive description, so the construction of the sorted binary tree is naturally recursive:
Sort the 3 characteristics of the binary tree:
1: The value of all the left children of the current node is less than the current node's value;
2: All the right children of the current node are greater than the current node's value;
3: The child node also satisfies two points

Package test.sort; public class Binarynode {private int value;//current value private binarynode lchild;//left child private Binarynod 
  E 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. 
* 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 Traversal file display text format of the file tree
in Java to write a code calendar tree, print out the structure, similar to the command in the CMD to enter the results. The
was simple enough to know that it was a little hard to do. If you are interested, you can also try.

Package Test.io; On the internet to find, heard or old bamboo original words.
Code concise, but I did a good job to digest 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). t
   Ostring (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 ("Start Menu");
  Root.addchild (F1);
  Folder F1_1 = new Folder ("program");
  F1.addchild (f1_1);
  Folder F1_1_1 = new folder ("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 ("", "$")); }//**************************************//After digestion I modified. 
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 ();
}//*****************************************//Then I still find it inconvenient to understand, after a few days may forget,//or write their own, although the idea of copying, but I feel that their understanding is very convenient.
With annotations, import java.io.*;
 public class Tree {File root = null;
 Public tree (File f) {this.root = f; /** test├1│├ Directory 1.txt│├ directory 11││├111.txt││└112.txt│└12└test.pdf//** * @param root file that is currently being scanned * @pa Ram CHILDLEFTSTR If the file has children, Childleftstr * Indicates the structural information that should be printed on the left side of the child's node * Take the example above, the child's left of the root node test is "empty, node" directory 11 "of the child's Structure information is "││", * @param junction node icon, if the node is the last node of its father, * thenFor "└", otherwise "├". */public void Showtree (File root, String childleftstr, String junction) {//Print node information System.out.println (junction + R
  Oot.getname ());
   If there are children, and the number of children is not 0 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++) {//To all child nodes, first print out the left structure information, System.out.print (CHILDLEFTSTR)
    ;
    Recursive call Showtree, note that the parameter changes, the file added depth of time, its children's structure information will also//increase, if not the last child, the structure of information to be added "│."
   Showtree (children[i].root,childleftstr+ "│", "├");
   //Last child needs special handling//print structure information System.out.print (CHILDLEFTSTR);
   If this is the last child, the structure information should be added "".
  The knot shape is also 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, "", ""); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.