-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
245 lines (223 loc) · 5.36 KB
/
index.js
File metadata and controls
245 lines (223 loc) · 5.36 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
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
'use strict';
/**
* Creates a binary heap.
*
* @constructor
* @param {function} customCompare An optional custom node comparison
* function.
*/
var BinaryHeap = function (customCompare) {
/**
* The backing data of the heap.
* @type {Object[]}
* @private
*/
this.list = [];
if (customCompare) {
this.compare = customCompare;
}
};
/**
* Builds a heap with the provided keys and values, this will discard the
* heap's current data.
*
* @param {Array} keys An array of keys.
* @param {Array} values An array of values. This must be the same size as the
* key array.
*/
BinaryHeap.prototype.buildHeap = function (keys, values) {
if (typeof values !== 'undefined' && values.length !== keys.length) {
throw new Error('Key array must be the same length as value array');
}
var nodeArray = [];
for (var i = 0; i < keys.length; i++) {
nodeArray.push(new Node(keys[i], values ? values[i] : undefined));
}
buildHeapFromNodeArray(this, nodeArray);
};
/**
* Clears the heap's data, making it an empty heap.
*/
BinaryHeap.prototype.clear = function () {
this.list.length = 0;
};
/**
* Extracts and returns the minimum node from the heap.
*
* @return {Node} node The heap's minimum node or undefined if the heap is
* empty.
*/
BinaryHeap.prototype.extractMinimum = function () {
if (!this.list.length) {
return undefined;
}
if (this.list.length === 1) {
return this.list.shift();
}
var min = this.list[0];
this.list[0] = this.list.pop();
heapify(this, 0);
return min;
};
/**
* Returns the minimum node from the heap.
*
* @return {Node} node The heap's minimum node or undefined if the heap is
* empty.
*/
BinaryHeap.prototype.findMinimum = function () {
return this.isEmpty() ? undefined : this.list[0];
};
/**
* Inserts a new key-value pair into the heap.
*
* @param {Object} key The key to insert.
* @param {Object} value The value to insert.
* @return {Node} node The inserted node.
*/
BinaryHeap.prototype.insert = function (key, value) {
var i = this.list.length;
var node = new Node(key, value);
this.list.push(node);
var parent = getParent(i);
while (typeof parent !== 'undefined' &&
this.compare(this.list[i], this.list[parent]) < 0) {
swap(this.list, i, parent);
i = parent;
parent = getParent(i);
}
return node;
};
/**
* @return {boolean} Whether the heap is empty.
*/
BinaryHeap.prototype.isEmpty = function () {
return !this.list.length;
};
/**
* @return {number} The size of the heap.
*/
BinaryHeap.prototype.size = function () {
return this.list.length;
};
/**
* Joins another heap to this one.
*
* @param {BinaryHeap} otherHeap The other heap.
*/
BinaryHeap.prototype.union = function (otherHeap) {
var array = this.list.concat(otherHeap.list);
buildHeapFromNodeArray(this, array);
};
/**
* Compares two nodes with each other.
*
* @private
* @param {Object} a The first key to compare.
* @param {Object} b The second key to compare.
* @return -1, 0 or 1 if a < b, a == b or a > b respectively.
*/
BinaryHeap.prototype.compare = function (a, b) {
if (a.key > b.key) {
return 1;
}
if (a.key < b.key) {
return -1;
}
return 0;
};
/**
* Heapifies a node.
*
* @private
* @param {BinaryHeap} heap The heap containing the node to heapify.
* @param {number} i The index of the node to heapify.
*/
function heapify(heap, i) {
var l = getLeft(i);
var r = getRight(i);
var smallest = i;
if (l < heap.list.length &&
heap.compare(heap.list[l], heap.list[i]) < 0) {
smallest = l;
}
if (r < heap.list.length &&
heap.compare(heap.list[r], heap.list[smallest]) < 0) {
smallest = r;
}
if (smallest !== i) {
swap(heap.list, i, smallest);
heapify(heap, smallest);
}
}
/**
* Builds a heap from a node array, this will discard the heap's current data.
*
* @private
* @param {BinaryHeap} heap The heap to override.
* @param {Node[]} nodeArray The array of nodes for the new heap.
*/
function buildHeapFromNodeArray(heap, nodeArray) {
heap.list = nodeArray;
for (var i = Math.floor(heap.list.length / 2); i >= 0; i--) {
heapify(heap, i);
}
}
/**
* Swaps two values in an array.
*
* @private
* @param {Array} array The array to swap on.
* @param {number} a The index of the first element.
* @param {number} b The index of the second element.
*/
function swap(array, a, b) {
var temp = array[a];
array[a] = array[b];
array[b] = temp;
}
/**
* Gets the index of a node's parent.
*
* @private
* @param {number} i The index of the node to get the parent of.
* @return {number} The index of the node's parent.
*/
function getParent(i) {
if (i === 0) {
return undefined;
}
return Math.floor((i - 1) / 2);
}
/**
* Gets the index of a node's left child.
*
* @private
* @param {number} i The index of the node to get the left child of.
* @return {number} The index of the node's left child.
*/
function getLeft(i) {
return 2 * i + 1;
}
/**
* Gets the index of a node's right child.
*
* @private
* @param {number} i The index of the node to get the right child of.
* @return {number} The index of the node's right child.
*/
function getRight(i) {
return 2 * i + 2;
}
/**
* Creates a node.
*
* @constructor
* @param {Object} key The key of the new node.
* @param {Object} value The value of the new node.
*/
function Node(key, value) {
this.key = key;
this.value = value;
}
module.exports = BinaryHeap;