简体   繁体   中英

How to write stack for a recursive function having two recursive functions?

I have been stuck on a problem having two recursive functions in it. I could not understand the mechanism of loop and stack behind it. This are the lines of code of my program.

#include<iostream>

using namespace std;

int test(int num)
{   
    if (num != 0)
    {
        num = num - 1;
        test(num);
        cout << num << " ";
        test(num);
    }
}

int main()
{
    test(3);
}

This is the output of the program

  0 1 0 2 0 1 0

Can someone explain me the output of this program using stack?

Unroll the recursion. The output in the rightmost column below matches what you are getting.

executed                  printed
--------                  -------
test(3)
    test(2)
        test(1)
            test(0)
            cout << 0        0
            test(0)
        cout << 1            1
        test(1)
            test(0)
            cout << 0        0
            test(0)
    cout << 2                2
    test(2)
        test(1)
            test(0)
            cout << 0        0
            test(0)
        cout << 1            1
        test(1)
            test(0)
            cout << 0        0
            test(0)

For brevity lets call this function f . What does this function do? Well, it prints something if its argument is positive and calls itself recursively twice, with the argument decreased by 1. If the argument is zero, the function immediately returns, so the recursion starting from positive arguments will be stopped there. For negative arguments we have an error.

Now, What does it do? It decreases its argument, then calls itself on it, prints it, calls itself again. We can draw a diagram like this:

n: [f(n-1) n-1 f(n-1)] 

which means that (neglecting the problem of the number of spaces) it prints whatever f(n-1) prints, then n-1 (and a space), then again f(n-1). The first conclusion: the printout will be symmetric about its central element. And it actually is. If you expand this formula a step further, you'll get this:

n-1: [f(n-2) n-2 f(n-2) n-1 f(n-2) n-2 f(n-2)]

So, in the central position there will always be n-1 . Its left-hand and right-hand "neighbour substrings" will be identical. n-2 will be seen in this sequence twice. It's not difficult to see that n-3 will be seen 4 times, etc., till 0 will be seen 2^n times. One can even see that 0 will occupy every second position.

How many numbers will we see? 1 + 2 +... + 2^n = 2^{n-1} -1. For n=2 this gives 2^3 - 1 = 8 - 1 = 7 . That's correct.

What may be the "meaning" of this sequence?

Look (n = 5):

0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 4 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0

Now, take the consequtive integers

1 2 3 4 5 6 7 8 9 10 ... 31

and count the number of times each of them is divisible by 2. Can you see? 1 is not divisible by 2, hence 0 in your sequence. 2 is divisible once, hence 1. 3 is not divisible by 2 - hence 0. 4 is divisible twice, hence 2 on the fourth position. Etc., etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM