Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TreePractical
- {
- class Program
- {
- /*private static void TraverseDir(DirectoryInfo dir, string spaces)
- {
- Console.WriteLine(spaces + dir.FullName);
- DirectoryInfo[] children = dir.GetDirectories();
- foreach (DirectoryInfo child in children)
- {
- TraverseDir(child, spaces + " ");
- }
- }
- public static void TraverseDir(string directoryPath)
- {
- TraverseDir(new DirectoryInfo(directoryPath), string.Empty);
- }
- */
- public static void PrintBFS(string dirName)
- {
- DirectoryInfo dir = new DirectoryInfo(dirName);
- Queue<DirectoryInfo> visited = new Queue<DirectoryInfo>();
- visited.Enqueue(dir);
- while (visited.Count != 0)
- {
- DirectoryInfo current = visited.Dequeue();
- Console.WriteLine(current.FullName);
- FileInfo[] files = current.GetFiles();
- foreach (var item in files)
- {
- Console.WriteLine(item.FullName);
- }
- DirectoryInfo[] children = current.GetDirectories();
- foreach (DirectoryInfo child in children)
- {
- visited.Enqueue(child);
- }
- }
- }
- public static void PrintDFS(string dirName)
- {
- DirectoryInfo dir = new DirectoryInfo(dirName);
- Stack<DirectoryInfo> visited = new Stack<DirectoryInfo>();
- visited.Push(dir);
- while (visited.Count != 0)
- {
- DirectoryInfo current = visited.Pop();
- Console.WriteLine(current.FullName);
- FileInfo[] files = current.GetFiles();
- foreach (var item in files)
- {
- Console.WriteLine(item.FullName);
- }
- DirectoryInfo[] children = current.GetDirectories();
- foreach (DirectoryInfo child in children)
- {
- visited.Push(child);
- }
- }
- }
- public static void Main()
- {
- /* DirectoryInfo di = new DirectoryInfo("C:\\");
- Console.WriteLine(di.FullName);
- DirectoryInfo[] dirs = di.GetDirectories();
- foreach (var item in dirs)
- {
- Console.WriteLine(item.FullName);
- }
- FileInfo[] files = di.GetFiles();
- foreach (var item in files)
- {
- Console.WriteLine(item.FullName);
- }*/
- Console.WriteLine("DFS:");
- PrintDFS(@"C:\Users\vnachev\MyDir");
- Console.WriteLine("BFS:");
- PrintBFS(@"C:\Users\vnachev\MyDir");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement