@@ -20,125 +20,83 @@ public final class PostfixToInfix {
20
20
private PostfixToInfix () {
21
21
}
22
22
23
+ /**
24
+ * Determines if a given character is a valid arithmetic operator.
25
+ *
26
+ * @param token the character to check
27
+ * @return true if the character is an operator, false otherwise
28
+ */
23
29
public static boolean isOperator (char token ) {
24
- switch (token ) {
25
- case '+' :
26
- case '-' :
27
- case '/' :
28
- case '*' :
29
- case '^' :
30
- return true ;
31
- default :
32
- return false ;
33
- }
30
+ return token == '+' || token == '-' || token == '/' || token == '*' || token == '^' ;
34
31
}
35
32
33
+ /**
34
+ * Validates whether a given string is a valid postfix expression.
35
+ *
36
+ * A valid postfix expression must meet these criteria:
37
+ * 1. It should have at least one operator and two operands.
38
+ * 2. The number of operands should always be greater than the number of operators at any point in the traversal.
39
+ *
40
+ * @param postfix the postfix expression string to validate
41
+ * @return true if the expression is valid, false otherwise
42
+ */
36
43
public static boolean isValidPostfixExpression (String postfix ) {
37
- /* Postfix expression length should NOT be less than 3 */
38
- if (postfix .length () < 3 ) {
39
- return false ;
44
+ if (postfix .length () == 1 && (Character .isAlphabetic (postfix .charAt (0 )))) {
45
+ return true ;
40
46
}
41
47
42
- /* First two characters should NOT be operators */
43
- if (isOperator (postfix .charAt (0 ))) {
44
- return false ;
45
- }
46
- if (isOperator (postfix .charAt (1 ))) {
47
- return false ;
48
+ if (postfix .length () < 3 ) {
49
+ return false ; // Postfix expression should have at least one operator and two operands
48
50
}
49
51
50
52
int operandCount = 0 ;
51
53
int operatorCount = 0 ;
52
54
53
- /* Traverse the postfix string to check if --> Number of operands = Number of operators + 1
54
- */
55
- for (int i = 0 ; i < postfix .length (); i ++) {
56
- char token = postfix .charAt (i );
57
-
55
+ for (char token : postfix .toCharArray ()) {
58
56
if (isOperator (token )) {
59
57
operatorCount ++;
60
58
if (operatorCount >= operandCount ) {
61
- return false ;
59
+ return false ; // Invalid: more operators than operands at any point
62
60
}
63
61
} else {
64
- if (operatorCount == 0 ) {
65
- operandCount ++;
66
- continue ;
67
- }
68
-
69
- if (operandCount != operatorCount + 1 ) {
70
- return false ;
71
- }
72
-
73
- /* Operand count is set to 2 because:-
74
- *
75
- * 1) the previous set of operands & operators combined have become a single valid
76
- * expression, which could be considered/assigned as a single operand.
77
- *
78
- * 2) the operand in the current iteration.
79
- */
80
- operandCount = 2 ;
81
-
82
- /* Reset operator count */
83
- operatorCount = 0 ;
62
+ operandCount ++;
84
63
}
85
64
}
86
65
87
- return ( operandCount == operatorCount + 1 ) ;
66
+ return operandCount == operatorCount + 1 ;
88
67
}
89
68
69
+ /**
70
+ * Converts a valid postfix expression to an infix expression.
71
+ *
72
+ * @param postfix the postfix expression to convert
73
+ * @return the equivalent infix expression
74
+ * @throws IllegalArgumentException if the postfix expression is invalid
75
+ */
90
76
public static String getPostfixToInfix (String postfix ) {
91
- String infix = "" ;
92
-
93
77
if (postfix .isEmpty ()) {
94
- return infix ;
78
+ return "" ;
95
79
}
96
80
97
- /* Validate Postfix expression before proceeding with the Infix conversion */
98
81
if (!isValidPostfixExpression (postfix )) {
99
82
throw new IllegalArgumentException ("Invalid Postfix Expression" );
100
83
}
101
84
102
85
Stack <String > stack = new Stack <>();
103
86
StringBuilder valueString = new StringBuilder ();
104
87
105
- String operandA ;
106
- String operandB ;
107
- char operator ;
108
-
109
- for (int index = 0 ; index < postfix .length (); index ++) {
110
- char token = postfix .charAt (index );
111
-
88
+ for (char token : postfix .toCharArray ()) {
112
89
if (!isOperator (token )) {
113
90
stack .push (Character .toString (token ));
114
- continue ;
91
+ } else {
92
+ String operandB = stack .pop ();
93
+ String operandA = stack .pop ();
94
+ valueString .append ('(' ).append (operandA ).append (token ).append (operandB ).append (')' );
95
+ stack .push (valueString .toString ());
96
+ valueString .setLength (0 );
115
97
}
116
-
117
- operator = token ;
118
- operandB = stack .pop ();
119
- operandA = stack .pop ();
120
-
121
- valueString .append ('(' );
122
-
123
- valueString .append (operandA );
124
- valueString .append (operator );
125
- valueString .append (operandB );
126
-
127
- valueString .append (')' );
128
-
129
- stack .push (valueString .toString ());
130
- valueString .setLength (0 );
131
98
}
132
99
133
- infix = stack .pop ();
134
- return infix ;
135
- }
136
-
137
- public static void main (String [] args ) {
138
- assert getPostfixToInfix ("ABC+/" ).equals ("(A/(B+C))" );
139
- assert getPostfixToInfix ("AB+CD+*" ).equals ("((A+B)*(C+D))" );
140
- assert getPostfixToInfix ("AB+C+D+" ).equals ("(((A+B)+C)+D)" );
141
- assert getPostfixToInfix ("ABCDE^*/-" ).equals ("(A-(B/(C*(D^E))))" );
142
- assert getPostfixToInfix ("AB+CD^/E*FGH+-^" ).equals ("((((A+B)/(C^D))*E)^(F-(G+H)))" );
100
+ return stack .pop ();
143
101
}
144
102
}
0 commit comments