3

How to change this recursive loop with non recursive? I know this method is easy way but i'm interested in non recursive way of this solution.

using System; using System.IO; namespace NonRecursion { class NonRecursion { static void Main() { string createPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string getPath = createPath + @"\folder"; GetDirsPath(getPath); Console.ReadKey(); } static void GetDirsPath(string getPath) { string[] dirs = Directory.GetDirectories(getPath); for (int i = 0; i < dirs.Length; i++) { Console.WriteLine(dirs[i]); GetDirsPath(dirs[i]); } } } } 

Can i change only this function?

static void GetDirsPath(string getPath) { string[] dirs = Directory.GetDirectories(getPath); for (int i = 0; i < dirs.Length; i++) { Console.WriteLine(dirs[i]); GetDirsPath(dirs[i]); } } 
10
  • 2
    Hint: You'll want to use a Queue<> or a Stack<> in GetDirsPath Commented Nov 29, 2016 at 11:50
  • 5
    Is this an excercise or not? Because if this is not an excercise then you should look at the version of GetDirectories that walks for you the subfolder tree and returns everything with a single call Commented Nov 29, 2016 at 11:51
  • And you should choose queue and stack by the kind of traversal you need to do, whether it's breadth-first or depth-first. Commented Nov 29, 2016 at 11:52
  • can i make it without using stack or queue? Commented Nov 29, 2016 at 11:53
  • 1
    Possible duplicate of Can every recursion be converted into iteration? Commented Nov 29, 2016 at 11:58

2 Answers 2

5

How about this:

public static IEnumerable<string> AllFolders(string root) { var folders = new Stack<string>(); folders.Push(root); while (folders.Count > 0) { string folder = folders.Pop(); yield return folder; foreach (var item in Directory.EnumerateDirectories(folder)) folders.Push(item); } } 

Test code (console app):

static void Main() { foreach (var dir in AllFolders("<your root folder here>")) { Console.WriteLine(dir); } } 

Here's an alternative approach using a List<string>:

public static IEnumerable<string> AllFolders(string root) { var folders = new List<string> {root}; while (folders.Count > 0) { string folder = folders[folders.Count - 1]; folders.RemoveAt(folders.Count-1); yield return folder; folders.AddRange(Directory.EnumerateDirectories(folder)); } } 

These both work the same way:

They maintain a list (or stack) of directories that have not yet been output, starting with the root directory.

The algorithms remove the topmost (stack) or last (list) directory from the stack/list and output it. Then they add all the subdirectories of that directory to the list/stack and repeat, until the list/stack is empty.

In particular, note that the List<> version is in fact merely using a List<> as a Stack<>, so it's algorithmically identical.

If you just want to make minimal changes to the GetDirsPath() method:

static void GetDirsPath(string getPath) { var dirs = new List<string> { getPath }; while (dirs.Count > 0) { string dir = dirs[dirs.Count - 1]; dirs.RemoveAt(dirs.Count - 1); Console.WriteLine(dir); dirs.AddRange(Directory.EnumerateDirectories(dir)); } } 
Sign up to request clarification or add additional context in comments.

3 Comments

It works but I only want to change GetDirPath function.
"How about this" is similar to "try this" for me. I don't like "try this" answers, because I can try a lot, just for finding out that it does not work. Your answer differs very much from OPs answer, so it seems to me that you have not converted his recursive code into non-recursive, but re-written everything.
@Mr.Pro Added a version with minimal changes to GetDirsPath().
2

You can use GetDirectories method:

string getPath = createPath + @"\folder"; var allDirectories = Directory.GetDirectories(getPath, "*.*", System.IO.SearchOption.AllDirectories); 

To get iterate through them:

foreach (string dir in allDirectories) { Console.WriteLine(dir); } 

https://msdn.microsoft.com/en-us/library/bb513869.aspx

1 Comment

I know this method but I only want to change GetDirPath function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.