Java Program to Implement Shunting Yard Algorithm
Last Updated :
17 Apr, 2024
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
- If the value appears next in the expression push the current value in the stack.
- 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:
Operator | Order 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 stacks | Actions 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
- Push “(“ onto Stack, and add “)” to the end of AE.
- Scan AE from left to right and repeat Step 3 to 6 for each element of AE until the Stack is empty.
- If an operand is encountered, append it to PE.
- If a left parenthesis is encountered, push it onto Stack.
- 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]
- 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
Token | Action | Stack | output |
---|
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 |
End | Pop whole stack | | 5238-5^2^/+ |
Implementing: Shunting Yard Algorithm
Java
// Java Implementation of Shunting Yard Algorithm
// Importing stack class for stacks DS
import java.util.Stack;
// Importing specific character class as
// dealing with only operators and operands
import java.lang.Character;
class GFG {
// Method is used to get the precedence of operators
private static boolean letterOrDigit(char c)
{
// boolean check
if (Character.isLetterOrDigit(c))
return true;
else
return false;
}
// Operator having higher precedence
// value will be returned
static int getPrecedence(char ch)
{
if (ch == '+' || ch == '-')
return 1;
else if (ch == '*' || ch == '/')
return 2;
else if (ch == '^')
return 3;
else
return -1;
}
// Operator has Left --> Right associativity
static boolean hasLeftAssociativity(char ch) {
if (ch == '+' || ch == '-' || ch == '/' || ch == '*') {
return true;
} else {
return false;
}
}
// Method converts given infixto postfix expression
// to illustrate shunting yard algorithm
static String infixToRpn(String expression)
{
// Initialising an empty String
// (for output) and an empty stack
Stack<Character> stack = new Stack<>();
// Initially empty string taken
String output = new String("");
// Iterating over tokens using inbuilt
// .length() function
for (int i = 0; i < expression.length(); ++i) {
// Finding character at 'i'th index
char c = expression.charAt(i);
// If the scanned Token is an
// operand, add it to output
if (letterOrDigit(c))
output += c;
// If the scanned Token is an '('
// push it to the stack
else if (c == '(')
stack.push(c);
// If the scanned Token is an ')' pop and append
// it to output from the stack until an '(' is
// encountered
else if (c == ')') {
while (!stack.isEmpty()
&& stack.peek() != '(')
output += stack.pop();
stack.pop();
}
// If an operator is encountered then taken the
// further action based on the precedence of the
// operator
else {
while (
!stack.isEmpty()
&& getPrecedence(c)
<= getPrecedence(stack.peek())
&& hasLeftAssociativity(c)) {
// peek() inbuilt stack function to
// fetch the top element(token)
output += stack.pop();
}
stack.push(c);
}
}
// pop all the remaining operators from
// the stack and append them to output
while (!stack.isEmpty()) {
if (stack.peek() == '(')
return "This expression is invalid";
output += stack.pop();
}
return output;
}
// Main driver code
public static void main(String[] args)
{
// Considering random infix string notation
String expression = "5+2/(3-8)^5^2";
// Printing RPN for the above infix notation
// Illustrating shunting yard algorithm
System.out.println(infixToRpn(expression));
}
}
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.
Similar Reads
Java Program to Implement of Gabow Scaling Algorithm Gabow's Algorithm is a scaling algorithm that aims in solving a problem by initially considering only the highest order bit of each relevant input value (such as an edge weight). Then it refines the initial solution by looking at the two highest-order bits. It progressively looks at more and more hi
4 min read
Java Program to Implement Bowyer-Watson Algorithm To gain some practical programming experience related to computational geometry topics, we will apply the Bowyer-Watson algorithm that will be able to facilitate the process to some extent. Bowyer-Watson AlgorithmThe Bowyer-Watson algorithm is a computational algorithm for constructing Delaunay tria
4 min read
Java Program to Implement VList VList is a data structure that combines fast indexing of arrays with the easy extension of singly-linked lists. VList generally supports the following functions. Insert ElementGet Element at index kClear ListDisplay ListEtc. VList has rows connected as Singly Linked List where the Nth row contains m
3 min read
Java Program to Implement Leftist Heap A leftist heap is a priority Queue implemented with a binary heap. Every node has a sValue which is at the nearest Distance to the other nodes. Now we will write a java program for performing certain operations on a leftist Heap (Inorder Traversal) like insert, delete, clear, and check for empty. A
7 min read
Java Program to Sort an array in wave form Given an unsorted array of integers, sort the array into a wave-like array. An array 'arr[0..n-1]' is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ..... Examples: Input: arr[] = {10, 5, 6, 3, 2, 20, 100, 80} Output: arr[] = {10, 5, 6, 2, 20, 3, 100, 80} OR
4 min read
Java Program to Implement Park-Miller Random Number Generation Algorithm ParkâMiller random number generator is also known as Lehmer random number generator. A general formula of a random number generator (RNG) of this type is, Xk+1 = a * xk mod m Where the modulus m is a prime number or a power of a prime number, the multiplier a is an element of high multiplicative ord
3 min read
Java Program for Iterative Quick Sort Below is the implementation of Iterative Quick Sort:Java// Java implementation of iterative quick sort import java.io.*; public class IterativeQuickSort { void swap(int arr[], int i, int j) { int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } /* This function is same in both iterative and recursive*/ in
3 min read
Java Program to Rotate an Image The problem statement is to rotate an image clockwise 90 degrees for which here we will be using some in-built methods of BufferedImage class and Color c Classes required to perform the operation is as follows: To read and write an image file we have to import the File class. This class represents f
3 min read
Java Program for Heap Sort Heap sort is a comparison-based sorting technique based on the Binary Heap data structure. It is similar to the selection sort where first find the maximum element and place it at the end. We repeat the same process for the remaining element. Heap Sort in JavaBelow is the implementation of Heap Sort
3 min read
Java Program to Find the Area of Rhombus A rhombus is a quadrilateral whose four sides all have the same length and a rhombus is also called an equilateral quadrilateral. The formula for finding the area of a rhombus is as follows: (product of the lengths of the diagonals)/2 Example: Input : p = 2, q = 3 Output: 3Input : p = 4, q = 5 Outpu
1 min read