Beruflich Dokumente
Kultur Dokumente
What is Recursion?
Recursive data structures
Recursive data algorithms
What is Recursion
The programs we have been doing to date have been organised into functions
that call another function in a disciplined, hierarchical way.
A function is said to be recursive if it calls itself either directly or indirectly
through another function.
A recursive function knows when to stop calling itself once a base case is
reached.
e.g. //print numbers 1 to n backwards
int print(int n)
{
if ( n = = 0)
// this is the terminating base case
return 0;
else {
System.out.print(+n+ );
return print(n-1); // recursive call to itself again
}
}
Infinite Recursion
Be very Careful! We must make sure that recursion eventually
stops, otherwise it will run forever (well not forever until we
run out of memory)
e.g.
// example of a badly defined recursive function
int Bad_recursion(n)
{
int x = Bad_recursion(n-1); // Bad!!!
if (n == 1)
return 1;
else
return n*x;
}
print(2)
returns 0
print(1)
returns 0
print(0)
returns 0
Recursive Algorithms
Recursive Algorithm : A solution that uses recursion to solve the problem
As mentioned previously, methods are said to be recursive if they call
themselves either directly or indirectly.
Many problems can be solved and best described through recursive
algorithms (e.g. traversal of nodes in a binary tree)
Some problems are best suited for recursive solutions while others are not.
Recursion is a complex topic and recursive algorithms can get quite complex.
We are going to look at some simple problems that can be solved recursively.
Solving Factorials
Factorial Explanation :
- The product of the positive integers from 1 to n inclusive is called
n factorial, usually denoted by n!
n! = n*(n-1)*(n-2)3*2*1
Mathematical explanation :
n! =
1,
n * (n-1)!
if n = 0
if n > 0
Example :
6! = 6*5*4*3*2*1 = 720
Programming and Data Structures
Pseudo-Code
Actual Code
Begin
factorial(n)
result = 1; //init result i.e. n = 0 or 1
for i = 2 to n
// if n is > 1
result = result * i; //fact is n*(n-1)!
endfor
return result
endmethod
End
1,
n * (n-1)!
if n = 0
if n > 0
From this we can obtain both our base and recursive cases:
- Base Case :
if(n <= 1)
return 1;
- Recursive Case:
return n*factorial(n-1);
Now that we know both our base and recursive cases we can write our factorial method:
int factorial(int n)
{
if (n <=1) // the base case
return 1
else
return n * factorial(n-1);
}
10
Final result = 24
4!
4!
4*6 24 returned
4 * 3!
3 * 2!
4 * 3!
3*2 6 returned
3 * 2!
2 * 1 2 returned
2 * 1!
2 * 1!
1 returned
1
Programming and Data Structures
11
12
Iteration
13
fib(n) =
0
1,
fib(n-1) + fib(n-2),
if n = 0
if n = 1
if n > 1
14
Note: In order to produce a fibonacci series we would iteratively call this function to produce the
required Fibonacci number in the series
Programming and Data Structures
15
fib(3)
returns 1
returns 1
returns 0
16
A word of caution is in order about recursive programs like the one we use
here to generate Fibonacci numbers.
Each level of recursion in the fibonacci function has a doubling effect on the
number of calls.
Calculating the 20th Fibonacci number would require on the order of about a
million calls.
Calculating the 30th Fibonacci number would require around a billion calls.
17
if x < 1
t = 0;
if x = 1
t = 1;
if x > 1
t = fib(x-1) + fib(x-2)
// if fib number x is 0
// then return 0
// if fib number x is 1
// return 1
// if fib number x is greater than 1
// recursively calculate this number
knownF[x] = t
return knownF[x]
Note the index of our knownFib array represents each fib number x so that x can be used here as
an index into our knownFib array.
Programming and Data Structures
18
19