Sunday, March 12, 2023

Application of Stack: Prefix and Postfix Expressions

 

📌 Application of Stack: Prefix and Postfix Expressions

Stacks are extensively used in expression evaluation and conversion, particularly for handling prefix and postfix expressions. These notations eliminate the need for parentheses and operator precedence rules, making computations more efficient.

🚀 Topics Covered:

✅ Infix, Prefix, and Postfix Notations
✅ Why Stacks are Used in Expression Evaluation
✅ Conversion from Infix to Postfix & Prefix
✅ Evaluating Postfix Expressions using Stack


1️⃣ Expression Notations

(a) Infix Notation (Standard Form)

  • Operators are between operands.
  • Requires operator precedence and parentheses for clarity.
  • Example:

    (3 + 5) * 2

(b) Prefix Notation (Polish Notation)

  • Operators before operands.
  • Example:

    * + 3 5 2 → Equivalent to (3 + 5) * 2

(c) Postfix Notation (Reverse Polish Notation - RPN)

  • Operators after operands.
  • Example:

    3 5 + 2 * → Equivalent to (3 + 5) * 2

💡 Why Use Prefix/Postfix?

  • No need for parentheses.
  • Easier and faster computation using stacks.
  • Widely used in compilers and calculators.

2️⃣ Why Stacks are Used in Expression Evaluation?

Stacks help in: ✔ Converting infix expressions to postfix or prefix.
Evaluating postfix expressions efficiently.


3️⃣ Conversion: Infix to Postfix Using Stack

Algorithm to Convert Infix to Postfix

1️⃣ Scan the infix expression from left to right.
2️⃣ Operands (A-Z, 0-9) → Directly append to the result.
3️⃣ *Operators (+, -, , /, ^) → Push to stack, considering precedence.
4️⃣ Parentheses Handling

  • '(' → Push to stack.
  • ')' → Pop from stack until '(' is found.
    5️⃣ Pop remaining operators after scanning.

Example:

Convert:


A + B * C

🔹 Stack Process

StepSymbolStackPostfix Expression
1AA
2++A
3B+A B
4*+ *A B
5C+ *A B C
6(End)A B C * +

Postfix Expression:


A B C * +

C Implementation: Infix to Postfix Conversion


#include <stdio.h> #include <ctype.h> #include <string.h> #define MAX 100 char stack[MAX]; int top = -1; // Push function void push(char c) { stack[++top] = c; } // Pop function char pop() { if (top == -1) return -1; return stack[top--]; } // Operator precedence function int precedence(char c) { if (c == '^') return 3; if (c == '*' || c == '/') return 2; if (c == '+' || c == '-') return 1; return 0; } // Convert infix to postfix void infixToPostfix(char* infix) { char postfix[MAX]; int j = 0; for (int i = 0; infix[i] != '\0'; i++) { if (isalnum(infix[i])) { postfix[j++] = infix[i]; // Append operand to postfix } else if (infix[i] == '(') { push(infix[i]); } else if (infix[i] == ')') { while (top != -1 && stack[top] != '(') { postfix[j++] = pop(); } pop(); // Remove '(' } else { while (top != -1 && precedence(stack[top]) >= precedence(infix[i])) { postfix[j++] = pop(); } push(infix[i]); } } while (top != -1) { postfix[j++] = pop(); } postfix[j] = '\0'; printf("Postfix: %s\n", postfix); } int main() { char infix[MAX]; printf("Enter infix expression: "); scanf("%s", infix); infixToPostfix(infix); return 0; }

🔹 Time Complexity: O(n)


4️⃣ Evaluating Postfix Expression Using Stack

Algorithm for Postfix Evaluation

1️⃣ Scan the postfix expression from left to right.
2️⃣ Operands (A-Z, 0-9) → Push onto stack.
3️⃣ *Operators (+, -, , /, ^) → Pop two operands, apply the operation, push result.
4️⃣ Final value in the stack is the result.

Example:

Evaluate:


6 2 + 5 * 8 4 / -
StepSymbolStack After Processing
166
226 2
3+8
458 5
5*40
6840 8
7440 8 4
8/40 2
9-38

Final Answer: 38


C Implementation: Postfix Evaluation


#include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAX 100 int stack[MAX]; int top = -1; // Push function void push(int value) { stack[++top] = value; } // Pop function int pop() { if (top == -1) return -1; return stack[top--]; } // Evaluate postfix expression int evaluatePostfix(char* postfix) { for (int i = 0; postfix[i] != '\0'; i++) { if (isdigit(postfix[i])) { push(postfix[i] - '0'); // Convert char to int } else { int val2 = pop(); int val1 = pop(); switch (postfix[i]) { case '+': push(val1 + val2); break; case '-': push(val1 - val2); break; case '*': push(val1 * val2); break; case '/': push(val1 / val2); break; } } } return pop(); } int main() { char postfix[MAX]; printf("Enter postfix expression: "); scanf("%s", postfix); printf("Result: %d\n", evaluatePostfix(postfix)); return 0; }

🔹 Time Complexity: O(n)


5️⃣ Summary Table: Stack Operations in Expression Handling

OperationTime Complexity
Infix to PostfixO(n)
Postfix EvaluationO(n)
Prefix EvaluationO(n)


No comments:

Post a Comment

Complete Binary Tree in Data Structures

  Complete Binary Tree in Data Structures 🌳 A Complete Binary Tree (CBT) is a type of Binary Tree where: ✔ All levels except possibly t...