-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContest3.java
More file actions
172 lines (138 loc) · 3.32 KB
/
Contest3.java
File metadata and controls
172 lines (138 loc) · 3.32 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
package techgig;
import java.util.Stack;
class XNode<T> {
private T data;
private XNode<T> left;
private XNode<T> right;
private XNode<T> parent;
public void setData(T data) {
this.data = data;
}
public T getData() {
return data;
}
public boolean ifLeftExists() {
return (left != null);
}
public XNode<T> getLeft() {
return left;
}
public XNode<T> getRight() {
return right;
}
public boolean ifRightExists() {
return (right != null);
}
public XNode<T> addLeft(T data) {
left = new XNode<T>();
left.setData(data);
left.parent = this;
return left;
}
public XNode<T> addRight(T data) {
right = new XNode<T>();
right.setData(data);
right.parent = this;
return right;
}
public XNode<T> getParent() {
return this.parent;
}
public void setParent(XNode<T> node) {
this.parent = node;
}
}
class BinaryTree<T> {
private XNode<T> root;
private XNode<T> focusXNode;
BinaryTree(T data) {
root = new XNode<T>();
root.setData(data);
root.setParent(null);
focusXNode = root;
}
public XNode<T> getRoot() {
return root;
}
public XNode<T> getFocusXNode() {
return focusXNode;
}
public void setFocusXNode(XNode<T> node) {
focusXNode = node;
}
public void traverse(XNode<T> node) {
if (node.ifLeftExists()) {
traverse(node.getLeft());
}
if (node.ifRightExists()) {
traverse(node.getRight());
}
}
}
public class Contest3 {
private static StringBuilder infixToPostfix(String infix) {
infix = infix.trim();
Stack<Character> stack = new Stack<Character>();
StringBuilder strb = new StringBuilder("");
for (int i = 0; i < infix.length(); i++) {
Character ch = infix.charAt(i);
if (Character.isDigit(ch)) {
strb.append(ch);
} else if (ch == '+' || ch == '-' || ch == '*') {
stack.push(ch);
} else if (ch == ')') {
strb.append(stack.pop());
}
}
if (!stack.isEmpty()) {
while (!stack.isEmpty()) {
strb.append(stack.pop());
}
}
return strb;
}
private static int calculate(XNode<Character> node) {
if (Character.isDigit(node.getData())) {
return Integer.parseInt(node.getData() + "");
} else {
int left = calculate(node.getLeft());
int right = calculate(node.getRight());
if (node.getData() == '+') {
return left + right;
} else if (node.getData() == '-') {
return left - right;
} else if (node.getData() == '*') {
return left * right;
}
}
return 0;
}
public static void main(String[] args) {
StringBuilder postfix = infixToPostfix("((4+3)*9)+1");
postfix = postfix.reverse();
String reverse = postfix.toString();
BinaryTree<Character> bt = new BinaryTree<Character>(reverse.charAt(0));
// make Tree
for (int i = 1; i < reverse.length(); i++) {
Character ch = reverse.charAt(i);
if (ch == '+' || ch == '-' || ch == '*') {
if (!bt.getFocusXNode().ifRightExists()) {
bt.setFocusXNode(bt.getFocusXNode().addRight(ch));
} else {
bt.setFocusXNode(bt.getFocusXNode().addLeft(ch));
}
} else if (Character.isDigit(ch)) {
if (!bt.getFocusXNode().ifRightExists()) {
bt.getFocusXNode().addRight(ch);
} else {
bt.getFocusXNode().addLeft(ch);
bt.setFocusXNode(bt.getFocusXNode().getParent());
}
}
}
// display tree
//bt.traverse(bt.getRoot());
// calculate expression using recursion
System.out.println(calculate(bt.getRoot()));
}
}