0% found this document useful (0 votes)
2 views

CSC2302_Lecture9

This lecture on recursion discusses its definition as a technique where a function calls itself during execution, providing an elegant alternative to loops for repetitive tasks. It highlights characteristics of problems suitable for recursive solutions, exemplifies recursion through the factorial function, and illustrates counting occurrences of a character in a string recursively. The lecture emphasizes the importance of base cases and recursive calls in implementing recursive methods.

Uploaded by

rami.qt05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CSC2302_Lecture9

This lecture on recursion discusses its definition as a technique where a function calls itself during execution, providing an elegant alternative to loops for repetitive tasks. It highlights characteristics of problems suitable for recursive solutions, exemplifies recursion through the factorial function, and illustrates counting occurrences of a character in a string recursively. The lecture emphasizes the importance of base cases and recursive calls in implementing recursive methods.

Uploaded by

rami.qt05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CSC 2302

Data Structures

Lecture 9: Recursion
Raifa Akkaoui
School of Science and Engineering, AUI
[email protected]

Spring 2024
What’s Recursion?

• One way to describe repetition within a computer program is the use of loops, such as C while-loop and
for-loop
• An entirely different way to achieve repetition; through a process called recursion

• Definition: a technique by which a function makes one or more calls to itself during execution, or
by which a data structure relies upon smaller instances of the very same type of structure in its
representation

• Examples in nature & art


• Fractal patterns are naturally recursive, such as leaves
• Russian Matryoshka dolls, each doll is either made of
solid wood, or is hollow and contains another
Matryoshka doll inside it

• N.B. recursion provides an elegant and powerful alternative for performing repetitive tasks

06/02/2024 Data Structures 2


The Nature of Recursion

• Problems that lend themselves to a recursive solution have the following characteristics:
• One or more simple cases of the problem have a straightforward, non-recursive solution
• The other cases can be redefined in terms of problems that are closer to the simple cases
• By applying this redefinition process (i.e., second characteristic) every time the recursive function is
called, eventually the problem is reduced entirely to simple cases, which are relatively easy to
solve

Size 1
Problem

Size 1 Size 1 Size 1 Size 1


Problem Problem Problem Problem

06/02/2024 Data Structures 3


Classic Example of Recursion

• Factorial function
• n! = 1*2*3*…*(n-1)*n
• if n == 0, n! = 1

• Iterative implementation using while-loop or for-loop

/* Function that returns factorial of n*/


int /* Function that returns factorial of n*/
factorial ( int n ) { int
int j = 1 ; factorial ( int n ) {
int i = 1 ; / ∗ initialization ∗ / int i , j = 1 ;
while ( i <= n / ∗ condition ∗ / ) { for ( i = 1 ; i <= n ; i ++)
j ∗= i ; j ∗= i ;
i ++; / ∗ increment ∗ / return j ;
} }
return j ;
}

06/02/2024 Data Structures 4


Classic Example of Recursion (Cont.)

• Factorial
1 𝑖𝑓 𝑛 = 0
• n! = ቊ
𝑛 ∗ 𝑛 − 1 ∗ 𝑛 − 2 … ∗ 3 ∗ 2 ∗ 1, 𝑖𝑓 𝑛 ≥ 1

• Recursive definition:
• There is a natural recursive definition for the factorial function
• Observe that, for instance 5! = 5 * (4 * 3 * 2 * 1) = 5 * 4!
• More generally, for a positive integer n, we can define n! to be n * (n−1)!
• This recursive definition can be formalized as:

1, 𝑖𝑓 𝑛 = 0
𝑓 𝑛 =ቊ
𝑛 ∗ 𝑓(𝑛 − 1), 𝑒𝑙𝑠𝑒

06/02/2024 Data Structures 5


Implementation of a Recursive Factorial

06/02/2024 Data Structures 6


Content of a Recursive Method

• Base Case(s)
• Values of the input variables for which we perform no recursive calls
• There should be at least one base case
• Every possible chain of recursive calls must eventually reach a base case
• e.g., n=0 is our base case in factorial

• Recursive calls
• Calls to the current method
• Each recursive call should be defined so that it progresses towards a base case

06/02/2024 Data Structures 7


Visualizing Recursion

Memory organization Activation


of a C program record

Recursion Trace
Stack frame of program while computing 4!

06/02/2024 Data Structures 8


Counting Occurrences of a Character in a String

• Suppose we want to count the occurrences of a letter in a given string


• e.g., who many ‘s’ are in Mississippi sassafras?

• We want to do this recursively!


• Hint: redefine the problem “counting ‘s’ in Mississippi sassafras” as “counting s in
ississippi sassafras and add one more if the first letter is an s” (base case a string with
no characters (i.e., \0))

06/02/2024 Data Structures 9


Counting Occurrences of a Character in a String

• Suppose we want to count the occurrences of a letter in a given string


• e.g., who many ‘s’ are in Mississippi sassafras?

• We want to do this recursively!


• Hint: redefine the problem “counting ‘s’ in Mississippi sassafras” as “counting s in
ississippi sassafras and add one more if the first letter is an s” (base case a string with
no characters (i.e., \0))

06/02/2024 Data Structures 10


Thank you!
Your attendance and engagement in this
lecture are more than appreciated : )

Source: L. kyle, Mastering algorithms with C

You might also like