Chapter Three Recursion
Chapter Three Recursion
3.1. Recursion
Many operations on data structures – especially those on nonlinear linked structures such as
trees and graphs- are easier to implement using the powerful technique of recursion. A recursive
function is one that calls itself. This powerful technique produces repetition without using loops
(e.g., while loops or for loops). Thus it can produce substantial results from very little code.
Recursion allows elegantly simple solutions to difficult problems. But it can also be misused,
producing inefficient code. Recursive code is usually produced from recursive algorithms.
A recursive function is one that calls itself. This causes automatic repetition, a virtual
loop. In fact most algorithms that use iteration can be recast, replacing each loop with a recursive
call. So recursion can be viewed as an alternative to iteration.
Recursive functions are typically more concise than their iterative alternatives. For
example consider the factorial function.
n !=
{¿ n1( n−1
, if n=0∨1
) ! , if n>1
n !=n ( n−1 ) !
The first 10 values of the factorial function are shown in Table shown on the right. The
first and second value, 0! And 1!, are defined by the upper half of the definition: 0! = 1 (for n = 0
or 1). All the rest of the values are defined by the lower half of the definition:
In order to use recursion for problem solving, one has to first understand to give solution
to a smaller but similar problem. This means a solution for (n-1)! Must be known to give a
solution for n!, a solution to (n-1-1)! must be known before answering (n-1)! and so on. While
one goes to the bottom recurring, he/she will eventually reach to the number (n-(n-1)! Which is
1!. This factorial has a known solution in the factorial definition above, which is 1. This value
will be returned to the calling function which has passed a little bigger problem which is (n-(n-
2)! Which is 2! = 2 * 1! = 2. This value again will be returned to the a little bigger problem
which is 3! and so on. Look at the following diagram done for 3!.
This problem can be viewed as climbing down a ladder and picking up the value (i.e. 1! =
1, in this case) and climb back up the ladder. The known value or in this case 0! = 1! = 1, is
called the base of the recursion. The other part which states n! = n(n-1)! is called the recursion.
1|Page
Chapter Three Data Structures and Algorithms
1. #include<iostream.h>
2. long factorial(int n){
3. if(n<2) return 1;
4. return n*factorial(n-1);
5. }
6. void main(){
7. for(int i = 0; i < 10; i ++)
8. cout<<"\nfactorial("<<i<< ") = " <<factorial(i);
9. }
Output
The “towers of Hanoi” is a puzzle that was popular near the end of the 19 th century. The
puzzle consists of a board with three vertical pegs and a progression of desks of increasing
diameter. Each disk has a hole in its center. The disks are stacked on one peg so that each disk
rests on a larger one. The puzzle is how to transfer all the disks from one peg to another by
means of a sequence of individual moves, subjected to the restriction that no disk may be placed
upon a smaller disk.
In the simplest, nontrivial case, with n = 3 disks, it is easy to see the solution to the puzzle
is the following sequence of seven moves.
1. A B 3. B C
2. A C
2|Page
Chapter Three Data Structures and Algorithms
4. A B 6. C A
5. C A 7. A B
A recursive solution for n disks can be formulated by generalizing the steps involved to solve
a big solution. This means in order to solve a 4 disk Tower of Hanoi problem, we must first
move the three disks from A to C then, move disk 4 from A to B then move the three disks from
C to B. in a more generalized form, to move n disks from A to B, move all but the last disk from
A to C; then move the last disk from A to B; then move all the other disks from C to B. A
recursive test run to solve tower of Hanoi of n disks outlines as follows.
1. #include<iostream.h>
2. void TowerOfHanoi(int n, char a, char b, char c){
3. if(n == 1) cout<<"\n"<<a<<" -> "<<b; // base
4. else{
5. TowerOfHanoi(n-1, a, c, b);
6. cout<<"\n"<<a<<" -> "<< b;
7. TowerOfHanoi(n-1, c, b, a);
8. }
9. }
10. void main(){
11. int n;
12. cout<<"enter the number of hanois";
13. cin>>n;
14. TowerOfHanoi(n, 'A', 'B', 'C');
15. }
The output of the above program varies with the input n, although it is similar with the solution
given for n = 3, indicating the moves of the top disks from one peg to another.
Fibonacci numbers are useful in quite a few diverse areas of computing. They were
discovered by Leonardo Pisano (1170 – 1250),. He is best remembered for having written Liber
Abaci, a textbook on arithmetic which introduced the Hindu-Arabic numerals to Europe. One of
the exercises in this book was the famous rabbit problem.
A certain man put a pair of rabbits in a place surrounded on all sides by a wall. How
many pairs of rabbits can be produced from the pair in a year if it is supposed that
ever4y month each pair begets a new pair which from the second month on becomes
productive
3|Page
Chapter Three Data Structures and Algorithms
because the number F n of pairs of rabbits after n months is precisely the number of F n−1of pairs that
were alive at the end of the previous month (none die) plus the number F n−2 that were alive at the end of
the month before that (the productive ones).
Now the equation in the problem was, how many pairs are produced on a year? The number of
pairs produced from the original pair would be the total number after 12 months or simply F 12.
1. #include<iostream.h>
2. int fib(int n){
3. if(n == 0) return 0;
4. if(n == 1) return 1;
5. else return fib(n-1) + fib(n-2);
4|Page
Chapter Three Data Structures and Algorithms
6. }
7. void main(){
8. for(int i = 0; i < 13; i ++)
9. cout<<"\nthe "<<i<<" --> "<<fib(i);
10. }
Let us look at the Fibonacci series example shown above. In order to find nth Fibonacci number
we must go through two recursive calls fn-1 and fn-2. The following calculation shows the complexity
analysis of a Fibonacci series recursion function
T(n) = 2T(n-1) + 1
= 2(2T(n-2) + 1) + 1
= 4(2T(n-3) + 1) + 1 + 2
:
:
= 2k T(n-k) + ∑ 2i
= 2k T(n-k) + 2k-1
= 2k T(n-k) + 2k - 1 using rn -1 / r -1
n-k = 0
n=k
T(n) = 2n T(n-n) + 2n - 1
= 2n T(0) + 2n - 1
= 0 + 2n - 1
= 2n - 1
5|Page