在递归方法中绘制调用堆栈
我想绘制任何递归方法的调用堆栈,因此我创建了一个像这样的架构,
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
}}
应用该架构将看起来像这样,
注意:
- 此模式可以通过在单独的 return 语句中进行递归调用来绘制具有 n 次递归调用的递归方法。
- returnValues列表,是一个保存所有返回值的列表,仅供查看问题之用。
- 绘制堆栈的意思是,简单地绘制一个简单的单元格“矩形”+绘制推送的字符串。
您对此有何看法?任何建议都非常受欢迎。
I want to draw the call stack for any recursive method, so I've created a schema 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 will look something like this,
Notes:
- This schema can draw recursive methods with n recursive call by making the recursive calls in a separate return statements.
- returnValues list, is a list which save all the return values, just for viewing issues.
- Draw stack means, simply Draw a simple cell "rectangle" + Drawing the pushed String.
What do you think of this? any suggestions are extremely welcomed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否正确理解你的问题,但会尝试一下,如果这是不正确的,请告诉我。
我收集到的是,您需要某种方法来跟踪递归函数中的堆栈。实现此目的的一种方法是拥有一个 Stack 数据结构,以及一个绘制该数据结构的函数,您希望如何绘制它取决于您,现在可能只是将堆栈绘制为类似
[-- -]
其中'-'
是递归深度。下面是一个近似 C++ 的示例:
所以我们有:
和另一种类似的类型:
所以“StackUpdater”在创建堆栈数据结构的对象时将其推送到堆栈数据结构上,并在其被破坏时将其弹出。
现在,在递归函数中,我们可以执行以下操作(使用您的代码片段):
也许您想要的是沿着这些路线的东西。如果没有,请澄清您的问题。
无论如何希望这会有所帮助。
I'm not sure if I understand your question correctly, but will take a stab at it, let me know if this is incorrect.
What I gather is that you want some way to keep track of you stack within a recursive function. One way you can do this is to have a Stack data structure, and a function that draws the data structure,how you wish to draw it is up to you, for now maybe just draw the stack as something like
[---]
with the'-'
being the recursive depth.Here is an approximate C++ like example:
So we have:
and another type something like:
so the 'StackUpdater' pushes something on the Stack data structure when an object of it is created, and pops it off when it is destructed.
Now within the recursive function we can do (using your code snippet):
Maybe what you want is something along those line. If not, then please clarify you question.
Hope this helps anyway.