2

I'm a Computer Engineering student just now taking data structures. Today's lecture covered recursive functions as an easy way to solve factorial problems or sorting problems. My professor used the Towers of Hanoi example, and I really want to make sure I understand the order in which the recursive functions get called. I've tried writing out step by step how the functions execute but I'm not getting the right sequence.

I'll post my source code below, it is pretty simple. If anyone could help explain it to me, I would be really greatful. Thanks!

void hanoi(int N, char S, char D, char I){
    //Step1: Anchor Value or Base Case
    if(N==1){
        cout <<"Move " << N << " from " << S << " to " << D << endl;
    }
    else {
        //Step2: Make progress towards base case
        hanoi(N-1, S, I, D);
        cout << "Move " << N << " from " << S << " to " << D << endl;
        hanoi (N-1, I, D, S);
    }
}

int main(){
    int N; //Number of disks
    cout << "Enter number of discs: " << endl;
    cin >> N;
    char S = 'S'; //Source
    char D = 'D'; //Destination
    char I = 'I'; //Intermediary

    hanoi(N, S, D, I);

    system("pause");
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • What don't you understand in particular? – πάντα ῥεῖ Sep 04 '14 at 18:29
  • I'm trying to logically understand how the recursion function flows. I'm using N=3 as my input and trying to trace every time the hanoi function executes. – Colin Butler Sep 04 '14 at 18:41
  • [Add information to clarify into your question please.](http://stackoverflow.com/posts/25672631/edit) – πάντα ῥεῖ Sep 04 '14 at 18:49
  • Imagine a tree. the root is `N=3`, it has two branches, for `N=2`, each of which has two branches for `N=1`. Now the calls are depth-first searches of the tree starting from the root. So `N=3` will be called, from it `N=2` on, let's say, left, `N=1` left, here there are no further calls, because of the `if` statement, so the function returns, `N=` right gets called and returns, and `N=2` left has all it needs to return, so `N=2` right gets called. this one goes through the the two `N=1` left and right, and returns, and finally `N=3` returns. I wish I could draw it in the comment :-) – triple_r Sep 04 '14 at 18:50
  • 1
    I do not understand those people that down-vote beginner questions. This question is understandable and has an example. – Adrian Maire Sep 04 '14 at 18:56
  • TBH he could have figured out the order himself by adding a few extra std::cout statements. – MSalters Sep 05 '14 at 07:23

1 Answers1

2

Example of execution with 3 disks:

hanoi( 3, 'S', 'D', 'I')
    (else) 
    hanoi(2, 'S', 'I', 'D')
        (else) 
        hanoi(1, 'S', 'D', 'I')
            (cout) move 1 from 'S' to 'D'
        (cout) move 2 from 'S' to 'I'
        hanoi(1, 'D', 'I', 'S'
            (cout) move 1 from 'D' to 'I'
    (cout) move 3 from 'S' to 'D'
    hanoi(2, 'I', 'D', 'S')
        (else)
        hanoi(1, 'I', 'S', 'D')
            (cout) move 1 from 'I' to 'S'
        (cout) move 2 from 'I' to 'D'
        hanoi(1, 'S', 'D', 'I')
            (cout) move 1 from 'S' to 'D'

To understand the caption, consider that 'S' is left, 'D' is middle and 'I' is left. 1 is the smaller disk, 2 is the medium disk and 3 is the greater disk.

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85