-
Notifications
You must be signed in to change notification settings - Fork 6
/
heap.js
47 lines (40 loc) · 897 Bytes
/
heap.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
var LEFT = 2;
var RIGHT = 3;
function make_heap() {
return [null, null, null, null];
}
function heap_add(heap, tri) {
var z = (tri[0][Z] + tri[1][Z] + tri[2][Z]) / 3.0;
_heap_insert(heap, tri, z);
}
function _heap_insert(heap, tri, z) {
if(!heap[0]) {
heap[0] = tri;
heap[1] = z;
}
else if(z > heap[1]) {
if(!heap[LEFT]) {
heap[LEFT] = [tri, z, null, null];
}
else {
_heap_insert(heap[LEFT], tri, z);
}
}
else {
if(!heap[RIGHT]) {
heap[RIGHT] = [tri, z, null, null];
}
else {
_heap_insert(heap[RIGHT], tri, z);
}
}
}
function heap_depth_first(heap, func) {
if(heap[LEFT]) {
heap_depth_first(heap[LEFT], func);
}
func(heap[0]);
if(heap[RIGHT]) {
heap_depth_first(heap[RIGHT], func);
}
}