VickTree
VickTree

Reputation: 909

Understanding c++ Code: Tower of Hanoi using Recursion

i am learning about recursion in c++ but have been stumped by the following c++ code used to solve the Tower Of Hanoi problem.

void Hanoi(int m, string start, string middle, string end){
    cout << "m is equal to: " << m << endl;
    if(m == 1){
        cout << "Move Disc " << " from " << start << "  to " << end << endl;
    }
    else{
        Hanoi(m-1,start,end,middle);
        cout << "Move disc " << m << " from " << start << " to " << end << endl;
        Hanoi(m-1,middle,start,end);
    }
}
int main(){
    int discs = 3;
    Hanoi(discs, "start","middle","end");

} 

the output of the code is as follows:

m is equal to: 3
m is equal to: 2
m is equal to: 1
Move Disc  from start  to end
Move disc 2 from start to middle
m is equal to: 1
Move Disc  from end  to middle
Move disc 3 from start to end
m is equal to: 2
m is equal to: 1
Move Disc  from middle  to start
Move disc 2 from middle to end
m is equal to: 1
Move Disc  from start  to end

My general problem is i don't understand how the recursion is working. why doe m go to 1 before it executes the "if" statement? how does m go back to 2?

Upvotes: 2

Views: 6044

Answers (4)

jjj
jjj

Reputation: 575

Your goal is to move all disks to another place.

  • To solve the puzzle you need to move the tower from position 1 to position 2.

  • In order to do that you need to move the largest disk from position 1 to position 2.

  • To do that position 2 needs to be empty and all other disks needs to be at position 3 (as a tower of size m-1).

  • So you need to solve the puzzle for size m-1 (move tower of size m-1 from position 1 to 3). This the the first recursive call.

  • Now you can move the largest disk from position 1 to position 2. This the the output between the recursive function calls.

  • Now you need to move the tower of size m-1 from position 3 to position 2.

  • So you need to solve the puzzle for size m-1 again (move tower of size m-1 from position 3 to 2). This is the 2nd recursive function call.

The only problem size you can solve without this recursion is to move tower of size 1 to another place, because there can't be any disk on top and you can move in on top of any other disk. This is the reason that m has to be 1 before you can execute the if statement.

Upvotes: -2

code_fodder
code_fodder

Reputation: 16321

If you print it as a tree you get somthing like this:

main
  |--> hanoi(3, ...)
  |      |
  |      |--> hanoi(2, ...)
  |      |     |
  |      |     |--> hanoi(1, ...)
  |      |     |--> hanoi(1, ...)
  |      |<----|
  |      |--> hanoi(2, ...)
  |      |     |
  |      |     |--> hanoi(1, ...)
  |      |     |--> hanoi(1, ...)
  |      |<----|
  |<-----|
  |

For each call to hanoi(m, ...) it will keep call hanoi(m - 1, ...) twice unless m == 1. In the first call it will call again call hanoi(m - 1, ...) ... until m is 1.

So going backwards when m is 2 it will call hanoi(1, ...) twice in a row:

   hanoi(2, ...)
      hanoi(1, ...)
      hanoi(1, ...)

When m is 3 it will call hanoi(2, ...) twice in a row, hence:

hanoi(3, ...)
   hanoi(2, ...)
      hanoi(1, ...)
      hanoi(1, ...)
   hanoi(2, ...)
      hanoi(1, ...)
      hanoi(1, ...)

Upvotes: 5

trieck
trieck

Reputation: 61

Let's start with the first part of the output: m is equal to: 3 m is equal to: 2 m is equal to: 1

The Hanoi function is first called like this: Hanoi(3).
Since m != 1 in this case, we will call Hanoi(m-1) again. This will produce the output above. We are now 3 levels deep in this function.

Since m == 1, we will now see this output: Move Disc from start to end.

Now, we exit the deepest function and pop back to level 2 of our function call stack. Now we output: Move disc 2 from start to middle.

Upvotes: 3

YourPalNurav
YourPalNurav

Reputation: 1310

This trace should help you, if it doesn't then you can always find more traces by googling "Tower of Hanoi recursion program trace"

Program trace

You can also find how the algorithm works at https://javabypatel.blogspot.com/2015/12/tower-of-hanoi.html

Upvotes: 1

Related Questions