2079 1

BIT201 ยท 2079 ยท 10 marks

How stack is used in recursion? Explain different stack operations. Explain algorithm to convert an infix expression to postfix using stack.

How stack is used in recursion? Explain different stack operations. Explain algorithm to convert an infix expression to postfix using stack.[10]

Data Structures and AlgorithmsChapter 5: Recursion and Advanced TechniquesStack usage in recursion

Quick answer

When a recursive function is called, the system uses an internal data structure called the call stack (or system stack) to manage function calls.

Stack in Recursion, Stack Operations, and Infix to Postfix Conversion


1. How Stack is Used in Recursion (2 marks)

Mechanism:

  • Every time a function calls itself recursively, the current state (local variables, parameters, and return address) is pushed onto the stack.
  • When the base case is reached, the function returns and the saved state is popped from the stack, resuming execution from where it left off.
  • This continues until the stack is empty and the original call completes.

Example: Factorial of n

factorial(3)
  โ†’ calls factorial(2)   [state of factorial(3) pushed]
    โ†’ calls factorial(1) [state of factorial(2) pushed]
      โ†’ returns 1        [base case]
    โ† factorial(2) = 2ร—1 = 2  [popped]
  โ† factorial(3) = 3ร—2 = 6   [popped]

Stack at deepest point:

Stack (top โ†’ bottom)
factorial(1)
factorial(2)
factorial(3)

Stack follows LIFO (Last In First Out) which perfectly models the "last called, first returned" nature of recursion.


2. Stack Operations (3 marks)

A stack is a linear data structure that follows the LIFO principle. The main operations are:

a) PUSH (Insert an element)

Adds an element to the top of the stack.

Algorithm:

PUSH(STACK, TOP, MAXSIZE, ITEM):
1. If TOP = MAXSIZE - 1, then:
      Print "Stack Overflow" and Return
2. Set TOP = TOP + 1
3. Set STACK[TOP] = ITEM
4. Return

b) POP (Delete an element)

Removes and returns the element from the top of the stack.

Algorithm:

POP(STACK, TOP):
1. If TOP = -1, then:
      Print "Stack Underflow" and Return
2. Set ITEM = STACK[TOP]
3. Set TOP = TOP - 1
4. Return ITEM

c) PEEK / TOP

Returns the top element without removing it.

PEEK(STACK, TOP):
1. If TOP = -1, then:
      Print "Stack is Empty" and Return
2. Return STACK[TOP]

d) isEmpty

Checks whether the stack is empty.

isEmpty(STACK, TOP):
1. If TOP = -1, Return TRUE
2. Else Return FALSE

e) isFull

Checks whether the stack is full.

isFull(STACK, TOP, MAXSIZE):
1. If TOP = MAXSIZE - 1, Return TRUE
2. Else Return FALSE

3. Algorithm to Convert Infix to Postfix Using Stack (5 marks)

Key Concepts

TermMeaning
InfixOperator between operands: A + B
PostfixOperator after operands: A B +

Operator Precedence Table

OperatorPrecedence
^ (exponent)3 (highest)
*, /2
+, -1 (lowest)
(0 (when in stack)

Algorithm: Infix to Postfix

InfixToPostfix(expression):

1. Initialize an empty STACK and empty output string POSTFIX.
2. Scan the infix expression from LEFT to RIGHT, one token at a time.
3. For each token:

   a. If token is an OPERAND (letter or digit):
         Append it directly to POSTFIX.

   b. If token is '(' (left parenthesis):
         PUSH it onto STACK.

   c. If token is ')' (right parenthesis):
         While TOP of STACK โ‰  '(':
            POP from STACK and append to POSTFIX.
         POP '(' from STACK (discard it).

   d. If token is an OPERATOR (+, -, *, /, ^):
         While STACK is not empty AND
               precedence(TOP of STACK) >= precedence(token):
            POP from STACK and append to POSTFIX.
         PUSH the current token onto STACK.

4. After all tokens are scanned:
      While STACK is not empty:
         POP from STACK and append to POSTFIX.

5. Return POSTFIX.

Worked Example

Convert: A + B * C - D

StepTokenStack (bottomโ†’top)Postfix Output
1A(empty)A
2++A
3B+A B
4*+ *A B
5C+ *A B C
6-pop * โ†’ pop +, push -A B C * +
7D-A B C * + D
End-(empty)A B C * + D -

Result: A B C * + D -


Another Example with Parentheses

Convert: (A + B) * C

StepTokenStackPostfix
1((
2A(A
3+( +A
4B( +A B
5)(empty)A B +
6*

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.