Lab 05 Stack & Recursion
Lab 05 Stack & Recursion
Session 5
Course: Data Structures (CL1002) Semester: Fall 2024
Instructor: Shafique Rehman T.A:
Note:
Lab manual cover following below recursion topics
Base Condition, Direct and Indirect Recursion, Tailed Recursion, Nested Recursion,
Backtracking
Maintain discipline during the lab.
Just raise hand if you have any problem.
Completing all tasks of each lab is compulsory.
Get your lab checked at the end of the session.
Recursion Visualization Tool (https://visualgo.net/en/recursion)
Key Points: In the above example, base case for n < = 1 is defined and larger value of number
can be solved by converting to smaller one till base case is reached.
Direct and Indirect Recursion
(Direct Recursion)
(In-Direct Recursion)
Tailed and Non Tailed Recursion
Non Tailed Recursion:
Tailed Recursion:
Nested Recursion
Issues in Recursion
1. Complexity of Recursive Logic: Problem: Designing and understanding recursive solutions
can be complex, especially for problems with multiple recursive calls or complex base cases.
2. Stack Overflow: Recursion heavily relies on the call stack. If the recursion goes too deep, it
can cause a stack overflow, leading to a program crash.
Example:
void infiniteRecursion() {
infiniteRecursion(); // Calls itself indefinitely
}
3. Infinite Recursion: If the base condition is not defined or incorrect, the recursion may
continue indefinitely, causing infinite recursion.
Example: void badRecursion(int n) {
if (n > 0) {
badRecursion(n); // Missing decrement of n
}
}
4. Performance: Recursion can be inefficient for large inputs due to repeated calculations.
Memoization or iteration may be better for performance in such cases.
Example: Calculating Fibonacci numbers using simple recursion leads to exponential time
complexity.
Backtracking
A Maze is given as N*N binary matrix of blocks where source block is the upper left most
block i.e., maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A
rat starts from source and has to reach the destination. The rat can move only in two directions:
forward and down.
In the maze matrix, 0 means the block is a dead end and 1 means the block can be used in the
path from source to destination. Note that this is a simple version of the typical Maze problem.
For example, a more complex version can be that the rat can move in 4 directions and a more
complex version can be with a limited number of moves.
Stack is a linear data structure based on LIFO in which the insertion of a new element and
removal of an existing element takes place at the same end represented as the top of the stack.
To implement the stack, it is required to maintain the pointer to the top of the stack , which is
the last element to be inserted because we can access the elements only on the top of the stack.
Types of Stack Data Structure:
Fixed Size Stack : As the name suggests, a fixed size stack has a fixed size and
cannot grow or shrink dynamically. If the stack is full and an attempt is made to add an
element to it, an overflow error occurs. If the stack is empty and an attempt is made to
remove an element from it, an underflow error occurs.
Dynamic Size Stack : A dynamic size stack can grow or shrink dynamically. When
the stack is full, it automatically increases its size to accommodate the new element,
and when the stack is empty, it decreases its size. This type of stack is implemented
using a linked list, as it allows for easy resizing of the stack.
In order to make manipulations in a stack, there are certain operations provided to us.
push() to insert an element into the stack (check if there is space available in the stack,
increment the top and insert the value at this new top position)
pop() to remove an element from the stack (check if there is an element available in
stack, return the top element and decrement the top)
top() Returns the top element of the stack. (simply returns the element at top)
isEmpty() returns true if stack is empty else false. (if top == -1, stack is empty)
isFull() returns true if the stack is full else false. (if top == MAX, stack is empty)
Lab Exercises for Recursion
1. Sort an array
4. Binary search
1. Arrays
2. Linked list