Repository navigation
Expand file tree
/
Copy pathbinary.js
More file actions
385 lines (342 loc) · 11.1 KB
/
Copy pathbinary.js
File metadata and controls
385 lines (342 loc) · 11.1 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
var ProgressStage = {
OPENBRACKET: 1, //(
CONSTRUCTOR: 2, //MAKE
IDENTIFIER: 3, //NODE
ARGUMENTS: 4, //VAL KEY LEFT RIGHT
CLOSEBRACKET: 5
}
function processFinite(block, identity){
var thisProcessStage = ProgressStage.OPENBRACKET;
var constructor = "";
var identifier = "";
var arguments = [];
var currentArgument = 0;
var numOpenBrackets = 0;
var hadSpace = false;
var closingMake = false;
var empty ="";
for (var i = 0, s = block.length; i < s; i++){
var c = block[i];
switch(thisProcessStage){
case ProgressStage.OPENBRACKET:
if (c == " "){
continue;
} else if (c != "("){
throw "Error: Bad Syntax";
} else {
numOpenBrackets++;
thisProcessStage = ProgressStage.CONSTRUCTOR;
console.log(thisProcessStage);
}
break;
case ProgressStage.CONSTRUCTOR:
if (c == " "){
continue;
} else if (constructor == "make"){
closingMake = true;
thisProcessStage = ProgressStage.IDENTIFIER;
} else if (constructor.length > 4){
throw "Error: not a constructor";
} else {
constructor += c;
}
break;
case ProgressStage.IDENTIFIER:
if (c == " "){
if (identifier == identity){
thisProcessStage = ProgressStage.ARGUMENTS;
} else {
console.log(identifier);
throw "Error: Identifier undefined";
}
} else if (c == ")"){
throw "Error: Syntax / No Arguments Found";
} else {
identifier += c;
}
break;
case ProgressStage.ARGUMENTS:
if (arguments.length == currentArgument){
arguments[currentArgument]="";
}
if (c == " "||c == "\n"){
hadSpace = true;
} else if (c == ")"){
numOpenBrackets--;
if (numOpenBrackets == 0){
break;
} else {
thisProcessStage = ProgressStage.CLOSEBRACKET;
}
} else if (c == "("){
numOpenBrackets++;
identifier = "";
constructor = "";
currentArgument++;
thisProcessStage = ProgressStage.CONSTRUCTOR;
} else{
if (hadSpace == true && arguments[currentArgument].length!=0){
console.log(arguments[currentArgument]);
arguments[currentArgument] += ","+c;
hadSpace = false;
} else {
arguments[currentArgument] += c;
}
}
break;
case ProgressStage.CLOSEBRACKET:
if (c == " " || c == "\n"){
continue;
}else if (c == "("){
numOpenBrackets++;
identifier = "";
constructor = "";
currentArgument++;
thisProcessStage = ProgressStage.CONSTRUCTOR;
}else if (c == ")"){
numOpenBrackets--;
if (numOpenBrackets == 0){
break;
}
}else {
empty += c;
if (empty == "empty"){
currentArgument++;
arguments[currentArgument] = "empty";
empty = "";
}
}
}
}
console.log(constructor);
console.log(identifier);
console.log(arguments);
//removes the first "comma" left over from previous parsing
for (var i = 0; i < arguments.length; i++){
var arg = arguments[i];
var new_argument = "";
var shouldDelete = true;
for (var j = 0; j < arg.length; j++){
if (arg.charAt(j) == ',' && shouldDelete == true){
continue;
}else{
new_argument += arg.charAt(j);
if (isAlphaNumeric(arg.charAt(j))){
shouldDelete = false;
}
}
}
arguments[i] = new_argument;
}
console.log(arguments);
processFiniteArguments(arguments);
}
function processFiniteArguments(arguments){
var path = [];
var info = [];
path.push("0");
info[0] = new Array(arguments[0], path[0], 2, window.innerWidth, window.innerWidth/2, 30);
var count = 0;
for (var i = 1; i < arguments.length; i++){
info[i] = "";
}
for (var i = 0; i < arguments.length-1; i++){
var node = arguments[i];
var countEmpty = (node.match(/empty/g) || []).length;
var next = arguments[i+1];
if (countEmpty == 0){
//the next node is the left child
path[i+1] = path[i]+",0";
info[i+1] = new Array(next, path[i+1], 2, window.innerWidth, window.innerWidth, window.innerHeight);
} else if (countEmpty == 1){
//the next node is the right child unless it's completely empty
if(node == "empty"){
//meaning the right side of arguments[i-1] is empty (filled); thus the very next argument has to find the first element with 0 empty while count == 0;
if(next == "empty"){
count++;
continue;
}else{
var tmp = count;
for (var j = i-1; j >= 0; j--){
var prev = arguments[j];
var countEmptyII = (prev.match(/empty/g) || []).length;
if(countEmptyII == 0){
if (count == 0){
path[i+1] = info[j][1] + ",1";
info[i+1] = new Array(next, path[i+1], 2, window.innerWidth, window.innerWidth, window.innerHeight);
arguments[j] = arguments[j] + ",empty";
console.log(arguments[j]);
count = tmp;
break;
}else{
count--;
}
}
}
}
}else{
path[i+1] = path[i]+",1";
info[i+1] = new Array(next, path[i+1], 2, window.innerWidth, window.innerWidth, window.innerHeight);
}
} else if (countEmpty == 2){
//this is a leaf node; the next node has to find the first one "empty" in its data; because it will be its right child;
if(next == "empty"){
count++;
}else{
var tmp = count;
for (var j = i-1; j >= 0; j--){
var prev = arguments[j];
var countEmptyII = (prev.match(/empty/g) || []).length;
if(countEmptyII == 0){
if (count == 0){
path[i+1] = info[j][1] + ",1";
info[i+1] = new Array(next, path[i+1], 2, window.innerWidth, window.innerWidth, window.innerHeight);
arguments[j] = arguments[j] + ",empty";
console.log(arguments[j]);
count = tmp;
break;
}else{
count--;
}
}
}
}
} else {
throw "Error: incorrect number of fields";
}
}
console.log(info);
var newInfo = [];
var j = 0;
for (var i = 0; i < info.length; i++){
if(info[i] != ""){
newInfo[j] = info[i];
j++;
}
}
console.log(newInfo);
getXYCoordinatesFinite(newInfo);
}
function getXYCoordinatesFinite(info){
var height = 0;
for (var n = 1; n < info.length; n++){
height = Math.max(height, info[n][1].length);
}
height = (height+1)/2; //returns height of tree
var layerHeight = window.innerHeight/height;
var defaultHeight = 30;
for (var i = 1; i < info.length; i++){
if (info[i][1].length == info[i-1][1].length+2){
//meaning this node is the previous nodes' child
var siblings = info[i][2];
var index = parseInt(info[i][1].charAt(info[i][1].length-1));
var layer = (info[i][1].length + 1)/2 - 1;
var width = info[i-1][3];
var x_coordinate = info[i-1][4];
var occupy_width = width/siblings;
var new_x = x_coordinate + (index - siblings/2 + 0.5)*occupy_width;
var y_coordinate = layer * layerHeight + defaultHeight;
info[i][3] = occupy_width;
info[i][4] = new_x;
info[i][5] = y_coordinate;
}else if (info[i][1].length == info[i-1][1].length){ //meaning this node is previous nodes' sibling node
var index = parseInt(info[i][1].charAt(info[i][1].length-1));
var layer = (info[i][1].length + 1)/2 - 1;
var width = info[i-1][3];
var x_coordinate = info[i-1][4];
var new_x = x_coordinate+width;
var y_coordinate = layer * layerHeight + defaultHeight;
info[i][3] = width;
info[i][4] = new_x;
info[i][5] = y_coordinate;
}else{
//meaning this layer is done; need to find the nodes' sibling
for (var j = 1; j < info.length; j++){
if (info[j][1].length == info[i][1].length &&
info[j][1].charAt(info[j][1].length-3) ==
info[i][1].charAt(info[i][1].length-3)){
var index = parseInt(info[i][1].charAt(info[i][1].length-1));
var layer = (info[i][1].length + 1)/2 - 1;
var width = info[j][3];
var x_coordinate = info[j][4];
console.log(info[j]);
var new_x = x_coordinate+width;
var y_coordinate = layer * layerHeight + defaultHeight;
info[i][3] = width;
info[i][4] = new_x;
info[i][5] = y_coordinate;
break;
}
}
}
}
console.log(info);
drawTreeFinite(info);
}
//lstnodes passed in is an ordered pair of destination x and y values
function drawTreeFinite(info){
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "white";
ctx.fill();
for (var i = 1; i < info.length; i++){
if (info[i][1].length == info[i-1][1].length+2){
//the child of previous node
ctx.beginPath();
ctx.moveTo(info[i-1][4], info[i-1][5]);
ctx.lineTo(info[i][4], info[i][5]);
ctx.stroke();
}else {
//finds the parent of this node (shud be else where)
for (var j = 0; j < info.length; j++){
if (info[i][1].length == info[j][1].length+2 && info[i][1].substring(0,info[j][1].length) === info[j][1]){
//parent is found
ctx.beginPath();
ctx.moveTo(info[j][4], info[j][5]);
ctx.lineTo(info[i][4], info[i][5]);
ctx.stroke();
break;
}
}
}
}
for (var i = 1; i < info.length; i++){
ctx.beginPath();
ctx.arc(info[i][4], info[i][5], 20, 0, 2*Math.PI);
ctx.fillStyle = "white";
ctx.fill();
ctx.stroke();
}
for (var i = 0; i < info.length; i++){
var index = info[i][0].indexOf("empty");
if (index == -1){
continue;
}else {
info[i][0] = info[i][0].substring(0, index-1);
}
}
for (var i = 1; i < info.length; i++){
ctx.font = "18px Times New Roman";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx. fillText(info[i][0], info[i][4], info[i][5]);
}
ctx.beginPath();
ctx.arc(info[0][4], info[0][5], 20, 0, 2*Math.PI);
ctx.fillStyle = "white";
ctx.fill();
ctx.stroke();
ctx.font = "18px Times New Roman";
ctx.fillStyle = "black";
ctx.textAlign = "center";
ctx. fillText(info[0][0], info[0][4], info[0][5]);
}
//helper function
function isAlphaNumeric (letter){
var a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
if (a.indexOf(letter) == -1){
return false;
}else{
return true;
}
}