-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.js
304 lines (232 loc) · 7.88 KB
/
bst.js
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
const nodeColor = 'white'; // color to fill the nodes with
const nodeBorder = 'black'; // color of the borders of the nodes
const nodeBorderWidth = 1; // name is self-explanatory
const nodeSize = 8; // radius of the nodes on the svg
const textOffset = 3; // make the nodes pretty
const maxDepth = 5; // the maximum depth of the binary tree - past this, nodes will go offscreen
const maxDatumValue = 99; // maximum absolute value of values in the BST
const deltaMove = 8; // how large should each step of the animation be? larger value -> faster animation
const unfinishedOffset = 20; // how far to offset nodes still being inserted into the BST
// helpful for working with IDs in the html file
const nodeIdPrefix = "bstVisNode";
const bstVisID = "bstvis";
const inputID = "valueInput";
const edgeIdPrefix = "bstEdge";
// constants for the workings of the BST
const nullNode = -1;
const VALUE = 0; // value stored in the node
const CHILD = 1; // children of the node
const VISID = 2; // the ID of the node in the html file
const COORD = 3; // where to draw the node
const EDGE = 4; // id of edge to parent
var datums = []; // stores the BST
var currID = 0; // ids to assign the nodes in the html file
var inAnim = false; // is the animation running?
var finished = false; // is the animation finished?
var toInsert = -1; // what is the value being inserted?
var currDatumInd = 0; // where in the BST are we right now?
var currNodeID = -1; // what is the ID of the node being inserted?
var cdep = 0; // what is the current depth?
var cacross = 0; // where in this depth level is the node?
var cx, cy, gx, gy; // helper variables for the animation
var pdep, pacc; // the parent of the current node
function vis() { return document.getElementById(bstVisID); }
// returns the html for a circle on the svg
function createCircle(x, y, r = nodeSize) {
var temp = '<circle cx=' + x +
' cy=' + y +
' r=' + r +
' stroke=' + nodeBorder +
' stroke-width=' + nodeBorderWidth +
' fill=' + nodeColor +
'></circle>';
return temp;
}
// returns the html to make text on the svg
function createText(x, y, textToMake) {
var temp = '<text x=' + x +
' y=' + (y + textOffset) +
' text-anchor=middle font-size=x-small>' +
textToMake + '</text>';
return temp;
}
// adds a node to the svg, returns its ID
function createNode(x, y, val) {
var displayNode = document.createElement('g');
displayNode.innerHTML += createCircle(x, y);
displayNode.innerHTML += createText(x, y, val);
displayNode.id = nodeIdPrefix + currID;
currID += 1;
vis().appendChild(displayNode);
vis().innerHTML += "";
return currID - 1;
}
// removes the specified element from the svg
function removeFromDoc(id) { document.getElementById(id).remove(); }
// takes in the depth and the position of the node at that depth
// returns the x/y coordinates
// finished - if not finished, moves the node up a bit
function getCoords(dep, which, finished = true) {
var numnodes = 2 ** dep;
var canvasWidth = document.getElementById(bstVisID).width.animVal.value;
var canvasHeight = document.getElementById(bstVisID).height.animVal.value;
var nv = numnodes + 1;
var x = (canvasWidth / nv) * (which + 1);
var y = (canvasHeight / (maxDepth + 2)) * (dep + 1);
if (finished == false)
y -= unfinishedOffset;
return [x, y];
}
// returns the information contained in a node
function newNode(val, id, dep, whichleft) {
return [val, // value stored
[-1, -1], // left, right children
id, // id of the node
[dep, whichleft], // where to draw it
false]; // does it have an edge connecting it to its parent?
}
// moves the specified element by the specified x and y
function moveThing(id, x, y) {
var container = document.getElementById(nodeIdPrefix + id);
if (container == null) return;
var transformNode = 'translate(' + x + ',' + y + ')';
container.setAttribute('transform', transformNode);
}
// geometry - a point dToWalk away from coords in the direction towards goal
function walkTo(coords, goal, dToWalk) {
var tdist = ((goal[1] - coords[1])**2 + (goal[0] - coords[0])**2) ** 0.5;
var xratio = (goal[0] - coords[0]) / tdist;
var cdx = xratio * dToWalk;
var yratio = (goal[1] - coords[1]) / tdist;
var cdy = yratio * dToWalk;
return [coords[0] + cdx, coords[1] + cdy];
}
// moves the current node from cx, cy to gx, gy
function animateNode() {
if (cy >= gy) {
moveThing(currNodeID, gx, gy);
return 1;
}
var newCoords = walkTo([cx, cy], [gx, gy], deltaMove);
moveThing(currNodeID, newCoords[0], newCoords[1]);
cx = newCoords[0];
cy = newCoords[1];
var req = window.requestAnimationFrame(animateNode);
return 1;
}
// creates an edge between two positions
function makeLine(pd, pa, cd, ca, id) {
if (pd == -1 || pa == -1) return false;
var par = getCoords(pd, pa);
var cur = getCoords(cd, ca);
var temp1 = walkTo(par, cur, nodeSize);
var temp2 = walkTo(cur, par, nodeSize);
par = temp1;
cur = temp2;
var cline = document.createElement('line');
cline.setAttribute('x1', par[0]);
cline.setAttribute('y1', par[1]);
cline.setAttribute('x2', cur[0]);
cline.setAttribute('y2', cur[1]);
cline.setAttribute('id', edgeIdPrefix + id);
cline.setAttribute('stroke-width', 1);
cline.setAttribute('stroke', 'black');
vis().appendChild(cline);
vis().innerHTML += "";
return true;
}
// takes the next step in inserting a node
function nextStep() {
var foundEqual = false;
if (currDatumInd >= datums.length) {
datums.push(newNode(toInsert, currNodeID, cdep, cacross));
finished = true;
}
else {
var cdatum = datums[currDatumInd][VALUE];
if (cdatum == toInsert) {
document.getElementById(nodeIdPrefix + currNodeID).remove();
finished = true;
foundEqual = true;
}
else {
var initLoc = getCoords(0, 0, false);
var prevLoc = getCoords(cdep, cacross, false);
pacc = cacross;
pdep = cdep;
if (toInsert < cdatum) {
if (datums[currDatumInd][CHILD][0] == -1)
datums[currDatumInd][CHILD][0] = datums.length;
currDatumInd = datums[currDatumInd][CHILD][0];
cdep += 1;
cacross *= 2;
}
else {
if (datums[currDatumInd][CHILD][1] == -1)
datums[currDatumInd][CHILD][1] = datums.length;
currDatumInd = datums[currDatumInd][CHILD][1];
cdep += 1;
cacross *= 2;
cacross += 1;
}
var newLoc = getCoords(cdep, cacross, false);
cx = prevLoc[0] - initLoc[0];
cy = prevLoc[1] - initLoc[1];
gx = newLoc[0] - initLoc[0];
gy = newLoc[1] - initLoc[1];
animateNode();
if (cdep > maxDepth) {
document.getElementById(nodeIdPrefix + currNodeID).remove();
foundEqual = true;
finished = true;
}
}
}
if (finished) {
inAnim = false;
if (foundEqual == false) {
var initLoc = getCoords(0, 0, false);
var prevLoc = getCoords(cdep, cacross, false);
var newLoc = getCoords(cdep, cacross, true);
cx = prevLoc[0] - initLoc[0];
cy = prevLoc[1] - initLoc[1];
gx = newLoc[0] - initLoc[0];
gy = newLoc[1] - initLoc[1];
if (makeLine(pdep, pacc, cdep, cacross, currNodeID))
datums[currDatumInd][EDGE] = true;
animateNode();
}
return false;
}
return true;
}
// creates a node
function addNode() {
var val = document.getElementById(inputID).value;
val = parseInt(val);
if (isNaN(val)) return;
if (Math.abs(val) > maxDatumValue) return;
inAnim = true;
finished = false;
toInsert = val;
currDatumInd = 0;
cdep = 0;
cacross = 0;
pacc = -1;
pdep = -1;
var sv = getCoords(0, 0, false);
currNodeID = createNode(sv[0], sv[1], val);
document.getElementById(inputID).value = "";
}
function gogo() {
if (inAnim) nextStep();
else addNode();
}
// https://stackoverflow.com/questions/11365632/how-to-detect-when-the-user-presses-enter-in-an-input-field
function gogoKeyPressed() {
var e = window.event;
var keyCode = e.code || e.key;
if (keyCode == 'Enter') {
gogo();
}
}