(反向?)树枚举

时间:2011-11-30 12:17:39

标签: .net vb.net recursion

我正在使用此应用收集一些文件/文件夹使用数据。目标是让我的对象知道其文件和子文件夹的大小;在树的每一点。

我的问题是;在将目录结构放入我的对象后,我很困惑如何在那里得到小计。

我可以在创建树时实际添加总数吗?或者我必须反过来穿过树(这似乎特别令人困惑......)

我是一名没有计算机科学教育的新手;如果我知道谷歌的正确术语,我肯定会有答案。

我的课程有点混乱,他们在那里仍然有一些形成的想法......

Imports System.IO
Public Class Form1
    Public Class mrFURDirectory
        Public Property Name As String
        Public Property FileSize As Long = 0
        Public Property FileCount As Long = 0
        Public Property DirSize As Long = 0
        Public Property DirCount As Long = 0
        Public Property Parent As mrFURDirectory
        Public Property Children As New List(Of mrFURDirectory)
        Public Property PercentOfParent As Short = 0
        Public Property DirChecked As Boolean = False

        Public Sub New(ByVal Name As String)
            Me.Name = Name
        End Sub
    End Class

    Public Class mrBaseFURDirectory
        Inherits mrFURDirectory
        Property RootPath As String

        Sub New(ByVal Name As String, ByVal RootPath As String)
            MyBase.New(Name = Name)
            Me.RootPath = RootPath
        End Sub

    End Class

    Dim BaseDirectory As New mrBaseFURDirectory("A:\", "a:\") 'RAM disk full files/folders

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'setup IO
        Dim IOBaseDirectory As IO.DirectoryInfo
        IOBaseDirectory = New IO.DirectoryInfo(BaseDirectory.RootPath)

        'go get 'em tiger!
        GatherChildDirectoryStats(IOBaseDirectory, BaseDirectory)

        MsgBox("Done.")
    End Sub

    Public Sub GatherChildDirectoryStats(ByVal IODirectory As IO.DirectoryInfo, ByVal FURDirectory As mrFURDirectory)

        'get the file count and total size of files in this directory
        For Each aFile As IO.FileInfo In IODirectory.GetFiles
            FURDirectory.FileSize += aFile.Length
            FURDirectory.FileCount += 1
        Next

        For Each Directory As IO.DirectoryInfo In IODirectory.GetDirectories
            'DirCount likely redundant..
            FURDirectory.DirCount += 1

            'if we're in here, we need another child, make one
            Dim NewChildDir As mrFURDirectory
            'attach it to the parent
            FURDirectory.Children.Add(New mrFURDirectory(Directory.Name))
            'reference the child
            NewChildDir = FURDirectory.Children(FURDirectory.Children.Count - 1)
            'tell the child who the parent is
            NewChildDir.Parent = FURDirectory

            'recurse into sub again
            GatherChildDirectoryStats(Directory, NewChildDir)
        Next

    End Sub

3 个答案:

答案 0 :(得分:2)

在将每个节点中的所有文件大小相加后,您正在调用子节点来执行相同操作。在此调用返回后添加以下内容:

'recurse into sub again
GatherChildDirectoryStats(Directory, NewChildDir)

'add child sums to this node    
FURDirectory.DirSize += NewChildDir.FileSize + NewChildDir.DirSize

所以最后一个节点汇总了它的文件,返回并且总和被添加到n-1节点等。递归岩石!

答案 1 :(得分:1)

你几乎就在那里:目录的大小是文件大小的总和(你有)加上子目录大小的总和。

因此,在您完成添加子目录后,您可以获得它的总大小,并将其添加到当前目录的总大小。

答案 2 :(得分:1)

你可以随着时间去做。您需要做的就是处理子目录,然后将统计信息添加到当前目录中。

另一个选项是在mrFURDirectory对象上放置一个属性,然后获取所有相关统计信息。此属性将查看当前mrFURDirectory上的统计信息,然后使用相关的stat属性循环遍历所有子项并将其添加。当然,子项将以递归方式查找等。

假设这是一个静态快照,你可以将这些结果存储在某些可空的空白中,这样一旦你完成了它就可以在第二次跳过计算。我怀疑当你谈到反向踩树时,这与你所想的一致,但这是一种更加明显和直观的方式。

在这种情况下,后一种方法与前一种方法大致相同,只是你对它更懒惰。第一种方法(在处理时添加一个子目录统计数据)将大致相同,除非你继续而不是在最后。

相关问题