Application of binary tree's ordinal traversal and post-order traversal--output file and statistic directory size

Source: Internet
Author: User

First, introduce

Two basic typical applications of binary tree in this paper are:

1) Output all file names (can have subfolders) under a folder---with the first order traversal implementation

2) Count the size of a folder (the size of all the files in that folder--with a sequential traversal implementation

Second, the realization analysis

For issue 1), the process for outputting the file name is as follows:

If it is a folder, export the folder name first, then output all the files (including subfolders) under that folder, and then, if there are subfolders, go to the subfolder and output all the filenames under that subfolder. This is a typical first-order traversal process.

For question 2), the size of the statistics folder is as follows:

To know the size of a folder, you must first know the size of all the files under that folder, and if you have subfolders, you must first know the size of all the subfolders of the subfolder to know the subfolder size.

This is a typical post-processing traversal.

Third, the code implementation

Output file name:

1      Public voidlist (File f) {2List (f, 0);3     }4      Public voidList (File F,intdepth) {5 Printname (f, depth);6         if(F.isdirectory ()) {7file[] Files =f.listfiles ();8              for(File file:files) {9List (file, depth + 1);Ten             } One         } A     } -  -      Private voidPrintname (File F,intdepth) { theString name =f.getname (); -          for(inti = 0; I < depth; i++) -System.out.print (""); -         if(F.isdirectory ()) +System.out.println ("Dir:" +name); -         Else +System.out.println (F.getname () + ":" + f.length ()/1024 + "KB"); A}

The Statistics folder size program is as follows:

1     Private LongTotalSize (File f) {2         LongSize = 0;3         if(F.isfile ())4         {5Size =f.length ();6         }7         Else8         {9file[] Files =f.listfiles ();Ten              for(File file:files) { OneSize + =totalsize (file); A             } -         } -         returnsize; the}

Analysis: Return size of line 14th; The final return is the outermost recursion (size at the first level of recursion), and the lookup Max/min node method in this article ultimately returns the innermost recursive formation comparison.

In fact, because the size of the statistics folder A, you need to calculate the sub-folder size, the final size of each subfolder is the size of the folder. The recursive program, which is called from Folder A, is called recursively when a subfolder is encountered, so the outermost recursive value should be returned.

A false recursive version:

1     Private LongTotalSize (File F,Longsize) {2         if(F.isfile ())3         {4Size + =f.length ();5         }6         Else7         {8file[] Files =f.listfiles ();9              for(File file:files) {TenSize + =totalsize (file, size); One             } A         } -         returnsize; -}

The cause of the error is as follows:

The size of the files in the upper directory is repeatedly counted.

Set the statistics a directory size. The A directory contains a subdirectory B, which contains c,d,e three files. The B directory has f,g two files.

The structure of a directory is as follows (uppercase letters indicate directories, lowercase characters indicate files):

A{b{f,g},c,d,e}

The wrong recursive method, in the calculation of the size of the B directory, will be a directory of c,d,e three file size also counted!!!

The entire complete program is as follows:

1 ImportJava.io.File;2 3  Public classFileList {4      Public voidlist (File f) {5List (f, 0);6     }7      Public voidList (File F,intdepth) {8 Printname (f, depth);9         if(F.isdirectory ()) {Tenfile[] Files =f.listfiles (); One              for(File file:files) { AList (file, depth + 1); -             } -         } the     } -      -     Private LongTotalSize (File f) { -         LongSize = 0; +         if(F.isfile ()) -         { +Size =f.length (); A         } at         Else -         { -file[] Files =f.listfiles (); -              for(File file:files) { -Size + =totalsize (file); -             } in         } -         returnsize; to     } +      -     /**Recursive error the Private Long TotalSize (File F, long size) { * if (F.isfile ()) $         {Panax Notoginseng size + = F.length (); -         } the Else +         { A file[] files = f.listfiles (); the For (File file:files) { + size + = totalsize (file, size); -             } $         } $ return size; -     } -     */ the      Private voidPrintname (File F,intdepth) { -String name =f.getname ();Wuyi          for(inti = 0; I < depth; i++) theSystem.out.print (""); -         if(F.isdirectory ()) WuSystem.out.println ("Dir:" +name); -         Else AboutSystem.out.println (F.getname () + ":" + f.length ()/1024 + "KB"); $     } -       -       Public Static voidMain (string[] args) { -FileList flist =NewFileList (); AFile f =NewFile ("F:\\ dissertation"); + flist.list (f); the          -         LongSize =flist.totalsize (f); $System.out.println (size/1024/1024 + "MB")); the     } the}

Application of binary tree's ordinal traversal and post-order traversal--output file and statistic directory size

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.