Drawing the call stack in a recursive method
I want to draw a call stack for any recursive method, so I created a diagram like this,
recursiveMethod(){
//Break recursion condition
if(){
// Add value here to the return values' list- No drawing
return
}
else{
//Draw stack with the value which will be pushed to the stack here
variable <- recursiveMethod()
//Clear the drawing which represents the poped value from the stack here
return variable
}}
Applying the schema would look something like this:
Notes:
- This diagram can draw recursive methods with n recursive calls by making recursive calls in separate return statements.
- returnValues list is a list that stores all return values just for viewing issues.
- To draw a stack means simply draw a simple "rectangle" cell + draw the pressed row.
What do you think about it? any suggestions are highly appreciated.
a source to share
I'm not sure if I understood your question correctly, but I'll take a stab at it, let me know if it's wrong.
I understand that you want to keep track of your stack in a recursive function somehow. One way to do this is to have a stack data structure and a function that draws the data structure, how you want to draw it is up to you, now maybe just draw the stack something like [---]
with '-'
is recursive depth.
Here's a rough C ++ example:
So we have:
Stack recursiveFunctionTrackingStack; //Stack of something, maybe just '-'
void DrawStack(const Stack& aStack);
and another type:
struct StackUpdater
{
StackUpdater(){ recursiveFunctionTrackingStack.push('-'); }
StackUpdater(const string& somevalue)
{
recursiveFunctionTrackingStack.push(somevalue);
}
~StackUpdater(){ recursiveFunctionTrackingStack.pop(); }
}
so "StackUpdater" pushes something in the Stack data structure when the object is created, and pops it when it is destroyed.
Now, within the framework of a recursive function, we can do (using a code snippet):
recursiveMethod(){
if(){ return }
else{
{
StackUpdater su(pushedInValue); //Value pushed
variable <- recursiveMethod();
DrawStack(recursiveFunctionTrackingStack);
} //Value popped on destruct.
DrawStack(recursiveFunctionTrackingStack);
return variable
}}
Perhaps what you want is something like that. If not, please clarify the question.
Hope this helps anyway.
a source to share
