RECURSION IN DSA OR RECURSIVE FUNCTIONS, Slides of Data Structures and Algorithms

The above uploaded documents covers the following topic in details..these are some verified and important lectures for those studnts or teachers who want to have some knwldge about the following topic along with pratice questions..IT CONTAIN code also for beginners and easy explanation

Typology: Slides

2021/2022

Available from 08/18/2022

SamenKhan
SamenKhan 🇵🇰

231 documents

1 / 31

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Science Department
Data Structure & Algorithms
Lecture 8
Recursion
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download RECURSION IN DSA OR RECURSIVE FUNCTIONS and more Slides Data Structures and Algorithms in PDF only on Docsity!

Data Structure & Algorithms

Lecture 8

Recursion

Introduction

  • In C++, any function can call another function.
  • A function can even call itself.
  • When a function call itself, it is making a recursive call. - Recursive Call - A function call in which the function being called is the same as the one making the call.
  • Recursion is a powerful technique that can be used in

the place of iteration(looping).

- Recursion

Recursive Definition

- A definition in which something is defined in terms

of smaller versions of itself.

  • To do recursion we should know the followings - Base Case: - The case for which the solution can be stated non-recursively - The case for which the answer is explicitly known. - General Case: - The case for which the solution is expressed in smaller version of itself. Also known as recursive case.

Example (Recursive Power Function)

  • Now lets suppose that X=3 and N=
  • Now we can simplify the above equation as
  • So the base case in this equation is

int Power ( int x , int n ) { if ( n == 1 ) return x; //Base case else return x * Power (x, n-1); // recursive call }

Factorial- a simple recursion

Recursive function: when we write a function in a simpler form of itself

Example 2 (Recursive Factorial Function)

- Factorial definition

n! = n × n-1 × n-2 × n-3 × … × 3 × 2 × 1 0! = 1

- To calculate factorial of n - Base case - If n = 0, return 1 - Recursive step - Calculate the factorial of n- - Return n × (the factorial of n-1)

Evaluation of Factorial Example

To evaluate Factorial(3) evaluate 3 * Factorial(2) To evaluate Factorial(2) evaluate 2 * Factorial(1) To evaluate Factorial(1) evaluate 1 * Factorial(0) Factorial(0) is 1 Return 1 Evaluate 1 * 1 Return 1 Evaluate 2 * 1 Return 2 Evaluate 3 * 2 Return 6

Recursive Programming

The Runtime Stack during Recursion

  • To understand how recursion works at run time, we need to understand what happens when a function is called.
  • Whenever a function is called, a block of memory is allocated to it in a run-time structure called the stack.
  • This block of memory will contain
    • the function’s local variables,
    • local copies of the function’s call-by-value parameters,
    • pointers to its call-by-reference parameters , and
    • a return address, in other words where in the program the function was called from. When the function finishes, the program will continue to execute from that point.

Stack Frames

*** .This is how recursion is implemented ***

int main() { cout << factorial(4); return 0; }

int factorial(int n) { int result; if ((n == 1) || (n == 0)) return 1; else { result = n * factorial(n-1); return result; } {

General System Stack

stack

stack

main frame

Fact: return=

200 n=4,result=

General System Stack

stack

stack

main frame

Fact: return=

200 n=4,result=

Fact: return=

300 n=3,result=

General System Stack

stack

stack

main frame

Fact: return=

200 n=4,result=

Fact: return=

300 n=3,result=

Fact: return=

400 n=2,result=

Fact: return=

500 n=1,result=

()int main } ;cout << factorial(4) ;return 0 {

int factorial(int n) } ;int result if ((n == 1) || (n == 0)) ;return 1 } else ;result = n * factorial(n-1) ;return result