1
1
import Comparator from '../../utils/comparator/Comparator' ;
2
2
3
3
export default class MinHeap {
4
+ /**
5
+ * @param {Function } [comparatorFunction]
6
+ */
4
7
constructor ( comparatorFunction ) {
5
8
// Array representation of the heap.
6
9
this . heapContainer = [ ] ;
7
10
this . compare = new Comparator ( comparatorFunction ) ;
8
11
}
9
12
13
+ /**
14
+ * @param {number } parentIndex
15
+ * @return {number }
16
+ */
10
17
static getLeftChildIndex ( parentIndex ) {
11
18
return ( 2 * parentIndex ) + 1 ;
12
19
}
13
20
21
+ /**
22
+ * @param {number } parentIndex
23
+ * @return {number }
24
+ */
14
25
static getRightChildIndex ( parentIndex ) {
15
26
return ( 2 * parentIndex ) + 2 ;
16
27
}
17
28
29
+ /**
30
+ * @param {number } childIndex
31
+ * @return {number }
32
+ */
18
33
static getParentIndex ( childIndex ) {
19
34
return Math . floor ( ( childIndex - 1 ) / 2 ) ;
20
35
}
21
36
37
+ /**
38
+ * @param {number } childIndex
39
+ * @return {boolean }
40
+ */
22
41
static hasParent ( childIndex ) {
23
42
return this . getParentIndex ( childIndex ) >= 0 ;
24
43
}
25
44
45
+ /**
46
+ * @param {number } parentIndex
47
+ * @return {boolean }
48
+ */
26
49
hasLeftChild ( parentIndex ) {
27
50
return MinHeap . getLeftChildIndex ( parentIndex ) < this . heapContainer . length ;
28
51
}
29
52
53
+ /**
54
+ * @param {number } parentIndex
55
+ * @return {boolean }
56
+ */
30
57
hasRightChild ( parentIndex ) {
31
58
return MinHeap . getRightChildIndex ( parentIndex ) < this . heapContainer . length ;
32
59
}
33
60
61
+ /**
62
+ * @param {number } parentIndex
63
+ * @return {* }
64
+ */
34
65
leftChild ( parentIndex ) {
35
66
return this . heapContainer [ MinHeap . getLeftChildIndex ( parentIndex ) ] ;
36
67
}
37
68
69
+ /**
70
+ * @param {number } parentIndex
71
+ * @return {* }
72
+ */
38
73
rightChild ( parentIndex ) {
39
74
return this . heapContainer [ MinHeap . getRightChildIndex ( parentIndex ) ] ;
40
75
}
41
76
77
+ /**
78
+ * @param {number } childIndex
79
+ * @return {* }
80
+ */
42
81
parent ( childIndex ) {
43
82
return this . heapContainer [ MinHeap . getParentIndex ( childIndex ) ] ;
44
83
}
45
84
85
+ /**
86
+ * @param {number } indexOne
87
+ * @param {number } indexTwo
88
+ */
46
89
swap ( indexOne , indexTwo ) {
47
90
const tmp = this . heapContainer [ indexTwo ] ;
48
91
this . heapContainer [ indexTwo ] = this . heapContainer [ indexOne ] ;
49
92
this . heapContainer [ indexOne ] = tmp ;
50
93
}
51
94
95
+ /**
96
+ * @return {* }
97
+ */
52
98
peek ( ) {
53
99
if ( this . heapContainer . length === 0 ) {
54
100
return null ;
@@ -57,6 +103,9 @@ export default class MinHeap {
57
103
return this . heapContainer [ 0 ] ;
58
104
}
59
105
106
+ /**
107
+ * @return {* }
108
+ */
60
109
poll ( ) {
61
110
if ( this . heapContainer . length === 0 ) {
62
111
return null ;
@@ -75,11 +124,30 @@ export default class MinHeap {
75
124
return item ;
76
125
}
77
126
127
+ /**
128
+ * @param {* } item
129
+ */
78
130
add ( item ) {
79
131
this . heapContainer . push ( item ) ;
80
132
this . heapifyUp ( ) ;
81
133
}
82
134
135
+ /**
136
+ * @param {* } item
137
+ * @return {Number[] }
138
+ */
139
+ findItem ( item ) {
140
+ const foundItemIndices = [ ] ;
141
+
142
+ for ( let itemIndex = 0 ; itemIndex < this . heapContainer . length ; itemIndex += 1 ) {
143
+ if ( this . compare . equal ( item , this . heapContainer [ itemIndex ] ) ) {
144
+ foundItemIndices . push ( itemIndex ) ;
145
+ }
146
+ }
147
+
148
+ return foundItemIndices ;
149
+ }
150
+
83
151
heapifyUp ( ) {
84
152
// Take last element (last in array or the bottom left in a tree) in
85
153
// a heap container and lift him up until we find the parent element
@@ -120,10 +188,16 @@ export default class MinHeap {
120
188
}
121
189
}
122
190
191
+ /**
192
+ * @return {boolean }
193
+ */
123
194
isEmpty ( ) {
124
195
return ! this . heapContainer . length ;
125
196
}
126
197
198
+ /**
199
+ * @return {string }
200
+ */
127
201
toString ( ) {
128
202
return this . heapContainer . toString ( ) ;
129
203
}
0 commit comments