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 | /*
* Usage:
* import * as CodeGuessing16 from path/to/index.mjs
* CodeGuessing16.entry(args)
*/
//export { entry }
var BRANCH = 'Λ'
class BinaryTreeNode {
constructor() {
this.left = null;
this.right = null;
}
}
function zip(l1, l2) {
return l1.map(function(k, i) {
return [k, l2[i]]
})
}
function fromPrefix(input) {
stack = []
for(var i = input.length-1; i >= 0; i--) {
if(input[i] == '`') {
var node = new BinaryTreeNode()
node.right = stack.pop()
node.left = stack.pop()
stack.push(node)
} else {
stack.push(input[i])
}
}
return stack.pop()
}
function fromPostfix(input) {
stack = []
for(var i = 0; i < input.length; i++) {
if(input[i] == ',') {
var node = new BinaryTreeNode()
node.left = stack.pop()
node.right = stack.pop()
stack.push(node)
} else {
stack.push(input[i])
}
}
return stack.pop()
}
function fromNestedHelper(tree) {
var node = new BinaryTreeNode()
if(!Array.isArray(tree))
return tree
node.right = fromNestedHelper(tree[0])
node.left = fromNestedHelper(tree[1])
return node
}
function fromNested(input) {
var tree = eval(input)
return fromNestedHelper(tree)
}
function fromASCIIHelper(grid, row, col) {
if(grid[row][col] !== BRANCH) {
return grid[row][col]
}
var node = new BinaryTreeNode()
var leftR = row + 1
var leftC = col + 1
while(grid[leftR][leftC] == '\\') {
leftR += 1
leftC += 1
}
console.log(leftR, leftC, grid[leftR][leftC])
node.left = fromASCIIHelper(grid, leftR, leftC)
var rightR = row + 1
var rightC = col - 1
while(grid[rightR][rightC] == '/') {
rightR += 1
rightC -= 1
}
node.right = fromASCIIHelper(grid, rightR, rightC)
return node
}
function fromASCII(input) {
var grid = input.split("\n")
console.log(grid)
var col = grid[0].indexOf("Λ")
console.log(col)
return fromASCIIHelper(grid, 0, col)
}
function toPrefix(tree) {
if(tree instanceof BinaryTreeNode) {
//console.log('happening', tree.left, tree.right)
return '`' + toPrefix(tree.left) + toPrefix(tree.right)
}
return tree
}
function toPostfix(tree) {
if(tree instanceof BinaryTreeNode) {
return toPrefix(tree.left) + toPrefix(tree.right) + ','
}
return tree
}
function toNested(tree) {
if(tree instanceof BinaryTreeNode)
return "[" + toNested(tree.left) + ", " + toNested(tree.right) + "]"
return "'" + tree + "'"
}
function depth(tree) {
if(tree instanceof BinaryTreeNode)
return 1 + Math.max(depth(tree.left), depth(tree.right))
return 0
}
function toASCII(tree) {
var dep = depth(tree)
var dim = Math.pow(2, dep+2)
var grid = []
for(var i = 0; i < dim - 1; i++)
grid.push(Array(dim).fill(' '))
function helper(tree, depth, row, col) {
if(tree instanceof BinaryTreeNode) {
grid[row][col] = BRANCH;
var len = Math.floor(Math.pow(2, depth+1)-1)
for(var i = 1; i <= len; i++) {
grid[row+i][col+i] = '\\'
grid[row+i][col-i] = '/'
}
helper(tree.right, depth - 1, row+len+1, col+len+1)
helper(tree.left, depth - 1, row+len+1, col-(len+1))
} else {
grid[row][col] = tree
}
}
helper(tree, dep-1, 0, grid[0].length/2)
// for(i of grid) {
// console.log(i.join(""))
// }
return grid.map(function(line) {
return line.join("")
}).join("\n")
}
function graphical(tree, canvasID, scale) {
var cnv = document.getElementById(canvasID)
var ctx = cnv.getContext("2d")
ctx.font = scale + 'px serif'
ctx.textAlign = 'center'
ctx.textBaseline = 'middle'
var dep = depth(tree)
function helper(tree, depth, x, y) {
if(tree instanceof BinaryTreeNode) {
var len = Math.floor(scale*Math.pow(2, depth+1)-1)
ctx.beginPath()
// console.log(x,y)
ctx.moveTo(x,y)
ctx.lineTo(x-len, y+len)
ctx.moveTo(x+len, y+len)
ctx.lineTo(x,y)
ctx.stroke()
ctx.closePath()
// console.log(tree)
helper(tree.right, depth-1, x+len, y+len)
helper(tree.left, depth-1, x-len, y+len)
} else {
ctx.beginPath()
ctx.arc(x, y, scale, 0, 2 * Math.PI)
ctx.fillStyle = "white"
ctx.fill()
ctx.stroke()
ctx.closePath()
ctx.fillStyle = "black"
ctx.fillText(tree, x, y)
}
}
helper(tree, dep, cnv.width/2, 0)
}
function entry(input, inputType = 'nestedArray', outputType = 'nestedArray', canvasID = "tree", scale = 10) {
var inputTypes = {
'prefix': fromPrefix,
'postfix': fromPostfix,
'nestedArray': fromNested,
'ASCIIArt': fromASCII
}
var tree = inputTypes[inputType](input)
var outputTypes = {
'prefix': toPrefix,
'postfix': toPostfix,
'nestedArray': toNested,
'ASCIIArt': toASCII
}
if(outputType == 'graphical') {
graphical(tree, canvasID, scale)
return undefined
}
return outputTypes[outputType](tree)
}
|
post a comment