arrow
arrow
arrow
Which data structure is mainly used for implementing the recursive algorithm?
Question



Which data structure is mainly used for implementing the recursive algorithm?

A.

Queue

B.

Stack

C.

Binary tree

D.

More than one of the above

E.

None of the above

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.

Free Tests

Free
Must Attempt

UPTET Paper 1: PYP Held on 23rd Jan 2022 (Shift 1)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
Free
Must Attempt

UPTET Paper 2 Social Science : PYP Held on 23rd Jan 2022 (Shift 2)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
Free
Must Attempt

UPTET Paper 2 Maths & Science : PYP Held on 23rd Jan 2022 (Shift 2)

languageIcon English
  • pdpQsnIcon150 Questions
  • pdpsheetsIcon150 Marks
  • timerIcon150 Mins
languageIcon English
test-prime-package

Access ‘BPSC TRE (11-12)’ Mock Tests with

  • 60000+ Mocks and Previous Year Papers
  • Unlimited Re-Attempts
  • Personalised Report Card
  • 500% Refund on Final Selection
  • Largest Community
students-icon
354k+ students have already unlocked exclusive benefits with Test Prime!
Our Plans
Monthsup-arrow