Open In App

Java Program to Implement Shunting Yard Algorithm

Last Updated : 17 Apr, 2024
Comments
Improve
Suggest changes
4 Likes
Like
Report

The shunting yard algorithm is used to convert the infix notation to reverse Polish notation. The postfix notation is also known as the Reverse Polish Notation (RPN). The algorithm was named a “Shunting yard” because its activity is similar to a railroad shunting yard. It is a method for representing expressions in which the operator symbol is placed after the arguments being operated on. Polish notation, in which the operator comes before the operands. Australian philosopher and computer scientist suggested placing the operator after the operands and hence created reverse Polish notation.  Dijkstra developed this algorithm

Representation and Interpretation:

Brackets are not required to represent the order of evaluation or grouping of the terms. RPN expressions are simply evaluated from left to right and this greatly simplifies the computation of the expression within computer programs. As an example, the arithmetic expression.

Interpreting from left to right the following two executions can be performed

  1. If the value appears next in the expression push the current value in the stack.
  2. Now, if the operator appears next, pop the topmost two elements from the stack, execute the operation, and push back the result into the stack.

The order of precedence of operators is:

OperatorOrder of precedence

^

3

/

2

*

2

+

1

-

1

Illustration: RPN expression will produce the sum of 2 and 3, namely 5: 2 3 +

Input: (3+4)*5
Output: 34+5*

This is the postfix notation of the above infix notation

Concepts Involved:

Function of stacksActions performed in the stacks
push()To insert an element in the stack
pop()To remove the current topmost element from the stack 
peek()To fetch the top element of the stack
isEmpty()To check if the stack is empty or not
IsFull()To check if the stack is empty or not

Examples: 

Infix Notation:   a+b*(c^d-e)^(f+g*h)-i 
Postfix Notation: abcd^e-fgh*+^*+i- 

Algorithm: AE is the arithmetic expression written in infix notation PE will be the postfix expression of AE 

  1. Push “(“ onto Stack, and add “)” to the end of AE.
  2. Scan AE from left to right and repeat Step 3 to 6 for each element of AE until the Stack is empty.
  3. If an operand is encountered, append it to PE.
  4. If a left parenthesis is encountered, push it onto Stack.
  5. If an operator is encountered, then: Repeatedly pop from Stack and append to PE each operator which has the same precedence as or higher precedence than the operator. Add an operator to Stack. [End of if]
  6. If a right parenthesis is encountered, then: Repeatedly pop from Stack and append to PE each operator until a left parenthesis is encountered.     Remove the left Parenthesis.  [End of If] [End of If]

Applying the same above algorithms for two examples given below:

Example 1 : Applying Shunting yard algorithm on the expression "1 + 2" 

Step 1: Input “1 + 2”
Step 2: Push 1 to the output queue
Step 3: Push + to the operator stack, because + is an operator.
Step 4: Push 2 to the output queue
Step 5: After reading the input expression, the output queue and operator stack pop the expression and then add them to the output.
Step 6: Output “1 2 +”

Example 2: Applying the Shunting yard algorithm on the expression 5 + 2 / (3- 8) ^ 5 ^ 2 

TokenActionStackoutput
5"5" add token to output 5
+Push token to stack+5
2"2" add token to output+5 2
/Push token to stack+/5 2
(Push token to stack+/(5 2
3"3" add token to output+/(5 2 3
-Push token to stack+/(-5 2 3
8"8" add token to output+/(-5 2 3 8
)Pop stack to output+/5 2 3 8 -
^Push token to stack+/^5 2 3 8 -
5"5" add token to output+/^5 2 3 8 – 5
^Push token to stack+/^5 2 3 8 - 5^
2"2" add token to output+/^5 2 3 8 - 5 ^ 2
EndPop whole stack 5238-5^2^/+

Implementing: Shunting Yard Algorithm 


Output
5238-52^^/+

Complexitu of the above Method:

  • Time Complexity: O(n) This algorithm takes linear time, as we only traverse through the expression once and pop and push only take O(1).
  • Space Complexity: O(n) as we use a stack of size n, where n is length given of expression.
     

Next Article

Similar Reads