-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathInfixtoPrefix.java
151 lines (140 loc) · 4.12 KB
/
InfixtoPrefix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Infix to Prefix Expression
*
* Algorithm Used:
* First the given infix expression is reversed.
* By reversing, every '(' becomes ')' and vice versa.
* Then, we obtain the postfix of the reversed expression.
* Now, we reverse this postfix expression to obtain the Prefix expression.
*
* Author: iamvs-2002
*/
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
import java.util.Stack;
public class InfixtoPrefix
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Kindly enter the infix expression: ");
String infix = sc.nextLine();
InfixtoPrefix x = new InfixtoPrefix();
String prefix = x.infixtoprefix(infix);
System.out.println("Prefix Expression: "+prefix);
}
static int priority(char c)
{
/**
* '+' and '-' have priority=1
* '*' and '/' have priority=2
* '^' has priority=3
*/
switch (c)
{
case '+':
return 1;
case '-':
return 1;
case '*':
return 2;
case '/':
return 2;
case '^':
return 3;
default:
return -1;
}
}
private String infixtoprefix(String expression)
{
//initially an empty string
String result = "";
//reversing the given infix expression
char[] array = new char[expression.length()];
for (int j = expression.length()-1; j >=0 ; j--) {
array[expression.length()-j-1] = expression.charAt(j);
}
Stack<Character> stack = new Stack<>();
int i=0;
for (i = 0; i < array.length; i++)
{
if (array[i] == '(')
{
array[i] = ')';
i++;
}
else if (array[i] == ')')
{
array[i] = '(';
i++;
}
}
String infix = '(' + String.valueOf(array) + ')';
for (i = 0; i < infix.length() ; i++)
{
char c = infix.charAt(i);
//check if char is operator or operand
if(priority(c)>0)
{
//if the topmost operator in the stack has a priority greater than c
//pop it and add to result string
//else push it into the stack
while(stack.isEmpty()==false && (priority(stack.peek())>priority(c) || (priority(stack.peek())>=priority(c) && c=='^')))
{
result = result + stack.pop();
}
stack.push(c);
}
else if(c==')')
{
//if character is ending bracket ')'
//pop from stack, until '(' is found
while(stack.peek()!='(')
{
result = result + stack.peek();
stack.pop();
}
//removing '(' from stack
char x = stack.pop();
}
else if(c=='(')
{
//character is starting bracket '('
stack.push(c);
}
else
{
//if the character is an operand, add it to the result string
result = result + c;
}
}
//the result string is now a postfix expression
//reversing the string to obtain the prefix expression.
//converting the final result string to a char array
char[] arr = new char[result.length()];
for (i = result.length()-1; i >=0 ; i--) {
arr[result.length()-i-1] = result.charAt(i);
}
return String.valueOf(arr);
}
}
/**
* Sample Outputs:
*
* Kindly enter the infix expression:
* A+B*C
* Prefix Expression: +A*BC
*
* Kindly enter the infix expression:
* x+y*z/w+u
* Prefix Expression: ++x/*yzwu
*
* Kindly enter the infix expression:
* A * B + C / D
* Prefix Expression: +*A B / C D
*
* Time Complexity: O(n^2)
* Space Complexity: O(n)
*/