Professional Documents
Culture Documents
RECURSIONS1F18BSCS0019
RECURSIONS1F18BSCS0019
RECURSIONS1F18BSCS0019
Terminating:
A recursive termination is a condition that, when met, will cause
the recursive function to stop calling itself. Because of
the termination condition, countDown(1) does not call
countDown(0) -- instead, the “if statement” does not execute, so
it prints “pop 1” and then terminates.
Stack case:
Recursive functions use something called the call stack.When a
program calls a function, that function goes on top of the
call stack. This similar to a stack of books. You add things one
at a time. Then, when you are ready to take something off, you
always take off the top item.
Recursive Tree:
In graph theory, a recursive tree (i.e., unordered tree) is a non-
planar labeled rooted tree. A size-n recursive tree is labeled by
distinct integers 1, 2, ..., n, where the labels are strictly
increasing starting at the root labeled 1. Recursive trees are non-
planar, which means that the children of a particular node are
not ordered. E.g. the following two size-three recursive trees are
the same.
Implementation:
Recursive functions are functions that call themselves. There are
two main parts to recursive functions:
1. general (recursive) case--the case for which the solution is
expressed in terms of a smaller version of itself. In other
words, here, the problem space is made smaller and
smaller. (the smaller problem space is sent to the calling
function)
2. base case--the case for which the solution can be stated
nonrecursively. Here, a solid solution is found.
Let's take an example of recursion using the factorial for a
positive integer.
n factorial can be represented by:
n!=n * (n-1) * (n-2) * ... * 1
For instance, 4! can be written as
4*3*2*1
By regrouping this, you can get: 4 * (3 * 2 * 1). Note that 3!=3 *
2 * 1, and by substitution, you can represent 4! by
4 * (3!)
You can generalize this reasoning to form the following
recursive definition of factorial:
n!=n * (n-1)!
where 0! is 1. Using this, you can evaluate 4! with the following
sequence of computations:
4!=4 * (3!)
=4 * (3 * (2!))
=4 * (3 * (2 * (1!)))
=4 * (3 * (2 * (1 * (0!))))
=4 * (3 * (2 * (1 * (1))))
The first four steps in this computation are recursive, with n!
being evaluated in terms of (n - 1)!. The final step (0!=1) is not
recursive. This demonstates the two main parts of recursive
functions:
1 if n = 0 (base case)
n! = {
n * (n - 1)! if n > 0 (recursive step)
Notice that in the recursive step, you are dealing with a smaller
and smaller problem space by calculating (n-1)!
Notice that in the recursive step, you are dealing with a smaller
and smaller problem space by calculating (n-1)!
This factorial function can be represented by the following code:
long factorial (int n)
// Computes n! using recursion.
{
long result; // Result returned
if ( n == 0 )
result = 1;
else
result = n * factorial (n-1);
return result;
}
Let's look at the call factorial(4) . Because 4 is not equal to 0
(the base case), the factorial() function calls factorial(3). The
recursive calls continue until the base case is reached--that is,
until n equals 0.
factorial(4)
↓ RECURSIVE STEP
4 * factorial (3)
↓ RECURSIVE STEP
3 * factorial (2)
↓ RECURSIVE STEP
2 * factorial (1)
↓ RECURSIVE STEP
1 * factorial (0)
↓ BASE CASE
1
The calls to factorial() are evaluated in reverse order. The
evaluation process continues until the value 24 is returned by the
call factorial (4).
factorial(4)
↑ RESULT 24
4 * factorial (3)
↑ RESULT 6
3 * factorial (2)
↑ RESULT 2
2 * factorial (1)
↑ RESULT 1
1 * factorial (0)
↑ RESULT 1
1
The calls to factorial() are evaluated in reverse order.