I'm writing an application that makes use of a tree structure, so of course I have some recursive methods that will iterate down every node of the tree and do something. The problem is sometimes these take a while, and I'd rather show a progress bar of some sort to the user rather then the program stop responding for a period of time.
If I'm iterating through a flat list I know how many items are in the list to begin with so it's easy to keep track of what number the loop is at and update a progress bar accordingly.
But with a recursive method iterating a tree structure I don't necessarily know how many nodes the tree has at the beginning. Should I first recursively read through the tree and just count all the nodes before running the actual recursive method that does whatever I want to do? Or maybe just keep track of a running total as nodes are added or removed from the tree? Is there a better option?