-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
294 lines (239 loc) · 8.74 KB
/
BinaryTree.java
File metadata and controls
294 lines (239 loc) · 8.74 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
package Automata_DFA;
import java.util.*;
class BinaryTree {
/*
***
(a|b)*a => creating binary syntax tree:
.
/ \
* a
/
|
/ \
a b
***
*/
private int leafNodeID = 0;
// Stacks for symbol nodes and operators
private Stack<Node> stackNode = new Stack<>();
private Stack<Character> operator = new Stack<Character>();
// Set of inputs
private Set<Character> input = new HashSet<Character>();
private ArrayList<Character> op = new ArrayList<>();
// Generates tree using the regular expression and returns it's root
public Node generateTree(String regular) {
Character[] ops = {'*', '|', '&'};
op.addAll(Arrays.asList(ops));
// Only inputs available
Character ch[] = new Character[26 + 26];
for (int i = 65; i <= 90; i++) {
ch[i - 65] = (char) i;
ch[i - 65 + 26] = (char) (i + 32);
}
Character integer[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
Character others[] = {'#','\\', '=', '_', '.', '*', '/', '+', '-', ' ', '(', ')'};
input.addAll(Arrays.asList(ch));
input.addAll(Arrays.asList(integer));
input.addAll(Arrays.asList(others));
// Generate regular expression with the concatenation
regular = AddConcat(regular);
// Cleaning stacks
stackNode.clear();
operator.clear();
// Flag which is true when there is something like: \( or \* or etc
boolean isSymbol = false;
for (int i = 0; i < regular.length(); i++) {
if (regular.charAt(i) == '\\') {
isSymbol = true;
continue;
}
if (isSymbol || isInputCharacter(regular.charAt(i))) {
if (isSymbol) {
//create a node with "\{symbol}" symbol
pushStack("\\"+Character.toString(regular.charAt(i)));
}
else{
pushStack(Character.toString(regular.charAt(i)));
}
isSymbol = false;
} else if (operator.isEmpty()) {
operator.push(regular.charAt(i));
} else if (regular.charAt(i) == '(') {
operator.push(regular.charAt(i));
} else if (regular.charAt(i) == ')') {
while (operator.get(operator.size() - 1) != '(') {
doOperation();
}
// Pop the '(' left parenthesis
operator.pop();
} else {
while (!operator.isEmpty()
&& Priority(regular.charAt(i), operator.get(operator.size() - 1))) {
doOperation();
}
operator.push(regular.charAt(i));
}
}
// Clean the remaining elements in the stack
while (!operator.isEmpty()) {
doOperation();
}
// Get the complete Tree
Node completeTree = stackNode.pop();
return completeTree;
}
private boolean Priority(char first, Character second) {
if (first == second) {
return true;
}
if (first == '*') {
return false;
}
if (second == '*') {
return true;
}
if (first == '&') {
return false;
}
if (second == '&') {
return true;
}
if (first == '|') {
return false;
}
return true;
}
// Do the desired operation based on the top of stackNode
private void doOperation() {
if (this.operator.size() > 0) {
char charAt = operator.pop();
switch (charAt) {
case ('|'):
union();
break;
case ('&'):
concatenation();
break;
case ('*'):
star();
break;
default:
System.out.println(">>" + charAt);
System.out.println("Unkown Symbol !");
System.exit(1);
break;
}
}
}
// Do the star operation
private void star() {
// Retrieve top Node from Stack
Node node = stackNode.pop();
Node root = new Node("*");
root.setLeft(node);
root.setRight(null);
node.setParent(root);
// Put node back in the stackNode
stackNode.push(root);
}
// Do the concatenation operation
private void concatenation() {
// retrieve node 1 and 2 from stackNode
Node node2 = stackNode.pop();
Node node1 = stackNode.pop();
Node root = new Node("&");
root.setLeft(node1);
root.setRight(node2);
node1.setParent(root);
node2.setParent(root);
// Put node back to stackNode
stackNode.push(root);
}
// Makes union of sub Node 1 with sub Node 2
private void union() {
// Load two Node in stack into variables
Node node2 = stackNode.pop();
Node node1 = stackNode.pop();
Node root = new Node("|");
root.setLeft(node1);
root.setRight(node2);
node1.setParent(root);
node2.setParent(root);
// Put Node back to stack
stackNode.push(root);
}
// Push input symbol into stackNode
private void pushStack(String symbol) {
Node node = new LeafNode(symbol, ++leafNodeID);
node.setLeft(null);
node.setRight(null);
// Put Node back to stackNode
stackNode.push(node);
}
// add "." when is concatenation between to symbols that: "." -> "&"
// concatenates to each other
private String AddConcat(String regular) {
String newRegular = new String("");
for (int i = 0; i < regular.length() - 1; i++) {
/*
*# consider a , b are characters in the Σ
*# and the set: {'(', ')', '*', '+', '&', '|'} are the operators
*# then, if '&' is the concat symbol, we have to concatenate such expressions:
*# a & b
*# a & (
*# ) & a
*# * & a
*# * & (
*# ) & (
*/
if (regular.charAt(i) == '\\' && isInputCharacter(regular.charAt(i + 1))) {
newRegular += regular.charAt(i);
} else if (regular.charAt(i) == '\\' && regular.charAt(i + 1) == '(') {
newRegular += regular.charAt(i);
} else if ((isInputCharacter(regular.charAt(i)) || (regular.charAt(i) == '(' && i > 0 && regular.charAt(i - 1) == '\\')) && isInputCharacter(regular.charAt(i + 1))) {
newRegular += regular.charAt(i) + "&";
} else if ((isInputCharacter(regular.charAt(i)) || (regular.charAt(i) == '(' && i > 0 && regular.charAt(i - 1) == '\\')) && regular.charAt(i + 1) == '(') {
newRegular += regular.charAt(i) + "&";
} else if (regular.charAt(i) == ')' && isInputCharacter(regular.charAt(i + 1))) {
newRegular += regular.charAt(i) + "&";
} else if (regular.charAt(i) == '*' && isInputCharacter(regular.charAt(i + 1))) {
newRegular += regular.charAt(i) + "&";
} else if (regular.charAt(i) == '*' && regular.charAt(i + 1) == '(') {
newRegular += regular.charAt(i) + "&";
} else if (regular.charAt(i) == ')' && regular.charAt(i + 1) == '(') {
newRegular += regular.charAt(i) + "&";
} else {
newRegular += regular.charAt(i);
}
}
newRegular += regular.charAt(regular.length() - 1);
return newRegular;
}
// Return true if is part of the automata Language else is false
private boolean isInputCharacter(char charAt) {
if (op.contains(charAt)) {
return false;
}
for (Character c : input) {
if ((char) c == charAt && charAt != '(' && charAt != ')') {
return true;
}
}
return false;
}
/* This method is here just to test buildTree() */
public void printInorder(Node node) {
if (node == null) {
return;
}
/* first recur on left child */
printInorder(node.getLeft());
/* then print the data of node */
System.out.print(node.getSymbol() + " ");
/* now recur on right child */
printInorder(node.getRight());
}
public int getNumberOfLeafs(){
return leafNodeID;
}
}