ディレクトリを検索し、そのディレクトリとサブディレクトリ内のすべてのテキスト ファイルを検索するアルゴリズムがあります。親ディレクトリにサブディレクトリとサブサブディレクトリがいくつあるかわからないと仮定します。複雑さを計算するにはどうすればよいですか?
これは私が使用しているコードです
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;
}
ありがとう