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?
To trace this recursive call in a debugger, set break point on the if statement, and run your program. When the breakpoint is reached: Inspect the value of n , Look at the call stack window.
A classic example of recursion The classic example of recursive programming involves computing factorials. The factorial of a number is computed as that number times all of the numbers below it up to and including 1. For example, factorial(5) is the same as 5*4*3*2*1 , and factorial(3) is 3*2*1 .
A recursive procedure is an algorithm that handles a list of items, where each item can be itself a list by decomposing the process into the handling of the first item of the list and follow this by the handling of the remainder of the list. A recursive procedure implements a process of total induction.
A common approach to this problem is to show a progress bar that is changing, but not updating an actual percentage. For example, the little loading icon in Firefox is a circle-thing that is just spinning -- you know it's doing something, but you don't know how long it's going to take. Maybe just do that and add a message that "this might take a moment depending on how much data there is..."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With