-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarytree.html
47 lines (41 loc) · 1.21 KB
/
binarytree.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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background: #000;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="/grid.js"></script>
<script>
function binaryTree(cells) {
/* At every iteration, toss a coin to link to N of E neighbour.
if no N or E neighbour, choose the only option available.
if both no N and E neighbour, halt. */
for (var i = cellsHigh - 1; i >= 0; i--) {
for (var j = 0; j < cellsWide; j++) {
idx = i * cellsWide + j;
// build list of available neighbours
neighbours = [];
if (cells[idx].N !== null) {
neighbours.push(cells[idx].N);
}
if(cells[idx].E !== null) {
neighbours.push(cells[idx].E);
}
// randomly pick the neighbour to link to
if (neighbours.length > 0) {
selected = Math.floor(Math.random() * neighbours.length);
// add the selected neighbour as link, bi-directionally.
cells[idx].links.push(neighbours[selected]);
cells[neighbours[selected]].links.push(idx);
}
}
}
}
initGrid();
binaryTree(cells);
drawGrid();
d3.select(self.frameElement).style("height", height + "px");
</script>