-
Notifications
You must be signed in to change notification settings - Fork 0
/
greedy-geometric-bst.html
269 lines (218 loc) · 8.49 KB
/
greedy-geometric-bst.html
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
<head>
<link rel="stylesheet" href="greedy-geometric-bst.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
// TODO: Move time to the y-axis and value to the x-axis
// TODO: Draw and label axes
// TODO: Allow option to randomize the points
// - Could input desired number of points?
// Represent each point as a json object:
// {'time': time, 'value': value, 'isOriginalPoint': boolean} where:
// 'time' = time accessed
// 'value' = the value of the key
// 'isOriginalPoint' is true if the point was in the
// original set of search points.
// Stores the current set of points in our graph
var currentPoints = [];
// Stores how long the next item should wait before appearing
// Used to ensure points and rectangles show up sequentially
var currentTotalTimeout = 0;
// Returns true if the rectangle with firstPoint and secondPoint as corners
// is unsatisfied and false otherwise.
// currentPoints is the set of points we currently have.
var unsatisfiedRectangle = function(firstPoint, secondPoint, currentPoints) {
if (firstPoint == secondPoint) {
return false;
}
// If they don't actually form a rectangle (they form a line),
// this is by definition already satisfied
if (firstPoint.value == secondPoint.value ||
firstPoint.time == secondPoint.time) {
return false;
}
var lowerTimeBound = Math.min(firstPoint.time, secondPoint.time);
var upperTimeBound = Math.max(firstPoint.time, secondPoint.time);
var lowerValueBound = Math.min(firstPoint.value, secondPoint.value);
var upperValueBound = Math.max(firstPoint.value, secondPoint.value);
var isUnsatisfied = true;
currentPoints.forEach(function(point) {
if (point == firstPoint || point == secondPoint) {
// Shouldn't take into account itself
} else if (lowerTimeBound <= point.time &&
point.time <= upperTimeBound &&
lowerValueBound <= point.value &&
point.value <= upperValueBound) {
// There's a point within the rectangle, so it's not unsatisfied
isUnsatisfied = false;
}
});
return isUnsatisfied;
}
// Draw a point during the normal animation --
// use this if you're drawing a point for the first time.
var drawPointAnimated = function(canvas, currentSearchPoint) {
currentTotalTimeout += 1000;
setTimeout(function() {
drawPoint(canvas, currentSearchPoint);
}, currentTotalTimeout);
}
// Draw a point immediately on the canvas.
// Use this *only* to redraw points that have been
// previously drawn.
var drawPoint = function(canvas, currentSearchPoint) {
var xCoordinate = currentSearchPoint.time;
var yCoordinate = canvas.height - currentSearchPoint.value;
var pointRadius = 5;
var ctx = canvas.getContext('2d');
// Color original points vs. new points differently
if (currentSearchPoint.isOriginalPoint) {
ctx.fillStyle = 'blue';
} else {
ctx.fillStyle = 'red';
}
ctx.beginPath();
ctx.arc(xCoordinate, yCoordinate, pointRadius, 0, 2*Math.PI, false);
ctx.fill();
ctx.closePath();
}
// Draw an unsatisfied rectangles on canvas with corners at
// firstPoint and secondPoint
var drawUnsatisfiedRectangle = function(canvas, firstPoint, secondPoint) {
currentTotalTimeout += 1000;
// Use top-left corner
var xCoordinate = Math.min(firstPoint.time, secondPoint.time);
var yCoordinate = canvas.height - Math.max(firstPoint.value, secondPoint.value);
var width = Math.abs(secondPoint.time - firstPoint.time);
var height = Math.abs(firstPoint.value - secondPoint.value);
// Draw the rectangle
setTimeout(function() {
ctx = canvas.getContext('2d');
ctx.strokeStyle = 'green';
ctx.strokeWidth = 1;
ctx.strokeRect(xCoordinate, yCoordinate, width, height);
},
currentTotalTimeout);
// Delete the rectangle at the same time as you add
// the point that satisfies the rectangle
setTimeout(function() {
ctx.clearRect(xCoordinate-1, yCoordinate-1, width+4, height+4);
// Redraw the rectangle's corners, since the clearRect
// will clear part of the original points
drawPoint(canvas, firstPoint);
drawPoint(canvas, secondPoint);
},
currentTotalTimeout + 1000);
}
// Animates the greedy geometric BST algorithm.
// ctx is the canvas on which to draw the points
// searchPoints must be in order from smallest to largest value
var greedyGeometricBst = function(canvas, searchPoints) {
if (searchPoints.length == 0) {
// Base case: we're done searching
var finalList = currentPoints.slice();
console.log("final list:");
console.log(finalList);
// Reset current points so greedyGeometricBst can be called again.
currentPoints = [];
return;
}
var currentSearchPoint = searchPoints[0];
// Add search point to our list
currentPoints.push(currentSearchPoint);
// Draw this point on the canvas
drawPointAnimated(canvas, currentSearchPoint, true);
// See if there's any unsatisfied rectangles
// If there are, satisfy them
currentPoints.forEach(function(point) {
if (unsatisfiedRectangle(point, currentSearchPoint, currentPoints)) {
// Show the unsatisfied rectangle
drawUnsatisfiedRectangle(canvas, point, currentSearchPoint);
// If unsatisfied, must add a point
// the point should be on the same row as the current
// search point, so it should have the same time as
// the other point
var newPointValue = currentSearchPoint.value;
var newPointTime = point.time;
var newPoint = {
'time': newPointTime,
'value': newPointValue,
'isOriginalPoint': false,
}
currentPoints.push(newPoint);
drawPointAnimated(canvas, newPoint, false);
}
});
// Recurse with the next search point
greedyGeometricBst(canvas, searchPoints.slice(1));
};
// Call function as an example
/*greedyGeometricBst([{'time': 3, 'value': 10},
{'time': 1, 'value': 20},
{'time': 4, 'value': 30},
{'time': 2, 'value': 40}]);
greedyGeometricBst([{'time': 5, 'value': 100},
{'time': 5, 'value': 200}]);*/
var animateAlgorithm = function(points) {
// Display all points for 1.5 seconds
var canvas = document.getElementById('graph-canvas');
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
points.forEach(function(point) {
drawPoint(canvas, point);
});
setTimeout(function() {
// Run the algorithm
ctx.clearRect(0, 0, canvas.width, canvas.height);
currentPoints = [];
// TODO: Would need to 'stop' the other animations
// or disable the button until the original animation
// is finished
currentTotalTimeout = 0;
greedyGeometricBst(canvas, points);
}, 1500);
}
$(document).ready(function() {
var canvas = document.getElementById('graph-canvas');
canvas.width = "500";
canvas.height = "500";
$('#class-example-button').on('click', function() {
var examplePoints =
[{'time': 300, 'value': 100, 'isOriginalPoint': true},
{'time': 100, 'value': 200, 'isOriginalPoint': true},
{'time': 400, 'value': 300, 'isOriginalPoint': true},
{'time': 200, 'value': 400, 'isOriginalPoint': true}];
animateAlgorithm(examplePoints);
});
$('#animate-button').on('click', function() {
var numRandomPoints = $('#num-random-points').val();
console.log(numRandomPoints);
var points = [];
for (var i = 0; i < numRandomPoints; i++) {
var time = Math.ceil(Math.random() * 500);
var value = Math.ceil(Math.random() * 500);
var point = {
'time': time,
'value': value,
'isOriginalPoint': true,
};
points.push(point);
}
// Sort points by value, smallest to largest
points.sort(function(elt1, elt2) {
return elt1.value - elt2.value;
});
animateAlgorithm(points);
});
});
</script>
</head>
<body>
<p> 6.851 CO4 Coding: Implement geometric view of BSTs </p>
<p> Caitlin Cassidy </p>
<button id="class-example-button">Animate Class Example</button>
Animate a random number of nodes:
<input id="num-random-points" type="text" />
<button id="animate-button">Animate</button>
<!-- TODO: Add pause/fast-foward buttons -->
<canvas id="graph-canvas"></canvas>
</body>