Professional Documents
Culture Documents
Data Structure - Infix Postfix Prefix - Converter & Evaluator
Data Structure - Infix Postfix Prefix - Converter & Evaluator
Simple Tools
A+(B*C-(D/E-F)*G)*H
By Raj
https://raj457036.github.io/Simple-Tools/prefixAndPostfixConvertor.html 1/3
2/4/2020 Data Structure : Infix Postfix Prefix - Converter & Evaluator
0 A ( A
1 + (+ A
2 ( (+( A
3 B (+( AB
4 * (+(* AB
5 C (+(* ABC
6 - (+(- ABC*
7 ( (+(-( ABC*
8 D (+(-( ABC*D
9 / (+(-(/ ABC*D
10 E (+(-(/ ABC*DE
11 - (+(-(- ABC*DE/
12 F (+(-(- ABC*DE/F
13 ) (+(- ABC*DE/F-
14 * (+(-* ABC*DE/F-
15 G (+(-* ABC*DE/F-G
16 ) (+ ABC*DE/F-G*-
17 * (+* ABC*DE/F-G*-
18 H (+* ABC*DE/F-G*-H
https://raj457036.github.io/Simple-Tools/prefixAndPostfixConvertor.html 2/3
2/4/2020 Data Structure : Infix Postfix Prefix - Converter & Evaluator
19 ) ABC*DE/F-G*-H*+
Algorithm used
Postfix
Step 1: Add '')" to the end of the infix expression
Step 2: Push(o nto the stack
Step 3: Repeat until each character in the infix notation is scanned
IF a(is encountered, push it on the stack
IF an operand (whetheradigit oracharacter) is encountered, add it postfix expression.
IF a ")" is encountered, then
a. Repeatedly pop from stack and add it to the postfix expression until a "(" is encountered.
b. Discard the "(".That is, remove the(from stack and do not add it to the postfix expression
IF an operator O is encountered, then
a. Repeatedly pop from stack and add each operator ( popped from the stack) to the postfix expression which
has the same precedence orahigher precedence than O
b. Push the operator to the stack
[END OF IF]
Step 4: Repeatedly pop from the stack and add it to the postfix expression until the stack is empty
Step 5: EXIT
Prefix
Step 1: Reverse the infix string. Note that while reversing the string you must interchange left and right parentheses.
Step 2: Obtain the postfix expression of the infix expression Step 1.
Step 3: Reverse the postfix expression to get the prefix expression
https://raj457036.github.io/Simple-Tools/prefixAndPostfixConvertor.html 3/3