0% found this document useful (0 votes)
14 views15 pages

Stack 5

Uploaded by

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

Stack 5

Uploaded by

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

APEX INSTITUTE OF TECHNOLOGY

Bachelor of Engineering (Computer Science &


Engineering)

Subject: Data Structure


Subject Code: CSH-231
Chapter: Stack
Subject Coordinator:
Mr. Vishal Kumar
(E12820)

Stack DISCOVER . LEARN .


Lecture No. 2.2.5 EMPOWER
Index

• Meaning and importance of recursion

• Principles of recursion

• Implementation of recursive procedure

2 2
Meaning of Recursion
• Recursion is an important concept in computer science.
• The process in which a function calls itself directly or
indirectly is called recursion and the corresponding
function is called a recursive function. A recursive
function, then, is a function that calls itself.
• A recursive function involves a recursive call and a base
case. A recursive call is the part of the function body
that calls itself until a specified condition is met, while
the base case is the function’s stopping point. It is
important that we have a base case because without it,
we would be stuck in an infinite loop.
Importance of Recursion

1. The code may be easier to write.


2. To solve such problems which are naturally
recursive such as tower of Hanoi.
3. Reduce unnecessary calling of function.
4. Extremely useful when applying the same solution.
5. Recursion reduce the length of code.
6. It is very useful in solving the data structure
problem.
7. Stacks evolutions and infix, prefix, postfix
evaluations etc.
Principles of recursion

• The recursion is a process by which a function calls


itself. We use recursion to solve bigger problem into
smaller sub-problems.
• One thing we have to keep in mind, that if each sub-
problem is following same kind of patterns, then only
we can use the recursive approach.
• A recursive function has two different parts. The base
case and the recursive case. The base case is used to
terminate the task of recurring. If base case is not
defined, then the function will recur infinite number
of times (Theoretically).
Continue..
Continue..
• In computer program, when we call one function, the
value of the program counter is stored into the internal
stack before jumping into the function area.
• After completing the task, it pops out the address and
assign it into the program counter, then resume the task.
During recursive call, it will store the address multiple
times, and jumps into the next function call statement.
• If one base case is not defined, it will recur again and
again, and store address into stack. If the stack has no
space anymore, it will raise an error as “Internal Stack
Overflow”.
Implementation of recursive
procedure
How recursion works?
Continue..
Working of Recursion
Continue..
• The recursion continues until some condition is met
to prevent it.
• To prevent infinite recursion, if…else statement (or
similar approach) can be used where one branch
makes the recursive call, and other doesn't.
Example: Sum of Natural
Numbers Using Recursion
Continue..

• Initially, the sum() is called from the main() function


with number passed as an argument.
• Suppose, the value of n inside sum() is 3 initially. During the next
function call, 2 is passed to the sum() function. This process
continues until n is equal to 0.
• When n is equal to 0, the if condition fails and the else part is
executed returning the sum of integers ultimately to
the main() function.
Continue..
References
WEB LINKS
• https://www.geeksforgeeks.org/data-structures/
• https://www.javatpoint.com/data-structure-tutoria
l
• https://www.tutorialspoint.com/data_structures_al
gorithms/index.htm
VIDEO LINK
• https://www.youtube.com/watch?v=AT14lCXuMKI
&list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU
Research Paper
• https://books.google.co.in/books?id=S-tXjl1hsUYC
THANK YOU

For queries
Email: [email protected]

15

You might also like