Repository navigation
Expand file tree
/
Copy pathheap.js
More file actions
97 lines (85 loc) · 2.07 KB
/
Copy pathheap.js
File metadata and controls
97 lines (85 loc) · 2.07 KB
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
// Generic binary heap
function Heap(opt_arr) {
// first element is not used
this.arr = [0];
if (opt_arr) {
this.buildHeap(opt_arr);
}
}
Heap.prototype.buildHeap = function(input) {
if (!input || input.length == 0) return;
this.arr = this.arr.concat(input);
for (var i = Math.floor(this.arr.length / 2); i >= 1; i--) {
this.heapify(i);
}
}
Heap.prototype.heapify = function(idx) {
var arr = this.arr;
if (!arr) return;
while (true) {
var left = 2 * idx;
var right = 2 * idx + 1;
if (right < arr.length && arr[idx] > arr[right] &&
arr[right] < arr[left]) {
var tmp = arr[idx];
arr[idx] = arr[right];
arr[right] = tmp;
idx = right;
} else if (left < arr.length && arr[idx] > arr[left]) {
var tmp = arr[idx];
arr[idx] = arr[left];
arr[left] = tmp;
idx = left;
} else {
break;
}
}
}
Heap.prototype.insert = function(value) {
var arr = this.arr;
var idx = arr.length;
arr.push(value);
while (idx > 1 && value < arr[Math.floor(idx/2)]) {
var parentIdx = Math.floor(idx/2);
arr[idx] = arr[parentIdx];
idx = parentIdx;
}
arr[idx] = value;
}
Heap.prototype.pop = function() {
var arr = this.arr;
if (!arr || this.isEmpty()) return null;
if (this.size() == 1) return arr.pop();
var top = arr[1];
arr[1] = arr.pop();
this.heapify(1);
return top;
}
Heap.prototype.peek = function() {
if (!this.arr || this.isEmpty()) return null;
return this.arr[1];
}
Heap.prototype.size = function() {
return Math.max(0, this.arr.length - 1);
}
Heap.prototype.isEmpty = function() {
return this.size() == 0;
}
Heap.prototype.toStringList = function() {
return this.arr.slice(1).join(', ');
}
Heap.prototype.toString = function() {
if (!this.arr || this.isEmpty()) return 'Heap is empty';
var out = '';
var nextLevelEnd = 1;
var cur = 1;
do {
out += this.arr[cur] + ' ';
if (cur == nextLevelEnd) {
out += '<br/>';
nextLevelEnd = nextLevelEnd * 2 + 1;
}
++cur;
} while (cur < this.arr.length);
return out;
}