Postfix to Infix
Last Updated :
11 Jul, 2023
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.
Postfix notation, also known as reverse Polish notation, is a syntax for mathematical expressions in which the mathematical operator is always placed after the operands. Though postfix expressions are easily and efficiently evaluated by computers, they can be difficult for humans to read. Complex expressions using standard parenthesized infix notation are often more readable than the corresponding postfix expressions. Consequently, we would sometimes like to allow end users to work with infix notation and then convert it to postfix notation for computer processing. Sometimes, moreover, expressions are stored or generated in postfix, and we would like to convert them to infix for the purpose of reading and editing
Examples:
Input : abc++
Output : (a + (b + c))
Input : ab*c+
Output : ((a*b)+c)
We have already discussed Infix to Postfix. Below is algorithm for Postfix to Infix.
Algorithm
1.While there are input symbol left
…1.1 Read the next symbol from the input.
2.If the symbol is an operand
…2.1 Push it onto the stack.
3.Otherwise,
…3.1 the symbol is an operator.
…3.2 Pop the top 2 values from the stack.
…3.3 Put the operator, with the values as arguments and form a string.
…3.4 Push the resulted string back to stack.
4.If there is only one value in the stack
…4.1 That value in the stack is the desired infix string.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isOperand( char x)
{
return (x >= 'a' && x <= 'z' ) ||
(x >= 'A' && x <= 'Z' );
}
string getInfix(string exp )
{
stack<string> s;
for ( int i=0; exp [i]!= '\0' ; i++)
{
if (isOperand( exp [i]))
{
string op(1, exp [i]);
s.push(op);
}
else
{
string op1 = s.top();
s.pop();
string op2 = s.top();
s.pop();
s.push( "(" + op2 + exp [i] +
op1 + ")" );
}
}
return s.top();
}
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' );
}
static String getInfix(String exp)
{
Stack<String> s = new Stack<String>();
for ( int i = 0 ; i < exp.length(); i++)
{
if (isOperand(exp.charAt(i)))
{
s.push(exp.charAt(i) + "" );
}
else
{
String op1 = s.peek();
s.pop();
String op2 = s.peek();
s.pop();
s.push( "(" + op2 + exp.charAt(i) +
op1 + ")" );
}
}
return s.peek();
}
public static void main(String args[])
{
String exp = "ab*c+" ;
System.out.println( getInfix(exp));
}
}
|
Python3
def isOperand(x):
return ((x > = 'a' and x < = 'z' ) or
(x > = 'A' and x < = 'Z' ))
def getInfix(exp) :
s = []
for i in exp:
if (isOperand(i)) :
s.insert( 0 , i)
else :
op1 = s[ 0 ]
s.pop( 0 )
op2 = s[ 0 ]
s.pop( 0 )
s.insert( 0 , "(" + op2 + i +
op1 + ")" )
return s[ 0 ]
if __name__ = = '__main__' :
exp = "ab*c+"
print (getInfix(exp.strip()))
|
C#
using System;
using System.Collections;
class GFG
{
static Boolean isOperand( char x)
{
return (x >= 'a' && x <= 'z' ) ||
(x >= 'A' && x <= 'Z' );
}
static String getInfix(String exp)
{
Stack s = new Stack();
for ( int i = 0; i < exp.Length; i++)
{
if (isOperand(exp[i]))
{
s.Push(exp[i] + "" );
}
else
{
String op1 = (String) s.Peek();
s.Pop();
String op2 = (String) s.Peek();
s.Pop();
s.Push( "(" + op2 + exp[i] +
op1 + ")" );
}
}
return (String)s.Peek();
}
public static void Main(String []args)
{
String exp = "ab*c+" ;
Console.WriteLine( getInfix(exp));
}
}
|
Javascript
<script>
function isOperand(x)
{
return (x >= 'a' && x <= 'z' ) ||
(x >= 'A' && x <= 'Z' );
}
function getInfix(exp)
{
let s = [];
for (let i = 0; i < exp.length; i++)
{
if (isOperand(exp[i]))
{
s.push(exp[i] + "" );
}
else
{
let op1 = s[s.length-1];
s.pop();
let op2 = s[s.length-1];
s.pop();
s.push( "(" + op2 + exp[i] +
op1 + ")" );
}
}
return s[s.length-1];
}
let exp = "ab*c+" ;
document.write( getInfix(exp));
</script>
|
PHP
<?php
class Stack {
protected $stack ;
protected $limit ;
function CreateStack( $limit ){
$this ->stack = array ();
$this ->limit = $limit ;
}
function push( $item ) {
if ( count ( $this ->stack) < $this ->limit) {
array_unshift ( $this ->stack, $item );
} else {
throw new RunTimeException( 'Stack is full!' );
}
}
function pop() {
if ( $this ->isEmpty()) {
throw new RunTimeException( 'Stack is empty!' );
} else {
return array_shift ( $this ->stack);
}
}
function top() {
return current( $this ->stack);
}
function isEmpty() {
return empty ( $this ->stack);
}
function Prec( $ch )
{
switch ( $ch )
{
case '+' :
case '-' :
return 1;
case '*' :
case '/' :
return 2;
case '^' :
return 3;
}
return -1;
}
function isOperand( $ch )
{
return ( $ch >= 'a' && $ch <= 'z' ) || ( $ch >= 'A' && $ch <= 'Z' );
}
function isOperator( $x ) {
switch ( $x ) {
case '+' :
case '-' :
case '/' :
case '*' :
return true;
}
return false;
}
public function getInfix( $exp )
{
$this ->CreateStack(sizeof( $exp ));
for ( $i =0; $exp [ $i ]!= null; $i ++)
{
if ( $this ->isOperand( $exp [ $i ]))
{
$op = $exp [ $i ];
$this ->push( $op );
}
else
{
$op1 = $this ->top(); $this ->pop();
$op2 = $this ->top(); $this ->pop();
$this ->push( "(" . $op2 . $exp [ $i ] . $op1 . ")" );
}
}
return $this ->top();
}
}
$myExample = new Stack();
echo $input = "ab*c+" ;
$exp = str_split ( $input ,sizeof( $input ));
echo '<br>' . $data = $myExample ->getInfix( $exp );
?>
|
Time Complexity: O(N) where N is the length of the string
Auxiliary Space: O(N) where N is the stack size.
Please Login to comment...