-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionProcessor.java
More file actions
408 lines (355 loc) · 13.9 KB
/
ExpressionProcessor.java
File metadata and controls
408 lines (355 loc) · 13.9 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
public class ExpressionProcessor {
// STEP 1: Tokenization
public static Queue<String> tokenize(String expr) {
Queue<String> queue = new Queue<>();
expr = expr.replaceAll("\\s+", ""); // Remove spaces
StringBuilder numBuffer = new StringBuilder(); // Temporary multi-digit number
boolean expectSign = true; // Flag for negative numbers
// Read the full string
for (int i = 0; i < expr.length(); i++) {
char c = expr.charAt(i);
// If the number isDigit or decimal add to buffer to be added together
if (Character.isDigit(c) || c == '.') {
numBuffer.append(c);
expectSign = false;
// If the number is negative
} else if (c == '-' && expectSign) {
numBuffer.append(c);
expectSign = false;
// If it's an operation
} else {
if (!numBuffer.isEmpty()) {
queue.add(numBuffer.toString()); // Add the number in the buffer to the queue
numBuffer.setLength(0); // Empty the buffer
}
queue.add(String.valueOf(c)); // add the operation to the queue
expectSign = (c == '(' || isOperator(String.valueOf(c))); // Set the expectSign to true if "(" or digit
}
}
if (!numBuffer.isEmpty())
queue.add(numBuffer.toString()); // Add the last number
return queue;
}
// STEP 2: Infix to Postfix
public static Queue<String> infixToPostfix(Queue<String> infixQueue) {
Queue<String> postfixQueue = new Queue<>();
Stack<String> operatorStack = new Stack<>();
while (!infixQueue.isEmpty()) {
String token = infixQueue.poll();
// If it's a number add it to the queue
if (isNumeric(token)) {
postfixQueue.add(token);
// If it's "(" push it to the stack
} else if (token.equals("(")) {
operatorStack.push(token);
// If it's ")" add all the operator between the "(...)" into the queue
} else if (token.equals(")")) {
while (!operatorStack.isEmpty() && !operatorStack.peek().equals("(")) {
postfixQueue.add(operatorStack.pop());
}
operatorStack.pop();
// If it's an operator we compare the operators
} else if (isOperator(token)) {
while (!operatorStack.isEmpty() && getPrecedence(operatorStack.peek()) >= getPrecedence(token)) {
postfixQueue.add(operatorStack.pop());
}
operatorStack.push(token);
}
}
while (!operatorStack.isEmpty())
postfixQueue.add(operatorStack.pop()); // Empty the stack
return postfixQueue;
}
// STEP 3: Evaluation
public static double evaluatePostfix(Queue<String> postfixQueue) {
Stack<Double> stack = new Stack<>();
while (!postfixQueue.isEmpty()) {
String token = postfixQueue.poll();
// If it's number push it to the stack
if (isNumeric(token)) {
stack.push(Double.parseDouble(token)); // Convert the string to number
// If it's an operator pop the last two and applyOperation
} else {
double b = stack.pop();
double a = stack.pop();
if (token.equals("/") && b == 0)
throw new ArithmeticException("Division by zero");
stack.push(applyOp(a, b, token));
}
}
return stack.pop();
}
// STEP 4: Expression Tree & Traversals
public static Node buildTree(Queue<String> postfixQueue) {
Stack<Node> stack = new Stack<>();
while (!postfixQueue.isEmpty()) {
String token = postfixQueue.poll();
Node newNode = new Node(token);
if (!isNumeric(token)) {
newNode.right = stack.pop();
newNode.left = stack.pop();
}
stack.push(newNode);
}
return stack.pop();
}
// preorder
public static void preorder(Node root) {
if (root == null)
return;
System.out.print(root.value + " ");
preorder(root.left);
preorder(root.right);
}
// inorder
public static void inorder(Node root) {
if (root == null)
return;
inorder(root.left);
System.out.print(root.value + " ");
inorder(root.right);
}
// postorder
public static void postorder(Node root) {
if (root == null)
return;
postorder(root.left);
postorder(root.right);
System.out.print(root.value + " ");
}
// preorder returning string
public static String preorderString(Node root) {
if (root == null)
return "";
return root.value + " " + preorderString(root.left) + preorderString(root.right);
}
// inorder returning string
public static String inorderString(Node root) {
if (root == null)
return "";
return inorderString(root.left) + root.value + " " + inorderString(root.right);
}
// postorder returning string
public static String postorderString(Node root) {
if (root == null)
return "";
return postorderString(root.left) + postorderString(root.right) + root.value + " ";
}
// Extract all numbers from tokenized expression
public static Queue<Double> extractNumbers(Queue<String> tokens) {
Queue<Double> numbers = new Queue<>();
Queue<String> copy = new Queue<>(tokens);
while (!copy.isEmpty()) {
String token = copy.poll();
if (isNumeric(token)) {
numbers.add(Double.parseDouble(token));
}
}
return numbers;
}
// Validate expression before processing
public static boolean isValidExpression(String expr) {
try {
Queue<String> tokens = tokenize(expr);
if (tokens.isEmpty()) return false;
Queue<String> copy = new Queue<>(tokens);
infixToPostfix(copy);
copy = new Queue<>(tokens);
Queue<String> postfix = infixToPostfix(copy);
Queue<String> evalCopy = new Queue<>(postfix);
evaluatePostfix(evalCopy);
return true;
} catch (ArithmeticException e) {
return true;
} catch (Exception e) {
return false;
}
}
// Process a single expression and return formatted output string
public static String processExpression(String expr, int tableSize) {
Queue<String> tokens = tokenize(expr);
Queue<String> tokensCopyForPostfix = new Queue<>(tokens);
Queue<String> postfixQueue = infixToPostfix(tokensCopyForPostfix);
Queue<String> postfixCopyForEval = new Queue<>(postfixQueue);
Queue<String> postfixCopyForTree = new Queue<>(postfixQueue);
double result = evaluatePostfix(postfixCopyForEval);
StringBuilder sb = new StringBuilder();
sb.append("Output:\n");
sb.append("Queue: ").append(tokens).append("\n");
sb.append("Postfix: ").append(postfixQueue).append("\n");
if (result == (long) result) {
sb.append("Result: ").append((long) result).append("\n");
} else {
sb.append("Result: ").append(result).append("\n");
}
Node root = buildTree(postfixCopyForTree);
sb.append("\nBST Traversals:\n");
sb.append("Preorder: ").append(preorderString(root).trim()).append("\n");
sb.append("Inorder: ").append(inorderString(root).trim()).append("\n");
sb.append("Postorder: ").append(postorderString(root).trim()).append("\n");
Queue<Double> numbers = extractNumbers(tokens);
sb.append("\nExtracted Numbers: ").append(numbers).append("\n");
HashTable hashTable = new HashTable(tableSize);
boolean linearOk = true, quadraticOk = true, doubleOk = true;
Queue<Double> numCopy = new Queue<>(numbers);
while (!numCopy.isEmpty()) {
double val = numCopy.poll();
if (!hashTable.insertLinear(val)) linearOk = false;
if (!hashTable.insertQuadratic(val)) quadraticOk = false;
if (!hashTable.insertDoubleHash(val)) doubleOk = false;
hashTable.insertChaining(val);
}
sb.append("\nHash Tables (size=").append(tableSize).append("):\n");
String linearDisplay = hashTable.displayLinear();
if (!linearOk) linearDisplay += " NOT ENOUGH TABLE SPACE\n";
sb.append(linearDisplay);
String quadraticDisplay = hashTable.displayQuadratic();
if (!quadraticOk) quadraticDisplay += " NOT ENOUGH TABLE SPACE\n";
sb.append(quadraticDisplay);
String doubleDisplay = hashTable.displayDoubleHash();
if (!doubleOk) doubleDisplay += " NOT ENOUGH TABLE SPACE\n";
sb.append(doubleDisplay);
sb.append(hashTable.displayChaining());
return sb.toString();
}
// STEP 5: Hash Table
static class HashTable {
int size;
Double[] tableLinear;
Double[] tableQuadratic;
Double[] tableDouble;
LinkedList<Double>[] tableChaining;
public HashTable(int size) {
this.size = size;
tableLinear = new Double[size];
tableQuadratic = new Double[size];
tableDouble = new Double[size];
tableChaining = new LinkedList[size];
for (int i = 0; i < size; i++) {
tableChaining[i] = new LinkedList<>();
}
}
// Simple Hash Function
private int hash(double value) {
return Math.abs((int) Math.round(value) % size);
}
// Secondary Hash Function for Double Hashing
private int hash2(double value) {
return 7 - Math.abs((int) Math.round(value) % size);
}
// Linear Probing
public boolean insertLinear(double value) {
int index = hash(value);
int i = 0;
while (tableLinear[(index + i) % size] != null && i < size) {
i++;
}
if (i < size) {
tableLinear[(index + i) % size] = value;
return true;
}
return false;
}
// Quadratic Probing
public boolean insertQuadratic(double value) {
int index = hash(value);
int i = 0;
while (tableQuadratic[(index + i * i) % size] != null && i < size) {
i++;
}
if (i < size) {
tableQuadratic[(index + i * i) % size] = value;
return true;
}
return false;
}
// Double Hashing
public boolean insertDoubleHash(double value) {
int index = hash(value);
int step = hash2(value);
int i = 0;
while (tableDouble[(index + i * step) % size] != null && i < size) {
i++;
}
if (i < size) {
tableDouble[(index + i * step) % size] = value;
return true;
}
return false;
}
// Separate Chaining
public void insertChaining(double value) {
int index = hash(value);
tableChaining[index].add(value);
}
public String displayLinear() {
return displayOpenAddressing(tableLinear, "Linear Probing");
}
public String displayQuadratic() {
return displayOpenAddressing(tableQuadratic, "Quadratic Probing");
}
public String displayDoubleHash() {
return displayOpenAddressing(tableDouble, "Double Hashing");
}
public String displayChaining() {
return displayChainTable("Separate Chaining");
}
private String displayOpenAddressing(Double[] table, String label) {
StringBuilder sb = new StringBuilder();
sb.append(label).append(":\n ");
for (int i = 0; i < size; i++) {
String val = table[i] == null ? "null" : table[i].toString();
if (i == size - 1) {
sb.append(String.format("[%d]: %s\n", i, val));
} else {
sb.append(String.format("[%d]: %s ", i, val));
}
}
return sb.toString();
}
private String displayChainTable(String label) {
StringBuilder sb = new StringBuilder();
sb.append(label).append(":\n ");
for (int i = 0; i < size; i++) {
String val = tableChaining[i].count() == 0 ? "empty" : tableChaining[i].toString();
if (i == size - 1) {
sb.append(String.format("[%d]: %s\n", i, val));
} else {
sb.append(String.format("[%d]: %s ", i, val));
}
}
return sb.toString();
}
}
// Helper methods
private static boolean isOperator(String s) {
return s.matches("[+\\-*/%^]");
}
private static boolean isNumeric(String s) {
try {
Double.parseDouble(s);
return true;
} catch (Exception e) {
return false;
}
}
private static int getPrecedence(String op) {
return switch (op) {
case "^" -> 3;
case "*", "/", "%" -> 2;
case "+", "-" -> 1;
default -> 0;
};
}
private static double applyOp(double a, double b, String op) {
return switch (op) {
case "+" -> a + b;
case "-" -> a - b;
case "*" -> a * b;
case "/" -> a / b;
case "%" -> a % b;
case "^" -> Math.pow(a, b);
default -> 0;
};
}
}