views:

47

answers:

2

I'm looking for ideas for fast and effective way to walk through folders bottom-up using C#/.NET 3.5

for example:

-0
--1
---12
--2
---21
---22
---23

first walk though: 12,21,22,23 then: 1,2

Thanks

+1  A: 

Walk the tree the usual way, and add an object containing each node and the level of the node to an IEnumerable.

public class DirectoryNode
{
    public DirectoryInfo Dir { get; set; }
    public int Level { get; set; }
}

public IEnumerable<DirectoryNode> myNodes;

Then, to get your list, just call your resulting IEnumerable with the OrderByDescending linq extension method, like this:

var result = myNodes.OrderByDescending(node => node.Level);
Robert Harvey
I see what you meant now, thanks :)
Andrija
A: 

You need the depth of each node and you need to find the maximum depth of all nodes before starting to traverse the tree. So you need to traverse all nodes in a preorder, inorder, or postorder traversal, find their depths, and then traverse the whole tree again in reverse order of depth.

Justice