Postfix to Infix Last Updated : 27 Feb, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Postfix to infix conversion involves transforming expressions where operators follow their operands (postfix notation) into standard mathematical expressions with operators placed between operands (infix notation). This conversion improves readability and understanding.Infix expression: The expression of the form a op b. When an operator is in-between every pair of operands. Postfix expression: The expression of the form a b op. When an operator is followed for every pair of operands. Examples: Input: abc++Output: (a + (b + c))Explanation: Infix expression is (a + (b + c)) for expression abc++Input: ab*c+Output: ((a*b)+c)Explanation: Infix expression is ((a*b)+c) for expression ab*c+Input: abc+*d/Output: (((a * (b + c))) / d)Explanation: Infix expression is (((a * (b + c)))/d) for expression abc+*d/Approach: Using Stack - O(n) Time and O(n) SpaceInitialize Stack – Create an empty stack.Process Input Symbols – Read symbols one by one from the inputHandle Operands – Push operands onto the stack.Handle Operators – Pop the top two values, form an infix expression, and push it back.Retrieve Final Expression – The remaining value in the stack is the desired infix expression. C++ #include <bits/stdc++.h> using namespace std; bool isOperand(char x) { return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'); } // Get Infix for a given postfix // expression string getInfix(string exp) { stack<string> s; for (int i=0; exp[i]!='\0'; i++) { // Push operands if (isOperand(exp[i])) { string op(1, exp[i]); s.push(op); } // We assume that input is // a valid postfix and expect // an operator. else { string op1 = s.top(); s.pop(); string op2 = s.top(); s.pop(); s.push("(" + op2 + exp[i] + op1 + ")"); } } // There must be a single element // in stack now which is the required // infix. return s.top(); } // Driver code int main() { string exp = "ab*c+"; cout << getInfix(exp); return 0; } Java import java.util.*; class GFG { static boolean isOperand(char x) { return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'); } // Get Infix for a given postfix // expression static String getInfix(String exp) { Stack<String> s = new Stack<String>(); for (int i = 0; i < exp.length(); i++) { // Push operands if (isOperand(exp.charAt(i))) { s.push(exp.charAt(i) + ""); } // We assume that input is // a valid postfix and expect // an operator. else { String op1 = s.peek(); s.pop(); String op2 = s.peek(); s.pop(); s.push("(" + op2 + exp.charAt(i) + op1 + ")"); } } // There must be a single element // in stack now which is the required // infix. return s.peek(); } // Driver code public static void main(String args[]) { String exp = "ab*c+"; System.out.println( getInfix(exp)); } } // This code is contributed by Arnab Kundu Python def isOperand(x): return ((x >= 'a' and x <= 'z') or (x >= 'A' and x <= 'Z')) # Get Infix for a given postfix # expression def getInfix(exp) : s = [] for i in exp: # Push operands if (isOperand(i)) : s.insert(0, i) # We assume that input is a # valid postfix and expect # an operator. else: op1 = s[0] s.pop(0) op2 = s[0] s.pop(0) s.insert(0, "(" + op2 + i + op1 + ")") # There must be a single element in # stack now which is the required # infix. return s[0] # Driver Code if __name__ == '__main__': exp = "ab*c+" print(getInfix(exp.strip())) # This code is contributed by # Shubham Singh(SHUBHAMSINGH10) C# using System; using System.Collections; class GFG { static Boolean isOperand(char x) { return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'); } // Get Infix for a given postfix // expression static String getInfix(String exp) { Stack s = new Stack(); for (int i = 0; i < exp.Length; i++) { // Push operands if (isOperand(exp[i])) { s.Push(exp[i] + ""); } // We assume that input is // a valid postfix and expect // an operator. else { String op1 = (String) s.Peek(); s.Pop(); String op2 = (String) s.Peek(); s.Pop(); s.Push("(" + op2 + exp[i] + op1 + ")"); } } // There must be a single element // in stack now which is the required // infix. return (String)s.Peek(); } // Driver code public static void Main(String []args) { String exp = "ab*c+"; Console.WriteLine( getInfix(exp)); } } // This code is contributed by Arnab Kundu JavaScript function isOperand(x) { return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z'); } // Get Infix for a given postfix // expression function getInfix(exp) { let s = []; for (let i = 0; i < exp.length; i++) { // Push operands if (isOperand(exp[i])) { s.push(exp[i] + ""); } // We assume that input is // a valid postfix and expect // an operator. else { let op1 = s[s.length-1]; s.pop(); let op2 = s[s.length-1]; s.pop(); s.push("(" + op2 + exp[i] + op1 + ")"); } } // There must be a single element // in stack now which is the required // infix. return s[s.length-1]; } // Driver code let exp = "ab*c+"; console.log( getInfix(exp)); Output((a*b)+c)Related PostInfix, Postfix and Prefix Expressions/Notations Convert Infix expression to Postfix expression Infix to Postfix Conversion using Stack in C++ Comment More info P PUSHPENDER SINGH Follow Improve Article Tags : Strings Stack Mathematical DSA expression-evaluation +1 More Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like