Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

http://www.abushofa.net.

ly

Department of Electrical and Electronic Engineering

Data Structures
Data Structures EE432
Fall 2018 - 2019

Data Structures - EE432


Fall 2018- 2019
Lecture - 07

Dr. Abubaker M. F. Abushofa


Department of Electrical & Electronic
Engineering,
Faculty of Engineering
University of Tripoli, Tripoli, LIBYA
http://www.abushofa.net.ly

What is Recursion?

Data Structures - EE432


Fall 2018- 2019
Recursion like a set of Russian dolls.
EE432 Dr. Abubaker Abushofa 2/25
http://www.abushofa.net.ly

Data Structures - EE432


Fall 2018- 2019
EE432 Dr. Abubaker Abushofa 3/35
http://www.abushofa.net.ly

What is a Recursion?
• Recursive call: A method call in which the
method being called is the same as the one

Data Structures - EE432


making the call

Fall 2018- 2019


• Direct recursion: Recursion in which a method
directly calls itself
• Indirect recursion: Recursion in which a chain
of two or more method calls returns to the
method that originated the chain

EE432 Dr. Abubaker Abushofa 4/25


http://www.abushofa.net.ly

Recursion
• You must be careful when using recursion.
• Recursive solutions can be less efficient than

Data Structures - EE432


Fall 2018- 2019
iterative solutions.
• Still, many problems lend themselves to
simple, elegant, recursive solutions.

EE432 Dr. Abubaker Abushofa 5/25


http://www.abushofa.net.ly

Some Definitions
• Base case: The case for which the solution can
be stated non‐recursively

Data Structures - EE432


• General (recursive) case: The case for which

Fall 2018- 2019


the solution is expressed in terms of a smaller
version of itself
• Recursive algorithm: A solution that is
expressed in terms of
1. smaller instances of itself and
2. a base case

EE432 Dr. Abubaker Abushofa 6/25


http://www.abushofa.net.ly

Finding a Recursive Solution


• Each successive recursive call should bring you
closer to a situation in which the answer is

Data Structures - EE432


known.

Fall 2018- 2019


• A case for which the answer is known (and can
be expressed without recursion) is called a
base case.
• Each recursive algorithm must have at least
one base case, as well as the general
(recursive) case.

EE432 Dr. Abubaker Abushofa 7/25


http://www.abushofa.net.ly

General format for many recursive


functions
if (some condition for which answer is known)

// base case

Data Structures - EE432


Fall 2018- 2019
solution statement

else // general case

recursive function call

EE432 Dr. Abubaker Abushofa 8/25


http://www.abushofa.net.ly

Computing Factorial
• Recursive definition

Data Structures - EE432


A definition in which something

Fall 2018- 2019


is defined in terms of a smaller
version of itself

What is 3 factorial?

EE432 Dr. Abubaker Abushofa 9/25


http://www.abushofa.net.ly

Computing Factorial

Data Structures - EE432


Fall 2018- 2019
EE432 Dr. Abubaker Abushofa 10/25
http://www.abushofa.net.ly

Recursive Computation

Data Structures - EE432


Fall 2018- 2019
EE432 Dr. Abubaker Abushofa 11/25
http://www.abushofa.net.ly

Factorial Program

• The decomposition of Factorial (3) is as

Data Structures - EE432


follows.

Fall 2018- 2019


Factorial(3) = 3 x Factorial(2) Factorial(3) = 3 x 2 = 6

Factorial(2) = 2 x Factorial(1) Factorial(2) = 2 x 1 = 2

Factorial(1) = 1 x Factorial(0) Factorial(1) = 1 x 1 = 1

Factorial(0) = 1

EE432 Dr. Abubaker Abushofa 12/25


http://www.abushofa.net.ly

Factorial Program
• The function call Factorial(4) should have
value 24, because that is 4 * 3 * 2 * 1 .

Data Structures - EE432


Fall 2018- 2019
• For a situation in which the answer is known,
the value of 0! is 1.
• So our base case could be along the lines of
if ( number == 0 )
return 1;

EE432 Dr. Abubaker Abushofa 13/25


http://www.abushofa.net.ly

Factorial Program
• Now for the general case . . .
• The value of Factorial(n) can be written as n *

Data Structures - EE432


the product of the numbers from (n ‐ 1) to ,1

Fall 2018- 2019


that is,
n * (n ‐ 1) * . . . * 1
or, n * Factorial(n ‐ 1)
• And notice that the recursive call Factorial(n ‐
1) gets us “closer” to the base case of
Factorial(0).

EE432 Dr. Abubaker Abushofa 14/25


http://www.abushofa.net.ly

Recursive Factorial
int Factorial ( int number )
{

Data Structures - EE432


// base case

Fall 2018- 2019


if ( number == 0)
return 1 ;
else // general case
return number * Factorial (number – 1);
}

EE432 Dr. Abubaker Abushofa 15/25


http://www.abushofa.net.ly

Three‐Questions for Verifying


Recursive Functions
• Base‐Case Question: Is there a non‐recursive
way out of the function?

Data Structures - EE432


Fall 2018- 2019
• Smaller‐Caller Question: Does each recursive
function call involve a smaller case of the
original problem leading to the base case?
• General‐Case Question: Assuming each
recursive call works correctly, does the whole
function work correctly?

EE432 Dr. Abubaker Abushofa 16/25


http://www.abushofa.net.ly

Computing Exponentiation Recursively


• From mathematics, we know that
20 = 1 and 25 = 2 * 24

Data Structures - EE432


Fall 2018- 2019
• In general,
x0 = 1 and xn = x * xn‐1
for integer x, and integer n > 0.
• Here we are defining xn recursively, in terms
of xn‐1

EE432 Dr. Abubaker Abushofa 17/25


http://www.abushofa.net.ly

Computing Exponentiation Recursively


// Recursive definition of power function
int Power ( int x, int n )

Data Structures - EE432


{

Fall 2018- 2019


if ( n == 0 )
return 1; // base case
else // general case
return ( x * Power ( x , n-1 ) ) ;
}

EE432 Dr. Abubaker Abushofa 18/25


http://www.abushofa.net.ly

Computing Exponentiation Recursively


Example: 53

Data Structures - EE432


power(5,3) = 5 x Power(5,2) power(5,3) = 5 x 25 = 125

Fall 2018- 2019


power(5,2) = 5 x 5 = 25
power(5,2) = 5 x power(5,1)

power(5,1) = 5 x power(5,0) power(5,1) = 5 x 1 = 5

power(5,0) = 1
EE432 Dr. Abubaker Abushofa 19/25
http://www.abushofa.net.ly

Fibonacci Sequence
• Problem: Calculate Nth item in Fibonacci
sequence

Data Structures - EE432


0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Fall 2018- 2019


55
– What is the next number?
– What is the size of the problem?
– Which case do you know the answer to?
– Which case can you express as a smaller version of
the size?

EE432 Dr. Abubaker Abushofa 20/25


http://www.abushofa.net.ly

Fibonacci Program
int Fibonacci(int n)
{

Data Structures - EE432


if (n == 0 || n == 1)

Fall 2018- 2019


return n;
else
return Fibonacci(n-2) + Fibonacci(n-1);
}

EE432 Dr. Abubaker Abushofa 21/25


http://www.abushofa.net.ly

Why Use Recursion?


• Those examples could have been written
without recursion, using iteration instead. The

Data Structures - EE432


iterative solution uses a loop, and the recursive

Fall 2018- 2019


solution uses an if statement.
• However, for certain problems the recursive
solution is the most natural solution.
• Recursive solutions are easier to reason about.
• The Functional Programming paradigm adopts
recursion.
EE432 Dr. Abubaker Abushofa 22/25
http://www.abushofa.net.ly

Printing List in Reverse


struct NodeType
{
int info;

Data Structures - EE432


NodeType* next;

Fall 2018- 2019


}
class SortedType
{
public:
...
private:
NodeType* listData;
};
EE432 Dr. Abubaker Abushofa 23/25
http://www.abushofa.net.ly

RevPrint(listData)

Data Structures - EE432


Fall 2018- 2019
EE432 Dr. Abubaker Abushofa 24/25
http://www.abushofa.net.ly

Base Case and General Case


• Base case: list is empty
Do nothing

Data Structures - EE432


Fall 2018- 2019
• General case: list is non‐empty
Extract the first element;
Print rest of the list (may be empty);
Print the first element

EE432 Dr. Abubaker Abushofa 25/25


http://www.abushofa.net.ly

Printing in Reverse
void RevPrint ( NodeType* listPtr )
{

Data Structures - EE432


if ( listPtr != NULL ) { // general case

Fall 2018- 2019


RevPrint ( listPtr-> next ); //process the rest
cout << listPtr->info << endl; // print this element
}
// Base case : if the list is empty, do nothing
}

EE432 Dr. Abubaker Abushofa 26/25


Data Structures - EE432
Fall 2018- 2019
End of lecture
http://www.abushofa.net.ly

Any question?
Department of Electrical and Electronic Engineering
Data Structures EE432
Fall 2018 - 2019

You might also like