-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin-heap.js
164 lines (133 loc) · 2.95 KB
/
min-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
'use strict';
/**
* @param {number} i
* @param {number} j
* @param {Array<number>} array
*/
function swap (i, j, array) {
let tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
/**
* Min heap implementation using an array
*/
export default class MinHeap {
constructor () {
this.values = [];
}
/**
* @param {number} index
* @return {number}
*/
getLeftChildIndex (index) {
return (index * 2) + 1;
}
/**
* @param {number} index
* @return {number}
*/
getRightChildIndex (index) {
return (index * 2) + 2;
}
/**
* @param {number} index
* @return {number}
*/
getParentIndex (index) {
return Math.ceil((index - 2) / 2);
}
/**
* @param {number} index
* @return {number}
*/
getLeftChild (index) {
this.checkIfEmpty();
return this.values[this.getLeftChildIndex(index)];
}
/**
* @param {number} index
* @return {number}
*/
getRightChild (index) {
this.checkIfEmpty();
return this.values[this.getRightChildIndex(index)];
}
/**
* @param {number} index
* @return {number}
*/
getParent (index) {
this.checkIfEmpty();
return this.values[this.getParentIndex(index)];
}
/**
* @param {number} index
* @return {Boolean}
*/
hasLeftChild (index) {
return this.getLeftChildIndex(index) < this.values.length;
}
/**
* @param {number} index
* @return {Boolean}
*/
hasRightChild (index) {
return this.getRightChildIndex(index) < this.values.length;
}
/**
* @param {number} index
*/
add (value) {
this.values.push(value);
if (this.values.length > 1) {
this.heapifyUp();
}
}
/**
* @return {number}
*/
peek () {
this.checkIfEmpty();
return this.values[0];
}
heapifyUp () {
let currentIndex = this.values.length - 1;
while (currentIndex > 0 && this.values[currentIndex] < this.getParent(currentIndex)) {
let parentIndex = this.getParentIndex(currentIndex);
swap(currentIndex, parentIndex, this.values);
currentIndex = parentIndex;
}
}
/**
* @return {number}
*/
poll () {
this.checkIfEmpty();
swap(0, this.values.length - 1, this.values);
let minNum = this.values.pop();
if (this.values.length > 1) {
this.heapifyDown();
}
return minNum;
}
heapifyDown () {
let currentIndex = 0;
while (this.hasLeftChild(currentIndex)) {
let smallestChildIndex = this.getLeftChildIndex(currentIndex);
if (this.hasRightChild(currentIndex) && this.getRightChild(currentIndex) < this.getLeftChild(currentIndex)) {
smallestChildIndex = this.getRightChildIndex(currentIndex);
}
if (this.values[currentIndex] < this.values[smallestChildIndex]) {
break;
}
swap(smallestChildIndex, currentIndex, this.values);
currentIndex = smallestChildIndex;
}
}
checkIfEmpty () {
if (!this.values.length) {
throw Error('Heap is empty!');
}
}
};