How tower of hanoi is an example for recursion?
Answers
Answered by
0
Suppose there are three towers.
You've got N discs to move from A to C.
So, first of all, you need to move the top N-1 discs to B and move N to C. Then you need to put the N-1 discs again to C, and then your goal is complete. So the rough algorithm looks like:
Say S, A, D denote the source, auxillary and destination towers respectively, then:
solve(N, A, B, C)
{
solve(N-1, A, C, B);
PUSH N FROM A TO C
solve(N-1, B, A, C);
}
[Not talking about the base case here]
So, you see. That's recursion. :D
You've got N discs to move from A to C.
So, first of all, you need to move the top N-1 discs to B and move N to C. Then you need to put the N-1 discs again to C, and then your goal is complete. So the rough algorithm looks like:
Say S, A, D denote the source, auxillary and destination towers respectively, then:
solve(N, A, B, C)
{
solve(N-1, A, C, B);
PUSH N FROM A TO C
solve(N-1, B, A, C);
}
[Not talking about the base case here]
So, you see. That's recursion. :D
Similar questions
Computer Science,
10 months ago
Math,
10 months ago
Environmental Sciences,
10 months ago
English,
1 year ago
English,
1 year ago
Science,
1 year ago