Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Conversion of Postfix expression to Infix expression

Last Updated on October 10, 2022 by Gokul Kannan

Problem statement

Given an arithmetic expression in postfix notation , convert it into the equivalent infix notation.

Sample example :

Postfix Input : abc/-ad/e-*

Infix output : ((a-(b/c))*((a/d)-e))

Postfix input : ab+cd+*

Infix Output : ((a+b)*(c+d))

Introduction to Arithmetic Notations

Any arithmetic expression consists of operands and operators . The way we arrange our operators and operands to write the arithmetic expression is called Notation .

There are three different notations for writing the Arithmetic expression :

  • Infix expression An expression is said to be in infix notation if the operators in the expression are placed in between the operands on which the operator works.
    For example => a + b * c Infix expressions are easy to read ,write and understand by humans , but not by computer It’s costly , in terms of time and space , to process Infix expressions

  • Postfix expression (Reverse Polish Notation) An expression is said to be in postfix notation if the operators in the expression are placed after the operands on which the operator works. For example => abc*+ It’s most used to notation for evaluating arithmetic expression

  • Prefix expression (or Polish Notation ) An expression is said to be in prefix notation if the operators in the expression are placed before the operands on which the operator works. For example => +a*bc

Precedence order and Associativity of Operators

PrecedenceTypeOperatorsAssociativity
1Postfix() [] -> . ++ —Left to Right
2Unary+ – ! ~ ++ — (type)* & sizeofRight to Left
3Multiplicative* / %Left to Right
4Additive+ –Left to Right
5Shift<<, >>Left to Right
6Relational< <= > >=Left to Right
7Equality== !=Left to Right
8Bitwise AND&Left to Right
9Bitwise XOR^Left to Right
10Bitwise OR|Left to Right
11Logical AND&&Left to Right
12Logical OR||Left to Right
13Conditional?:Right to Left
14Assignment= += -+ *= /= %= >>= <<= &= ^= |=Right to Left
15Comma,Left to Right

In this article we are gonna talk about how to convert postfix expressions into infix expressions . For the simplicity and understanding purpose , we will consider only β€˜+’ , β€˜-’ ,’/’ , β€˜*’ , β€˜(’ and β€˜)’ .

Approach

For converting Postfix to infix we use a stack . The stack helps us store the operands. Whenever an operator is found , we pop two operands from the stack and push a new operand , which is the result of the current operator on the popped operands, into the stack with parenthesis . The final element at the top of the stack will be our infix expression .

Algorithm

  1. Scan all symbols one by one from left to right in the given postfix expression .

  2. If the reading symbol is an operand , push it into the stack .

  3. If the reading symbol is an operator , then a. Pop two expression from the stack , operand1 and operand2 , which is operand for the current operator b. Push β€œ(β€œ + operand2 + operator + operand1 + β€œ)” into the stack

  4. If there is no symbol left then stop the process . Top of the stack will have the required infix expression .

NOTE : β€˜+’ denotes the concatenation of strings .

Postfix to infix conversion example with dry run

Implementation

#include <bits/stdc++.h>
using namespace std;

bool isOperand(char ch){
	return (ch>='a' && ch<='z') || (ch>='A' && ch <='Z');
}

string postfixToInfix(string postfix) {
  stack<string> st;
 
  int len = postfix.size();
  for (int i = 0; i < len; i++) {
  	if(isOperand(postfix[i])){
  		
  		st.push(string(1,postfix[i]));
  	   
  	}
  	else{
  		string operand1=st.top();
  		st.pop();
  		
  		string operand2=st.top();
  		st.pop();
  		
  		st.push("(" + operand2 + string(1,postfix[i]) + operand1 + ")");
  	}
  }
  return st.top();
}

int main() {
	
	string postfix="abc/-ad/e-*";
	string infix=postfixToInfix(postfix);
	cout<<"Postfix expression : "<<postfix<<endl;
	cout<<"Infix expression : "<<infix<<endl;
	
	return 0;
}

This article tried to discuss postfix to infix conversion. Hope this blog helps you understand and solve the problem. To practice more problems you can check out MYCODE | Competitive Programming at PrepBytes.

Leave a Reply

Your email address will not be published. Required fields are marked *