0% found this document useful (0 votes)
9 views23 pages

CH 5.2 - Recursion

The document provides an overview of recursion, explaining its definition, types (direct and indirect), and components (base case and general case). It discusses how to work with recursion, including outlining algorithms and ensuring termination. Additionally, it includes examples of recursive functions, such as calculating factorials and Fibonacci numbers, and highlights the use of recursion in linked list processing.

Uploaded by

shaimaaabudayyeh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views23 pages

CH 5.2 - Recursion

The document provides an overview of recursion, explaining its definition, types (direct and indirect), and components (base case and general case). It discusses how to work with recursion, including outlining algorithms and ensuring termination. Additionally, it includes examples of recursive functions, such as calculating factorials and Fibonacci numbers, and highlights the use of recursion in linked list processing.

Uploaded by

shaimaaabudayyeh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Faculty of Information Technology - Computer Science Department 1

Data Structures

Faculty of Information Technology - Computer Science Department 2


Chapter 5
Recursion

Faculty of Information Technology - Computer Science Department 3


Recursion Specifications
 Recursion is a method where the solution to a problem depends on solutions to
smaller instances of the same problem.

Faculty of Information Technology - Computer Science Department 4


Recursion Specifications
 A recursive procedure or function calls itself directly or indirectly.
 Direct Recursion: a function calls itself.
void FunA()
{
FunA();
}
 Indirect Recursion
 function A calls function B, and function B calls function A.
void FunA() void FunB()
{ {
FunB(); FunA();
} }
 Or, function A calls function B, which calls function C, which then calls function
A
void FunA() void FunB() void FunC()
{ { {
FunB(); FunC(); FunA();
} } } 5
Faculty of Information Technology - Computer Science Department
Recursion Specifications

 A recursive process consists of two parts:

 A base case: the part that is processed without recursion (termination case).

 A general case: the part that is reduced to one, or more, smaller cases. Each time a
recursive call is made, it comes closer to the base case.

Faculty of Information Technology - Computer Science Department 6


Recursion Specifications

Working with Recursion


 Find the general step

 Begin by asking yourself “How can this problem be divided into parts?”.

 Be sure to keep your answer simple but applicable in the general case.

 Find the stopping rule (base case)

 The stopping rule indicates that the problem is done.

Faculty of Information Technology - Computer Science Department 7


Working with Recursion

 Outline your algorithm

 Combine the stopping rule and the general step, using an if statement to select between
them.
 You should now be able to write the main program and a recursive function that will
describe how to subdivide the problem until the stopping rule applies.
 Check termination

 Verify that the recursion will always terminate. Start with a general situation and check
that, in a finite number of steps, the stopping rule will be satisfied and the recursion will
terminate.
Faculty of Information Technology - Computer Science Department 8
How Function Calls Work

 When a function is called, it must have a storage area which is used to save all needed
data for this call as calling parameters, local variables, return value, …etc.
 When the function returns, the storage area is released.
 In the Figure below, we have:
 The function main(…) calls the function r(…).
 The function r(…) calls the function s(…), which calls the function t(…)

Faculty of Information Technology - Computer Science Department 9


How Function Calls Work

Faculty of Information Technology - Computer Science Department 10


How Recursive Function Calls Work

 Each time a recursive function is called, a new copy of the function runs, with new
instances of parameters and local variables being created.
 As each copy finishes executing, it returns to the copy of the function that called it.
 When the initial copy finishes executing, it returns to the part of the program that
made the initial call to the function.

Faculty of Information Technology - Computer Science Department 11


How Recursive Function Calls Work

Example 1 Output

Faculty of Information Technology - Computer Science Department 12


How Recursive Function Calls Work

Faculty of Information Technology - Computer Science Department 13


How Recursive Function Calls Work
Example 2 Order of function call ()
RecFun(5)
print5
RecFun(4)
print 4
RecFun(3)
print 3
RecFun(2)
print 2
RecFun(1)
print 1
RecFun(0)
print Stop!

Output
Try to modify this section to become: 5
RecFun(n-1); 4
System.out.println(n + " "); 3
what will the output be?? 2
1
Stop!
Faculty of Information Technology - Computer Science Department 14
Solving Recursively Defined Problems
 The natural definition of some problems leads to a recursive solution.
Example 1: Factorial Problem
 The factorial of a nonnegative integer n is the product of all positive integers less or
equal to n.
 The factorial of n is denoted by n!
 The factorial of 0 is 1.

0!=1
n ! = n × (n-1) × … × 2 × 1 if n > 0
 Factorial of n can be expressed in terms of the factorial of n-1

0!=1
n ! = n x (n-1) ! Faculty of Information Technology - Computer Science Department 15
Solving Recursively Defined Problems
• n ! = n × (n-1) × … × 2 × 1 if n > 0
5!=5 × 4 × 3 × 2 × 1=120

• n ! = n x (n-1) !
– 5!=5 × 4!
– 5!=5 ×4 × 3!
– 5!=5 ×4 × 3 ×2!
– 5!=5 ×4 × 3 ×2 ×1!
– 5!=5 ×4 × 3 ×2 ×1 ×0!
– 5!=5 ×4 × 3 ×2 ×1 ×1

Faculty of Information Technology - Computer Science Department 16


Solving Recursively Defined Problems

Faculty of Information Technology - Computer Science Department 17


Solving Recursively Defined Problems

Example 2: Fibonacci Numbers


fib(0), fib(1), fib(2), fib(3), fib(4), fib(5), …
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
 After the initial 0 and 1, each term is the sum of the two preceding terms.
 Recursive calculation of the nth Fibonacci number:
fib(n) = fib(n – 1) + fib(n – 2);
 Base cases: n = = 0, n = = 1

Faculty of Information Technology - Computer Science Department 18


Recursive Fibonacci Function

Faculty of Information Technology - Computer Science Department 19


Recursive Linked-List Processing

 Recursion is usually used with linked list processing to iterate (traverse) the linked
list backwards.
 For example, if we want to reverse print the content of a linked list we can achieve
that easily using recursion.

Faculty of Information Technology - Computer Science Department 20


Recursive Linked-List Processing

 To run this function, you need to define


public function:

 In the main function, trivially, public will be


called which will call the private one with
the head node as parameter.

Faculty of Information Technology - Computer Science Department 21


Repetition

Faculty of Information Technology - Computer Science Department 22


Recursion Specifications
Iterative

Recursion

Faculty of Information Technology - Computer Science Department 23

You might also like