-
Notifications
You must be signed in to change notification settings - Fork 118
/
Binary-heap.js
225 lines (169 loc) · 5.59 KB
/
Binary-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
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
// BinaryHeap
// Implement the following functions on the MaxBinaryHeap/MinBinaryHeap class
// insert
// This function should insert a node in a binary heap.
// Make sure to re-order the heap after insertion if necessary.
// extractMax/Min
// This function should remove the root node in a binary heap.
// Make sure to re-order the heap after removal if necessary.
// heapify
// This function should convert array into a max/min heap.
// Should return the values property on the heap.
// heapSort
// This function should sort array with Time complexity O(n * log n) and
// Space complexity O(1). Should return the values property on the heap.
// Additionally, the following methods are implemented on the class:
// find - returns an array of indexes of items with the given value
// remove - removes all items with the given value
// isHeap - checks if the heap invariant is still being maintained (if the heap is valid)
class BinaryHeap {
constructor(array = []) {
if (new.target === BinaryHeap) {
throw new TypeError('You cannot instantiate BinaryHeap class directly');
}
this.values = array;
this.getItem = this.getItem.bind(this);
this.heapify();
}
getLeftChildIndex(parentIndex) {
return (2 * parentIndex) + 1;
}
getRightChildIndex(parentIndex) {
return (2 * parentIndex) + 2;
}
getParentIndex(childIndex) {
return Math.floor((childIndex - 1) / 2);
}
getItem(index) {
return this.values[index];
}
insert(value) {
this.values.push(value);
this.bubbleUp();
return this;
}
extract() {
if (this.isEmpty()) return null;
if (this.values.length === 1) return this.values.pop();
this.swap(0, this.values.length - 1);
const extractedItem = this.values.pop();
this.sinkDown();
return extractedItem;
}
peek() {
if (this.isEmpty()) return null;
return this.values[0];
}
find(value, getValueFn = this.getItem) {
const indexes = [];
for (let i = 0; i < this.values.length; i++) {
if (getValueFn(i) === value) indexes.push(i);
}
return indexes;
}
remove(value, getValueFn = this.getItem) {
const numberItems = this.find(value, getValueFn).length;
for (let i = 0; i < numberItems; i++) {
const itemIndex = this.find(value, getValueFn)[0];
if (this.values.length === 1 || itemIndex === this.values.length - 1) {
this.values.pop();
return this;
}
this.swap(itemIndex, this.values.length - 1);
this.values.pop();
const swapItemValue = this.getItem(itemIndex);
const parentIndex = this.getParentIndex(itemIndex);
if (parentIndex >= 0 && this.compare(swapItemValue, this.getItem(parentIndex))) {
this.bubbleUp(itemIndex);
} else {
this.sinkDown(itemIndex);
}
}
return this;
}
// Time complexity O(n)
// http://www.cs.umd.edu/~meesh/351/mount/lectures/lect14-heapsort-analysis-part.pdf
heapify() {
for (let i = Math.max(0, Math.floor(this.values.length / 2 - 1)); i >= 0; i--) {
this.sinkDown(i);
}
}
heapSort() {
const valuesCopy = [...this.values];
for (let i = this.values.length - 1; i > 0; i--) {
this.swap(0, i);
this.sinkDown(0, i);
}
const sortedArray = [...this.values];
this.values = valuesCopy;
return sortedArray.reverse();
}
heapSortViaExtract() {
const sortedArray = [];
const valuesCopy = [...this.values];
while (this.values.length) {
sortedArray.push(this.extract());
}
;
this.values = [...valuesCopy];
return sortedArray;
}
bubbleUp(idx = this.values.length - 1) {
let index = idx;
const value = this.getItem(index);
while (index > 0) {
const parentIndex = this.getParentIndex(index);
if (this.compare(this.getItem(parentIndex), value)) break;
this.swap(index, parentIndex);
index = parentIndex;
}
}
sinkDown(idx = 0, length = this.values.length) {
let index = idx;
while (true) {
const leftChildIndex = this.getLeftChildIndex(index);
const rightChildIndex = this.getRightChildIndex(index);
if (leftChildIndex >= length && leftChildIndex >= length) break;
if (rightChildIndex >= length &&
this.compare(this.getItem(index), this.getItem(leftChildIndex))) {
break;
}
if (this.compare(this.getItem(index), this.getItem(leftChildIndex)) &&
this.compare(this.getItem(index), this.getItem(rightChildIndex))) {
break;
}
const swapIndex = rightChildIndex >= length ||
this.compare(this.getItem(leftChildIndex), this.getItem(rightChildIndex))
? leftChildIndex
: rightChildIndex;
this.swap(index, swapIndex);
index = swapIndex;
}
}
isHeap(idx = 0) {
if (idx >= this.values.length) return true;
const parent = this.getItem(idx);
const leftChildIndex = this.getLeftChildIndex(idx);
const rightChildIndex = this.getRightChildIndex(idx);
if (leftChildIndex < this.values.length &&
!this.compare(parent, this.getItem(leftChildIndex))) {
return false;
}
if (rightChildIndex < this.values.length &&
!this.compare(parent, this.getItem(rightChildIndex))) {
return false;
}
return this.isHeap(leftChildIndex) && this.isHeap(rightChildIndex);
}
isEmpty() {
return !this.values.length;
}
swap(indexOne, indexTwo) {
[this.values[indexOne], this.values[indexTwo]] =
[this.values[indexTwo], this.values[indexOne]];
}
compare(firstItem, secondItem) {
throw new Error('You must implement your own compare method for min or max heap.');
}
}
module.exports = BinaryHeap;