0% found this document useful (0 votes)
82 views14 pages

Recursion Notes

The document discusses recursion concepts including functions, return statements, recursion stacks, what is recursion, printing natural numbers recursively, factorial program recursively, insertion sort recursively, multiple recursion, Fibonacci series recursively, number of paths in a matrix recursively, and tree traversals recursively.

Uploaded by

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

Recursion Notes

The document discusses recursion concepts including functions, return statements, recursion stacks, what is recursion, printing natural numbers recursively, factorial program recursively, insertion sort recursively, multiple recursion, Fibonacci series recursively, number of paths in a matrix recursively, and tree traversals recursively.

Uploaded by

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

Recursion Notes

@rithik_codez

1) Must know terminology

FUNCTIONS -
In coding, a function is a named block of reusable code
that performs a specific task or a series of related tasks.
Functions are designed to modularize code and promote
code reusability, making programs more organized,
efficient, and easier to maintain.

RETURN STATEMENT-
The return statement is a construct used in programming
languages to specify the value that a function should
return after its execution completes. It allows a
function to produce a result or output that can be
utilized by other parts of the program.

RECURSION STACK-
A recursion stack, also known as a call stack or execution
stack, is a data structure used by programming
languages to manage the execution of recursive function
calls. It keeps track of the sequence of function calls
and their corresponding execution contexts.
Recursion Notes
@rithik_codez

Functions encapsulate a set of instructions or


algorithms that can be called or invoked from
different parts of a program whenever needed.
Instead of repeating the same code multiple times,
you can define a function and call it whenever that
code needs to be executed.
Functions typically have input parameters
(arguments) and may have a return value. The
parameters allow you to pass data into the function,
and the return value is the result or output of the
function's computation.
When a return statement is encountered in a
function, it immediately terminates the function's
execution and sends the specified value back to the
caller. The syntax for using the return statement is
as follows:

RECURSION STACK-
A recursion stack, also known as a call stack or execution
stack, is a data structure used by programming languages
to manage the execution of recursive function calls. It
keeps track of the sequence of function calls and their
corresponding execution contexts.
Recursion Notes
@rithik_codez

Functions encapsulate a set of instructions or


algorithms that can be called or invoked from different
parts of a program whenever needed. Instead of
repeating the same code multiple times, you can define a
function and call it whenever that code needs to be
executed.
Functions typically have input parameters (arguments)
and may have a return value. The parameters allow you
to pass data into the function, and the return value is
the result or output of the function's computation.
When a return statement is encountered in a
function, it immediately terminates the function's
execution and sends the specified value back to the
caller. The syntax for using the return statement is as
follows:
When a function is called recursively, each
subsequent recursive call is placed on top of the stack,
creating a stack frame. This stack frame contains the
local variables, parameters, and the return address for
that particular invocation of the function.
The recursion stack operates on the principle of Last-In-
First-Out (LIFO), meaning that the most recent function
call is at the top of the stack, and the earlier calls are
below it. Once a recursive
Recursion Notes
@rithik_codez
function call reaches its base case (a condition that
terminates the recursion), the corresponding stack frame
is removed from the stack, and the control returns to the
previous stack frame, continuing the execution.

2) WHAT IS RECURSION?
Recursion is a programming concept where a function
calls itself to solve a problem by breaking it down into
smaller, simpler instances of the same problem. It is a
way to solve complex tasks by reducing them to smaller,
more manageable subtasks.
3) Printing first 5 natural number using recursion
the code is well explained in my youtube tutorial please
feel free to refer that.
Now, what if we want to reverse the sequence from 1 2 3
4 5 to 5 4 3 2 1, We will change only the sequence of two
lines from the above code.
In simpler terms, recursion can be thought of as a
process of self-replication or self-referencing. When a
recursive function is called, it performs some computation
and then calls itself with a modified input or state. This
process continues until a base case is reached, which is a
condition that allows the recursion to stop and the
function calls to start returning values.
Recursion Notes
@rithik_codez

3) Printing first 5 natural number using recursion

the code is well explained in my youtube tutorial


please feel free to refer that.
Now, what if we want to reverse the sequence from
1 2 3 4 5 to 5 4 3 2 1, We will change only the
sequence of two lines from the above code.
Recursion Notes
@rithik_codez

4) FACTORIAL PROGRAM
The factorial of a non-negative integer n, denoted as
n!, is the product of all positive integers less
than or equal to n. For example, 5! (read as "5
factorial") is calculated as 5 x 4 x 3 x 2 x 1,
which equals 120.
Factorial can be defined recursively by the following
formula:
n! = n * (n-1)!
The program is as follows
Recursion Notes
@rithik_codez

5) INSERTION SORT USING RECURSION


Insertion sort is a simple comparison-based sorting
algorithm that works by dividing the input array
into two parts: a sorted subarray and an unsorted
subarray. It iterates through the unsorted subarray,
takes one element at a time, and inserts it into its correct
position within the sorted subarray.
Here's a step-by-step explanation of the insertion sort
algorithm:
1. Start with an unsorted array of elements.
2. Iterate through the array from the second element to the
last element. This represents the
unsorted subarray.
Recursion Notes
@rithik_codez

3. For each element in the unsorted subarray,


compare it with the elements in the sorted subarray
(i.e., the elements to its left).
4. Move the elements in the sorted subarray that are
greater than the current element one position
to the right.
5. Insert the current element into its correct position
within the sorted subarray.
6. Repeat steps 3-5 until all elements in the unsorted
subarray are processed.
The sorted subarray starts with just the first
element of the input array and gradually grows as
more
elements are inserted into their correct positions. At
each iteration, the sorted subarray becomes larger,
and the unsorted subarray becomes smaller until all
elements are sorted.
The code is explained in the youtube video, still
providing a snippet for you guys
Recursion Notes
@rithik_codez

You will find the detailed explanation of the


same in my youtube video :)
Recursion Notes
@rithik_codez

6) WHAT IS MULTIPLE RECURSION?


Multiple recursion, also known as nested recursion,
refers to a situation where a recursive function
makes more than one recursive call within its own
execution. In multiple recursion, a function can call
itself multiple times, either directly or indirectly, to solve
a problem by dividing it into multiple smaller
subproblems.
Unlike single recursion, where a function makes only one
recursive call, multiple recursion involves
multiple levels of recursive calls, potentially forming a
recursive tree structure.
For more details please refer to my youtube video
7) FIBONACCI SERIES
The Fibonacci series is a sequence of numbers in which
each number is the sum of the two preceding
ones. The series starts with 0 and 1, and every
subsequent number is obtained by adding the two
numbers that come before it. So, the Fibonacci series
begins as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
and so on.
Mathematically, the Fibonacci series can be defined as:
F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2), for n ≥ 2
The code for the same is as follows
Recursion Notes
@rithik_codez

please refer to my youtube video for an explanation of the above


code

8)Total number of paths to reach the end of the


matrix from the start of the matrix(0,0 to n,n)
only by moving right or down

This is a very famous leetcode problem, I have


explained the solution in my youtube video please
refer
to the video for the explanation
Recursion Notes
@rithik_codez

9) Inorder, Preorder and Postorder traversal methods


in binary tree using double recursion.

So,These all are traversal methods in a binary tree


and are very easy to understand all the three are
explained in my youtube video, not going in much
detail here are the code snippets of all the three
traversal methods
Recursion Notes
@rithik_codez
Recursion Notes
@rithik_codez

So, these notes along with my youtube videos


will be of great help, the link to my youtube
videos are already in the telegram group, I will be
bringing up more such data structure and
algorithm videos and notes so stay tuned.

Follow me on instagram, subscribe me on


youtube, love you all :)

You might also like