SlideShare a Scribd company logo
Advanced Data Structure
Recursion
Presented by:-A.RAVINDRA KUMAR RAO
(B.TECH (C.E))
Madanapalle Institute Of Technology & Science
CONTENTS
 RECURSION
 RECURSIVE ALGORITHMAS
 RECURSIVE TYPES
 RECURSIVE FUNCTIONS
 RECURSIVE DATATYPES
 ORDER OF EXECUTION
 EXAMPLE
RECURSION
What is recursion?
It’s when a function calls itself.
how does this happen?
When we talk about recursion, we are really talking about creating a loop.
Let’s start by looking at a basic loop.
for(int i=0; i<10;i++)
{
cout<<“the number is:”<<i<< end1;
}
Output:
the number is :0
the number is:1
the number is:2
the number is:3
RECURSIVE ALGORITHM
Implementation and use of Recursive Algorithms:
 space for all local, automatic (not static) variables
 space for all formal parameters
 the return address
 any other system information about the function or the function that called it.
 The function must have a selection construct which caters for the base case
 The recursive call must deal with a simpler/smaller version of the data
 Recursion uses selection construct and achieves repetition through repeated function
calls.
 Recursion has a base case.
 Any problem that can be solved recursively can be solved iteratively.
RECURSIVE ALGORITHM
A recursive algorithm is an algorithm which calls itself with "smaller or simpler"
input values, and which obtains the result for the current input by applying simple
operations to the returned value for the smaller input.
Example 1: Algorithm for finding the k-th even natural number
Algorithm 1: Even(positive integer k)
Input: k , a positive integer
Output: k-th even natural number (the first even being 0)
if k = 1,
then return 0;
else
return Even(k-1) + 2 .
RECURSIVE TYPES
Single recursion:
Recursion that only contains a single self-reference is
known as single recursion.
Example: Factorial function.
Multiple recursion:
Recursion that contains multiple self-references is known as
multiple recursion.
Ex: Fibonacci sequence
Direct recursion:
In which a function calls itself.
Ex: ƒ calls ƒ i.e. direct recursion
Indirect recursion:
Indirect recursion occurs when a function is called not by
itself but by another function that it called (either directly or
indirectly).
Ex: ƒ calls g, which calls ƒ
RECURSIVE TYPES
Anonymous recursion:
Recursion is usually done by explicitly calling a function by name.
However, recursion can also be done via implicitly calling a function based on
the current context, which is particularly useful for anonymous function and
is known as anonymous recursion.
Structural Recursion:
Functions that consume structured data, typically decompose their
arguments into their immediate structural components and then process
those components. If one of the immediate components belongs to the same
class of data as the input, the function is recursive.
EX: Factorial.
Generative Recursion:
Many well-known recursive algorithms generate an entirely new
piece of data from the given data and recur on it.
Ex: GCD, Binary Search
RECURSIVE FUNCTION
RECURSION FUNCTION:
Recursion is often seen as an efficient method of programming since it
requires the least amount of code to perform the necessary functions. However,
recursion must be incorporated carefully, since it can lead to an infinite loop if no
condition is met that will terminate the function.
MATHMATICAL FUNCTION:
In mathematics we often define a function in terms of itself.
For example: The factorial function f(n)=n!, for n is an integer, is defined as follows;
ƒ(n)={1 n≤1
{ n ƒ(n-1) n>1
TAIL RECURSIVE: In a tail-recursive function, none of the recursive call do
additional work after the recursive call is complete (additional work includes
printing, etc), except to return the value of the recursive call.
return rec_func( x, y ) ; // no work after recursive call, just return the
value of call
MUTUALLY RECURSIVE: For example, function f() can call function g() and
function g() can call function f(). This is still considered recursion because a
function can eventually call itself. In this case, f() indirectly calls itself.
RECURSIVE FUNCTIONS
FACTORIAL FUNCTION:
int Fact(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * Fact(n - 1);
}
}
RECURSIVE FUNCTION
RECURSIVE DATA TYPES
Inductively defined data:
An inductively defined recursive data definition is one that
specifies how to construct instances of the data.
Ex: linked list.
Co-inductively defined data type:
A co-inductive data definition is one that specifies the operations
that may be performed on a piece of data.
A co-inductive definition of infinite streams of strings
Ex: A stream of strings is an object s such that head(s) is a string,
and tail(s) is a stream of strings.
ORDER OF EXECUTION
In the simple case of a function calling itself only once, instructions placed before the
recursive call are executed once per recursion before any of the instructions placed
after the recursive call. The latter are executed repeatedly after the maximum
recursion has been reached.
Function 1
void recursiveFunction(int num)
{
printf("%dn", num);
if (num < 4)
recursiveFunction(num + 1);
}
ORDER OF EXECUTION
Function 2 with swapped lines:
void recursiveFunction(int num) {
if (num < 4)
recursiveFunction(num + 1);
printf("%dn", num);
}
SYSTEM STACK OF RECURSION
OBJECT HEAP
UNUSED
MEMORY
CALL STACK
STATIC VARS
BYTECODES
OS / JVM
Method n
Activation on Record
. . . .
Method 3
Activation on Record
Method 2
Activation on Record
Method 1
Activation on Record
LOCAL VARS
PARAMETERS
RETURN ADDRESS
(PC Values)
PREVIOUS BASE
POINTER
RETURN VALUE
BASE POINTER
PROGRAM COUNTER
REAL TIME EXAMPLE
(Recursion)ads

More Related Content

PPT
3 recursion
PPTX
Recursion(Advanced data structure)
PPTX
Recursion and Sorting Algorithms
PPT
Recursion - Algorithms and Data Structures
PPT
Ch10 Recursion
PPT
2 b queues
PPTX
Recursion
PPT
2 a stacks
3 recursion
Recursion(Advanced data structure)
Recursion and Sorting Algorithms
Recursion - Algorithms and Data Structures
Ch10 Recursion
2 b queues
Recursion
2 a stacks

What's hot (20)

PPT
Recursion and looping
PPT
1 list datastructures
PDF
Iterations and Recursions
PPT
Data Structures- Part5 recursion
PPT
Chapter 7 ds
PPT
Chapter 4 ds
PPT
Data structures
PPT
Data Structures- Part2 analysis tools
PPTX
Searching Algorithms
PPT
Chapter 6 ds
PPTX
Data structures and algorithms
PPT
Cis435 week04
PPT
Data Structure and Algorithms
PPT
Data Structures- Part1 overview and review
PPT
Recursion
PDF
Module 01 Stack and Recursion
PDF
Iteration, induction, and recursion
PPT
C1320prespost
PPT
Database structure Structures Link list and trees and Recurison complete
PPTX
Introduction to datastructure and algorithm
Recursion and looping
1 list datastructures
Iterations and Recursions
Data Structures- Part5 recursion
Chapter 7 ds
Chapter 4 ds
Data structures
Data Structures- Part2 analysis tools
Searching Algorithms
Chapter 6 ds
Data structures and algorithms
Cis435 week04
Data Structure and Algorithms
Data Structures- Part1 overview and review
Recursion
Module 01 Stack and Recursion
Iteration, induction, and recursion
C1320prespost
Database structure Structures Link list and trees and Recurison complete
Introduction to datastructure and algorithm
Ad

Similar to (Recursion)ads (20)

PPT
Lec-6 Recursion of Data Structures & Algorithms
PPTX
Function & Recursion
PDF
Recursion.pdf
PPTX
Recursion is used in programming languages to use a procedure multiple times ...
PPT
Lecture9 recursion
PPTX
Unit-I Recursion.pptx
PPTX
Recursion in Data Structure
PPT
Data structures &amp;algorithms
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
PPTX
Functions.pptx
PDF
Recursion concepts by Divya
PPTX
Recursionaditya..................................pptx
PPT
C++ and Data Structure.ppt
PDF
[ITP - Lecture 14] Recursion
PPT
Recursion.ppt
PPT
3-Recursion.ppt
PDF
Data Structure - Lecture 2 - Recursion Stack Queue.pdf
PPTX
Recursion
PPTX
Lecture_7_StackAndRecursion (1).pptx
Lec-6 Recursion of Data Structures & Algorithms
Function & Recursion
Recursion.pdf
Recursion is used in programming languages to use a procedure multiple times ...
Lecture9 recursion
Unit-I Recursion.pptx
Recursion in Data Structure
Data structures &amp;algorithms
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
Functions.pptx
Recursion concepts by Divya
Recursionaditya..................................pptx
C++ and Data Structure.ppt
[ITP - Lecture 14] Recursion
Recursion.ppt
3-Recursion.ppt
Data Structure - Lecture 2 - Recursion Stack Queue.pdf
Recursion
Lecture_7_StackAndRecursion (1).pptx
Ad

Recently uploaded (20)

PPTX
Lecture Notes Electrical Wiring System Components
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Well-logging-methods_new................
PPTX
Geodesy 1.pptx...............................................
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
web development for engineering and engineering
PDF
composite construction of structures.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Lecture Notes Electrical Wiring System Components
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
OOP with Java - Java Introduction (Basics)
R24 SURVEYING LAB MANUAL for civil enggi
Foundation to blockchain - A guide to Blockchain Tech
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Sustainable Sites - Green Building Construction
Internet of Things (IOT) - A guide to understanding
Operating System & Kernel Study Guide-1 - converted.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Well-logging-methods_new................
Geodesy 1.pptx...............................................
CH1 Production IntroductoryConcepts.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
web development for engineering and engineering
composite construction of structures.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf

(Recursion)ads

  • 1. Advanced Data Structure Recursion Presented by:-A.RAVINDRA KUMAR RAO (B.TECH (C.E)) Madanapalle Institute Of Technology & Science
  • 2. CONTENTS  RECURSION  RECURSIVE ALGORITHMAS  RECURSIVE TYPES  RECURSIVE FUNCTIONS  RECURSIVE DATATYPES  ORDER OF EXECUTION  EXAMPLE
  • 3. RECURSION What is recursion? It’s when a function calls itself. how does this happen? When we talk about recursion, we are really talking about creating a loop. Let’s start by looking at a basic loop. for(int i=0; i<10;i++) { cout<<“the number is:”<<i<< end1; } Output: the number is :0 the number is:1 the number is:2 the number is:3
  • 4. RECURSIVE ALGORITHM Implementation and use of Recursive Algorithms:  space for all local, automatic (not static) variables  space for all formal parameters  the return address  any other system information about the function or the function that called it.  The function must have a selection construct which caters for the base case  The recursive call must deal with a simpler/smaller version of the data  Recursion uses selection construct and achieves repetition through repeated function calls.  Recursion has a base case.  Any problem that can be solved recursively can be solved iteratively.
  • 5. RECURSIVE ALGORITHM A recursive algorithm is an algorithm which calls itself with "smaller or simpler" input values, and which obtains the result for the current input by applying simple operations to the returned value for the smaller input. Example 1: Algorithm for finding the k-th even natural number Algorithm 1: Even(positive integer k) Input: k , a positive integer Output: k-th even natural number (the first even being 0) if k = 1, then return 0; else return Even(k-1) + 2 .
  • 6. RECURSIVE TYPES Single recursion: Recursion that only contains a single self-reference is known as single recursion. Example: Factorial function. Multiple recursion: Recursion that contains multiple self-references is known as multiple recursion. Ex: Fibonacci sequence Direct recursion: In which a function calls itself. Ex: ƒ calls ƒ i.e. direct recursion Indirect recursion: Indirect recursion occurs when a function is called not by itself but by another function that it called (either directly or indirectly). Ex: ƒ calls g, which calls ƒ
  • 7. RECURSIVE TYPES Anonymous recursion: Recursion is usually done by explicitly calling a function by name. However, recursion can also be done via implicitly calling a function based on the current context, which is particularly useful for anonymous function and is known as anonymous recursion. Structural Recursion: Functions that consume structured data, typically decompose their arguments into their immediate structural components and then process those components. If one of the immediate components belongs to the same class of data as the input, the function is recursive. EX: Factorial. Generative Recursion: Many well-known recursive algorithms generate an entirely new piece of data from the given data and recur on it. Ex: GCD, Binary Search
  • 8. RECURSIVE FUNCTION RECURSION FUNCTION: Recursion is often seen as an efficient method of programming since it requires the least amount of code to perform the necessary functions. However, recursion must be incorporated carefully, since it can lead to an infinite loop if no condition is met that will terminate the function. MATHMATICAL FUNCTION: In mathematics we often define a function in terms of itself. For example: The factorial function f(n)=n!, for n is an integer, is defined as follows; ƒ(n)={1 n≤1 { n ƒ(n-1) n>1
  • 9. TAIL RECURSIVE: In a tail-recursive function, none of the recursive call do additional work after the recursive call is complete (additional work includes printing, etc), except to return the value of the recursive call. return rec_func( x, y ) ; // no work after recursive call, just return the value of call MUTUALLY RECURSIVE: For example, function f() can call function g() and function g() can call function f(). This is still considered recursion because a function can eventually call itself. In this case, f() indirectly calls itself. RECURSIVE FUNCTIONS
  • 10. FACTORIAL FUNCTION: int Fact(int n) { if (n == 0) { return 1; } else { return n * Fact(n - 1); } } RECURSIVE FUNCTION
  • 11. RECURSIVE DATA TYPES Inductively defined data: An inductively defined recursive data definition is one that specifies how to construct instances of the data. Ex: linked list. Co-inductively defined data type: A co-inductive data definition is one that specifies the operations that may be performed on a piece of data. A co-inductive definition of infinite streams of strings Ex: A stream of strings is an object s such that head(s) is a string, and tail(s) is a stream of strings.
  • 12. ORDER OF EXECUTION In the simple case of a function calling itself only once, instructions placed before the recursive call are executed once per recursion before any of the instructions placed after the recursive call. The latter are executed repeatedly after the maximum recursion has been reached. Function 1 void recursiveFunction(int num) { printf("%dn", num); if (num < 4) recursiveFunction(num + 1); }
  • 13. ORDER OF EXECUTION Function 2 with swapped lines: void recursiveFunction(int num) { if (num < 4) recursiveFunction(num + 1); printf("%dn", num); }
  • 14. SYSTEM STACK OF RECURSION OBJECT HEAP UNUSED MEMORY CALL STACK STATIC VARS BYTECODES OS / JVM Method n Activation on Record . . . . Method 3 Activation on Record Method 2 Activation on Record Method 1 Activation on Record LOCAL VARS PARAMETERS RETURN ADDRESS (PC Values) PREVIOUS BASE POINTER RETURN VALUE BASE POINTER PROGRAM COUNTER