在递归方法中绘制调用堆栈

发布于 2024-08-28 19:10:46 字数 823 浏览 5 评论 0原文

我想绘制任何递归方法的调用堆栈,因此我创建了一个像这样的架构,

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
}}

应用该架构将看起来像这样,

替代文字

注意:

  1. 此模式可以通过在单独的 return 语句中进行递归调用来绘制具有 n 次递归调用的递归方法。
  2. returnValues列表,是一个保存所有返回值的列表,仅供查看问题之用。
  3. 绘制堆栈的意思是,简单地绘制一个简单的单元格“矩形”+绘制推送的字符串。

您对此有何看法?任何建议都非常受欢迎。

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,

alt text

Notes:

  1. This schema can draw recursive methods with n recursive call by making the recursive calls in a separate return statements.
  2. returnValues list, is a list which save all the return values, just for viewing issues.
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏剧牡丹亭 2024-09-04 19:10:46

我不确定我是否正确理解你的问题,但会尝试一下,如果这是不正确的,请告诉我。

我收集到的是,您需要某种方法来跟踪递归函数中的堆栈。实现此目的的一种方法是拥有一个 Stack 数据结构,以及一个绘制该数据结构的函数,您希望如何绘制它取决于您,现在可能只是将堆栈绘制为类似 [-- -] 其中 '-' 是递归深度。

下面是一个近似 C++ 的示例:

所以我们有:

Stack recursiveFunctionTrackingStack; //Stack of something, maybe just '-'

void DrawStack(const Stack& aStack);

和另一种类似的类型:

struct StackUpdater
{
    StackUpdater(){ recursiveFunctionTrackingStack.push('-'); }
    StackUpdater(const string& somevalue)
    { 
        recursiveFunctionTrackingStack.push(somevalue); 
    }
    ~StackUpdater(){ recursiveFunctionTrackingStack.pop(); }
}

所以“StackUpdater”在创建堆栈数据结构的对象时将其推送到堆栈数据结构上,并在其被破坏时将其弹出。

现在,在递归函数中,我们可以执行以下操作(使用您的代码片段):

recursiveMethod(){
  if(){ return }
  else{
    {
        StackUpdater su(pushedInValue); //Value pushed
        variable <- recursiveMethod();
        DrawStack(recursiveFunctionTrackingStack);
    } //Value popped on destruct.
    DrawStack(recursiveFunctionTrackingStack); 
   return variable
}}

也许您想要的是沿着这些路线的东西。如果没有,请澄清您的问题。

无论如何希望这会有所帮助。

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:

Stack recursiveFunctionTrackingStack; //Stack of something, maybe just '-'

void DrawStack(const Stack& aStack);

and another type something like:

struct StackUpdater
{
    StackUpdater(){ recursiveFunctionTrackingStack.push('-'); }
    StackUpdater(const string& somevalue)
    { 
        recursiveFunctionTrackingStack.push(somevalue); 
    }
    ~StackUpdater(){ recursiveFunctionTrackingStack.pop(); }
}

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):

recursiveMethod(){
  if(){ return }
  else{
    {
        StackUpdater su(pushedInValue); //Value pushed
        variable <- recursiveMethod();
        DrawStack(recursiveFunctionTrackingStack);
    } //Value popped on destruct.
    DrawStack(recursiveFunctionTrackingStack); 
   return variable
}}

Maybe what you want is something along those line. If not, then please clarify you question.

Hope this helps anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文