-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathMinHeap.js
More file actions
82 lines (71 loc) · 2.41 KB
/
MinHeap.js
File metadata and controls
82 lines (71 loc) · 2.41 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
export default class MinHeap {
constructor() {
this.arr = Array(1).fill(null);
}
swap(i, j) {
const temp = this.arr[i];
this.arr[i] = this.arr[j];
this.arr[j] = temp;
}
insert(elem) {
// For min heap, when we insert a new element, we begin with adding it to the
// end of the array. Then we keep swapping with its parent until the parent's
// priority is smaller than both its children. This process is called bubble up.
// The worst-case runtime of the algorithm is O(log N) since that's the height
// of the binary tree and the most swaps we have to do.
this.arr.push(elem);
const insertedIndex = this.arr.length - 1;
this.bubbleUp(insertedIndex);
}
getHeap() {
return this.arr;
}
isEmpty() {
return this.arr.length < 2;
}
getMin() {
if (this.isEmpty()) {
return undefined;
}
return this.arr[1];
}
deleteMin() {
// For min Heap, when we delete min, we fill the void with the last element in the heap.
// Then we swap that element with its children until both its children
// are greater in priority than the element. This process is called bubble down.
// The worst-case runtime of the algorithm is O(log N), which is the height of
// the binary tree.
if (this.isEmpty()) {
return undefined;
}
const parentIndex = this.arr.length - 1;
this.swap(parentIndex, 1);
const min = this.arr.pop();
this.bubbleDown(1);
return min;
}
bubbleUp(insertedIndex) {
const parentIndex = Math.floor(insertedIndex / 2);
if (this.arr[parentIndex] === null) {
return;
}
if (this.arr[parentIndex].priority <= this.arr[insertedIndex].priority) {
return;
}
this.swap(parentIndex, insertedIndex);
this.bubbleUp(parentIndex);
}
bubbleDown(parentIndex) {
const leftChildIndex = parentIndex * 2;
const rightChildIndex = (parentIndex * 2) + 1;
const smallerChildIndex = [leftChildIndex, rightChildIndex]
.filter(i => this.arr[i] !== undefined)
.sort((a, b) => this.arr[a].priority - this.arr[b].priority)[0];
if (!smallerChildIndex) return;
if (this.arr[parentIndex].priority < this.arr[smallerChildIndex].priority) {
return;
}
this.swap(parentIndex, smallerChildIndex);
this.bubbleDown(leftChildIndex);
}
}