Given a Prefix expression, convert it into a Postfix expression. A Prefix expression places the operator before the operands (operator operand1 operand2), for example *+AB-CD is represented as (A + B) * (C − D). A Postfix expression places the operator after the operands (operand1 operand2 operator), for example AB+CD-* is represented as (A + B) * (C − D).
Example:
Input : Prefix : *+AB-CD
Output : AB+CD-*
Explanation : Prefix to Infix : (A+B) * (C-D)
Infix to Postfix : AB+CD-*
Input : Prefix : *-A/BC-/AKL
Output : ABC/-AK/L-*
Explanation : Prefix to Infix : (A-(B/C))*((A/K)-L)
Infix to Postfix : ABC/-AK/L-*
[Approach] - Using Stack - O(n) Time and O(n) Space
The idea is process from right to left so that we first see operands. We store operands in a stack. When an operator is found, the two most recent operands on the stack belong to it. Placing the operator after these operands forms the correct postfix expression
- Traverse the prefix expression from right to left
- If the symbol is an operand, push it onto the stack
- If the symbol is an operator, pop two operands from the stack, form a postfix string: operand1 + operand2 + operator and push the result back onto the stack
- Continue until the entire expression is processed
- The remaining element in the stack is the postfix expression
#include <iostream>
#include <stack>
using namespace std;
// function to check if character is operator or not
bool isOperator(char x)
{
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
}
return false;
}
// Convert prefix to Postfix expression
string preToPost(string &preExp)
{
stack<string> s;
// length of expression
int n = preExp.size();
// reading from right to left
for (int i = n - 1; i >= 0; i--)
{
// check if symbol is operator
if (isOperator(preExp[i]))
{
// pop two operands from stack
string op1 = s.top();
s.pop();
string op2 = s.top();
s.pop();
// concat the operands and operator
string temp = op1 + op2 + preExp[i];
// Push string temp back to stack
s.push(temp);
}
// if symbol is an operand
else {
// push the operand to the stack
s.push(string(1, preExp[i]));
}
}
// stack contains only the Postfix expression
return s.top();
}
// Driver Code
int main()
{
string preExp = "*-A/BC-/AKL";
cout << preToPost(preExp);
return 0;
}
class GfG {
// function to check if character
// is operator or not
static boolean isOperator(char x)
{
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
}
return false;
}
// Convert prefix to Postfix expression
static String preToPost(String preExp)
{
Stack<String> s = new Stack<String>();
// length of expression
int length = preExp.length();
// reading from right to left
for (int i = length - 1; i >= 0; i--)
{
// check if symbol is operator
if (isOperator(preExp.charAt(i)))
{
// pop two operands from stack
String op1 = s.peek();
s.pop();
String op2 = s.peek();
s.pop();
// concat the operands and operator
String temp = op1 + op2 + preExp.charAt(i);
// Push String temp back to stack
s.push(temp);
}
// if symbol is an operand
else {
// push the operand to the stack
s.push(preExp.charAt(i) + "");
}
}
// stack contains only the Postfix expression
return s.peek();
}
// Driver Code
public static void main(String args[])
{
String preExp = "*-A/BC-/AKL";
System.out.println(preToPost(preExp));
}
}
def isOperator(x):
return x in ['+', '-', '*', '/']
def preToPost(preExp):
stack = []
length = len(preExp)
# Read from right to left
for i in range(length - 1, -1, -1):
ch = preExp[i]
# If operator, pop two operands
if isOperator(ch):
op1 = stack.pop()
op2 = stack.pop()
# Concatenate operands and operator
temp = op1 + op2 + ch
# Push back to stack
stack.append(temp)
else:
# Push operand
stack.append(ch)
# Final postfix expression
return stack[-1]
if __name__== "__main__":
preExp = "*-A/BC-/AKL"
print( preToPost(preExp))
// C# Program to convert prefix to postfix
using System;
using System.Collections.Generic;
class GFG {
// function to check if character
// is operator or not
static bool isOperator(char x)
{
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
}
return false;
}
// Convert prefix to Postfix expression
static String preToPost(String preExp)
{
Stack<String> s = new Stack<String>();
// length of expression
int length = preExp.Length;
// reading from right to left
for (int i = length - 1; i >= 0; i--)
{
// check if symbol is operator
if (isOperator(preExp[i]))
{
// pop two operands from stack
String op1 = s.Peek();
s.Pop();
String op2 = s.Peek();
s.Pop();
// concat the operands and operator
String temp = op1 + op2 + preExp[i];
// Push String temp back to stack
s.Push(temp);
}
// if symbol is an operand
else {
// push the operand to the stack
s.Push(preExp[i] + "");
}
}
// stack contains only the Postfix expression
return s.Peek();
}
// Driver Code
public static void Main(String[] args)
{
String preExp = "*-A/BC-/AKL";
Console.WriteLine(preToPost(preExp));
}
}
function isOperator(x) {
return x === '+' || x === '-' || x === '*' || x === '/';
}
// Convert prefix to postfix expression
function preToPost(preExp) {
let stack = [];
let length = preExp.length;
// Read from right to left
for (let i = length - 1; i >= 0; i--) {
let ch = preExp[i];
// If operator, pop two operands
if (isOperator(ch)) {
let op1 = stack.pop();
let op2 = stack.pop();
// Concatenate operands and operator
let temp = op1 + op2 + ch;
// Push back to stack
stack.push(temp);
}
// If operand
else {
stack.push(ch);
}
}
// Final postfix expression
return stack[stack.length - 1];
}
// Driver code
let preExp = "*-A/BC-/AKL";
console.log(preToPost(preExp));
Output
ABC/-AK/L-*