http://en.wikipedia.org/wiki/Tree_traversal
Basically you got Preorder, Inorder, Postorder and Level-order. Each one with their own pro's n con's, but in the end, they do the same thing.
The wikipage got pseudo-code implementations for the recursive and the iterative versions of the algorithms. I won't bother re-doing them here, but if you need help to understand them, please post here, and I'll go through them step-by-step :P
[EDIT] Eeek, yikes, etc! :D Seems like I were just a tad too fast on my trigger finger there. The link only provides algorithms for traversing binary trees. However, the principle remains the same, except that instead of a "left" and a "right" child on a given node, you now got 0 to many children.
Say you have a tree with an arbitary number of childs on each node, you'd traverse it like this, using some sort of In/Out data structure (queues or stacks, basically). The one you choose for this will dictate in which order you search your tree. In this example Ill use a queue:
public void TraverseTree(Node rootNode)
{
Queue nodeQueue();
nodeQueue.push(rootNode); //The first node to examine is the rootNode
Node currentNode;
//Okay, here we go. We will continue till the queue is empty. The queue will
//be empty when we've seen all children of all children :)
while (!nodeQueue.IsEmpty())
{
currentNode = nodeQueue.Pop(); //Get the next node to examine
DoStuffToNode(currentNode); //Do whatever we want to the node (in your case
//do some FTP stuff to the node (aka. the file)
//Okay, we're done with this node. Now, let's add all the children of this node
//To the queue of nodes we want to examine
foreach(Node child in currentNode.children)
nodeQueue.push(child);
}
}
You can do this with an array if you want to, but it will take some hoax, highly likely to be ineffective and not really intuitive.
Let's assume you want to transfer C: to an FTP-site (for the sake of explanation)
Using a stack, you will traverse ALL of the children of your current node, before going to the grand-children. So, you'd first Create a folder called "C:", then "Program Files", then "Windows" - And then afterwards you'd go into "Program files" and create "Adobe", then "Microsoft" etc. etc..
Using a queue, you will traverse all ancestors of a child before going to the next child. We would then first create "Program files", then "Adobe", then "Microsoft" etc. etc. and afterwards create "Windows".
I really hope I'm making myself clear here :) It's a lot easier to explain with a single animation.
The basic algorithm is this:
- Go to the next node in our queue or stack
- Do stuff to current node
- Add all children of the current node to the queue or stack
- Go to 1
Oh, btw, I dont have any experience with MFC, but can't you use a std::queue<> instead of a CArray? :)