Correct option is B
Recursion in programming is implemented using a
stack data structure. When a recursive function is called, the system stores the function's execution state (parameters, local variables, return address) on the
call stack. Each time a function calls itself, a new frame is pushed onto the stack. When a function completes, its frame is popped from the stack.
Important Key Points:
1.
Why Stack is Used in Recursion?
· Every function call in recursion creates a new
stack frame.
· These stack frames maintain function parameters, local variables, and return addresses.
· The function returns by
popping the stack, allowing execution to resume from where it left off.
Example of Recursion Using a Stack:
int factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}
· The function calls are stored in a
stack as:
factorial(3) → factorial(2) → factorial(1) → factorial(0)
· Once factorial(0) returns 1, the calls
unwind from the stack.
2. The system stack is used for function calls, but recursion can also be implemented using an explicit stack (iteration-based approach).
3. If recursion depth is too high, it may cause a stack overflow error.
4. Tail recursion optimization (TCO) can reduce stack usage in some languages like Python, Lisp and functional programming paradigms.
5. Stacks are also used in expression evaluation (e.g., postfix evaluation) and backtracking algorithms.
Knowledge Booster:
·
Queue → ❌ Incorrect: Queues
follow FIFO (First-In-First-Out) order, whereas recursion follows
LIFO (Last-In-First-Out), which is characteristic of a stack.
·
(c) Binary Tree → ❌ Incorrect: While recursion is
commonly used in binary tree traversals (e.g., inorder, preorder, postorder), the actual
implementation still relies on a stack for function calls.