我有一个算法可以搜索一个目录并搜索该目录和任何子目录中的所有文本文件。假设我不知道父目录中有多少子目录和子目录。我如何计算复杂度?
这是我正在使用的代码
public List<string> GetFilesInDirectory(string directoryPath)
{
// Store results in the file results list.
List<string> files = new List<string>();
// Store a stack of our directories.
Stack<string> stack = new Stack<string>();
// Add initial directory.
stack.Push(Server.MapPath(directoryPath));
// Continue while there are directories to process
while (stack.Count > 0)
{
// Get top directory
string dir = stack.Pop();
try
{
// Add all files at this directory to the result List.
files.AddRange(Directory.GetFiles(dir, "*.txt"));
// Add all directories at this directory.
foreach (string dn in Directory.GetDirectories(dir))
{
stack.Push(dn);
}
}
catch(Exception ex)
{
}
}
return files;
}
谢谢