Chapter 4
Chapter 4
Stacks
• A Stack is a linear data structure that follows a
particular order in which the operations are
performed. The order may be LIFO(Last In First
Out) or FILO(First In Last Out). LIFO implies that
the element that is inserted last, comes out first
and FILO implies that the element that is inserted
first, comes out last. It behaves like a stack of plates, where the last plate added is the
first one to be removed.
Advantages of Stacks
• Simplicity: Stacks are a simple and easy-to-understand data structure, making them
suitable for a wide range of applications.
• Efficiency: Push and pop operations on a stack can be performed in constant
time (O(1)), providing efficient access to data.
• Last-in, First-out (LIFO): Stacks follow the LIFO principle, ensuring that the last
element added to the stack is the first one removed. This behavior is useful in many
scenarios, such as function calls and expression evaluation.
• Limited memory usage: Stacks only need to store the elements that have been pushed
onto them, making them memory-efficient compared to other data structures.
Disadvantages of STACKS
• Limited access: Elements in a stack can only be accessed from the top, making it
difficult to retrieve or modify elements in the middle of the stack.
• Potential for overflow: If more elements are pushed onto a stack than it can hold, an
overflow error will occur, resulting in a loss of data.
• Not suitable for random access: Stacks do not allow for random access to elements,
making them unsuitable for applications where elements need to be accessed in a
specific order.
• Limited capacity: Stacks have a fixed capacity, which can be a limitation if the number
of elements that need to be stored is unknown or highly variable.
Priority Queues
• A priority queue is a collection of elements such that each element has been assigned a
priority and such that the order in which elements are deleted and processed comes from
the following rules.
Need of Recursion
• Performing the same operations multiple times with different inputs.
• In every step, we try smaller inputs to make the problem smaller.
• Base condition is needed to stop the recursion otherwise infinite loop will occur.
Applications of Recursion
• Tree and graph traversal: Recursion is frequently used for traversing and searching
data structures such as trees and graphs. Recursive algorithms can be used to explore all
the nodes or vertices of a tree or graph in a systematic way.
• Sorting algorithms: Recursive algorithms are also used in sorting algorithms such as
quicksort and merge sort. These algorithms use recursion to divide the data into smaller
subarrays or sublists, sort them, and then merge them back together.
• Divide-and-conquer algorithms: Many algorithms that use a divide-and-conquer
approach, such as the binary search algorithm, use recursion to break down the problem
into smaller subproblems.
• Fractal generation: Fractal shapes and patterns can be generated using recursive
algorithms. For example, the Mandelbrot set is generated by repeatedly applying a
recursive formula to complex numbers.
• Backtracking algorithms: Backtracking algorithms are used to solve problems that
involve making a sequence of decisions, where each decision depends on the previous
ones. These algorithms can be implemented using recursion to explore all possible paths
and backtrack when a solution is not found.
• Memoization: Memoization is a technique that involves storing the results of expensive
function calls and returning the cached result when the same inputs occur again.
Memoization can be implemented using recursive functions to compute and cache the
results of subproblems.