Java Reference
In-Depth Information
number of blocks contained in all the children (which may be directories that
must be evaluated recursively): books (41), courses (8), and .login (2). The
total number of blocks is then the total in all the children plus the blocks used
at the root (1), or 52. The size routine shown in Figure 18.8 implements this
strategy. If the current FileSystem object is not a directory, size merely returns
the number of blocks it uses. Otherwise, the number of blocks in the current
directory is added to the number of blocks (recursively) found in all the chil-
dren. To illustrate the difference between postorder traversal and preorder tra-
versal, in Figure 18.9 we show how the size of each directory (or file) is
produced by the algorithm. We get a classic postorder signature because the
total size of an entry is not computable until the information for its children
has been computed. As indicated previously, the running time is linear. We
have much more to say about tree traversals in Section 18.4.
1 int size( )
2 {
3 int totalSize = sizeOfThisFile( );
4
5 if( isDirectory( ) )
6 for each file c in this directory (for each child)
7 totalSize += c.size( );
8
9 return totalSize;
10 }
figure 18.8
A routine for
calculating the total
size of all files in a
directory
ch1 9
ch2 7
dsaa 17
ch1 4
ch2 6
ecp 11
ch1 3
ch2 8
ipps 12
books 41
syl 2
cop3223 3
syl 3
cop3530 4
courses 8
.login 2
mark 52
figure 18.9
A trace of the size
method
Search WWH ::




Custom Search