Skip to content

Commit 1ac1a5a

Browse files
authored
Add Postfix to Infix using Stack (fixes TheAlgorithms#2339) (TheAlgorithms#2970)
1 parent fe61eb2 commit 1ac1a5a

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package com.thealgorithms.datastructures.stacks;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Postfix to Infix implementation via Stack
7+
*
8+
* Function: String getPostfixToInfix(String postfix)
9+
* Returns the Infix Expression for the given postfix parameter.
10+
*
11+
* Avoid using parentheses/brackets/braces for the postfix string.
12+
* Postfix Expressions don't require these.
13+
*
14+
*
15+
* @author nikslyon19 (Nikhil Bisht)
16+
*
17+
*/
18+
19+
public class PostfixToInfix {
20+
21+
22+
public static boolean isOperator(char token)
23+
{
24+
switch(token)
25+
{
26+
case '+':
27+
case '-':
28+
case '/':
29+
case '*':
30+
case '^':
31+
return true;
32+
}
33+
34+
return false;
35+
}
36+
37+
38+
public static boolean isValidPostfixExpression(String postfix)
39+
{
40+
/* Postfix expression length should NOT be less than 3 */
41+
if(postfix.length() < 3) return false;
42+
43+
44+
/* First two characters should NOT be operators */
45+
if(isOperator(postfix.charAt(0))) return false;
46+
if(isOperator(postfix.charAt(1))) return false;
47+
48+
49+
int operandCount = 0;
50+
int operatorCount = 0;
51+
52+
53+
/* Traverse the postfix string to check if --> Number of operands = Number of operators + 1 */
54+
for(int i = 0; i < postfix.length(); i++)
55+
{
56+
char token = postfix.charAt(i);
57+
58+
if(isOperator(token))
59+
{
60+
operatorCount++;
61+
if(operatorCount >= operandCount) return false;
62+
}
63+
64+
else
65+
{
66+
if(operatorCount == 0)
67+
{
68+
operandCount++;
69+
continue;
70+
}
71+
72+
if(operandCount != operatorCount + 1) return false;
73+
74+
75+
/* Operand count is set to 2 because:-
76+
*
77+
* 1) the previous set of operands & operators combined have become a single valid expression,
78+
* which could be considered/assigned as a single operand.
79+
*
80+
* 2) the operand in the current iteration.
81+
*/
82+
operandCount = 2;
83+
84+
85+
/* Reset operator count */
86+
operatorCount = 0;
87+
}
88+
}
89+
90+
return (operandCount == operatorCount + 1);
91+
}
92+
93+
94+
public static String getPostfixToInfix(String postfix)
95+
{
96+
String infix = "";
97+
98+
if(postfix.isEmpty()) return infix;
99+
100+
101+
/* Validate Postfix expression before proceeding with the Infix conversion */
102+
if(!isValidPostfixExpression(postfix))
103+
{
104+
throw new IllegalArgumentException("Invalid Postfix Expression");
105+
}
106+
107+
Stack<String> stack = new Stack<>();
108+
StringBuilder valueString = new StringBuilder();
109+
110+
String operandA, operandB;
111+
char operator;
112+
113+
114+
for(int index = 0; index < postfix.length(); index++)
115+
{
116+
char token = postfix.charAt(index);
117+
118+
if(!isOperator(token))
119+
{
120+
stack.push(Character.toString(token));
121+
continue;
122+
}
123+
124+
operator = token;
125+
operandB = stack.pop();
126+
operandA = stack.pop();
127+
128+
valueString.append('(');
129+
130+
valueString.append(operandA);
131+
valueString.append(operator);
132+
valueString.append(operandB);
133+
134+
valueString.append(')');
135+
136+
stack.push(valueString.toString());
137+
valueString.setLength(0);
138+
}
139+
140+
infix = stack.pop();
141+
return infix;
142+
}
143+
144+
145+
public static void main(String args[])
146+
{
147+
assert getPostfixToInfix("ABC+/").equals("(A/(B+C))");
148+
assert getPostfixToInfix("AB+CD+*").equals("((A+B)*(C+D))");
149+
assert getPostfixToInfix("AB+C+D+").equals("(((A+B)+C)+D)");
150+
assert getPostfixToInfix("ABCDE^*/-").equals("(A-(B/(C*(D^E))))");
151+
assert getPostfixToInfix("AB+CD^/E*FGH+-^").equals("((((A+B)/(C^D))*E)^(F-(G+H)))");
152+
}
153+
}

0 commit comments

Comments
 (0)