11import Comparator from '../../utils/comparator/Comparator' ;
22
33export default class MinHeap {
4+ /**
5+ * @param {Function } [comparatorFunction]
6+ */
47 constructor ( comparatorFunction ) {
58 // Array representation of the heap.
69 this . heapContainer = [ ] ;
710 this . compare = new Comparator ( comparatorFunction ) ;
811 }
912
13+ /**
14+ * @param {number } parentIndex
15+ * @return {number }
16+ */
1017 static getLeftChildIndex ( parentIndex ) {
1118 return ( 2 * parentIndex ) + 1 ;
1219 }
1320
21+ /**
22+ * @param {number } parentIndex
23+ * @return {number }
24+ */
1425 static getRightChildIndex ( parentIndex ) {
1526 return ( 2 * parentIndex ) + 2 ;
1627 }
1728
29+ /**
30+ * @param {number } childIndex
31+ * @return {number }
32+ */
1833 static getParentIndex ( childIndex ) {
1934 return Math . floor ( ( childIndex - 1 ) / 2 ) ;
2035 }
2136
37+ /**
38+ * @param {number } childIndex
39+ * @return {boolean }
40+ */
2241 static hasParent ( childIndex ) {
2342 return this . getParentIndex ( childIndex ) >= 0 ;
2443 }
2544
45+ /**
46+ * @param {number } parentIndex
47+ * @return {boolean }
48+ */
2649 hasLeftChild ( parentIndex ) {
2750 return MinHeap . getLeftChildIndex ( parentIndex ) < this . heapContainer . length ;
2851 }
2952
53+ /**
54+ * @param {number } parentIndex
55+ * @return {boolean }
56+ */
3057 hasRightChild ( parentIndex ) {
3158 return MinHeap . getRightChildIndex ( parentIndex ) < this . heapContainer . length ;
3259 }
3360
61+ /**
62+ * @param {number } parentIndex
63+ * @return {* }
64+ */
3465 leftChild ( parentIndex ) {
3566 return this . heapContainer [ MinHeap . getLeftChildIndex ( parentIndex ) ] ;
3667 }
3768
69+ /**
70+ * @param {number } parentIndex
71+ * @return {* }
72+ */
3873 rightChild ( parentIndex ) {
3974 return this . heapContainer [ MinHeap . getRightChildIndex ( parentIndex ) ] ;
4075 }
4176
77+ /**
78+ * @param {number } childIndex
79+ * @return {* }
80+ */
4281 parent ( childIndex ) {
4382 return this . heapContainer [ MinHeap . getParentIndex ( childIndex ) ] ;
4483 }
4584
85+ /**
86+ * @param {number } indexOne
87+ * @param {number } indexTwo
88+ */
4689 swap ( indexOne , indexTwo ) {
4790 const tmp = this . heapContainer [ indexTwo ] ;
4891 this . heapContainer [ indexTwo ] = this . heapContainer [ indexOne ] ;
4992 this . heapContainer [ indexOne ] = tmp ;
5093 }
5194
95+ /**
96+ * @return {* }
97+ */
5298 peek ( ) {
5399 if ( this . heapContainer . length === 0 ) {
54100 return null ;
@@ -57,6 +103,9 @@ export default class MinHeap {
57103 return this . heapContainer [ 0 ] ;
58104 }
59105
106+ /**
107+ * @return {* }
108+ */
60109 poll ( ) {
61110 if ( this . heapContainer . length === 0 ) {
62111 return null ;
@@ -75,11 +124,30 @@ export default class MinHeap {
75124 return item ;
76125 }
77126
127+ /**
128+ * @param {* } item
129+ */
78130 add ( item ) {
79131 this . heapContainer . push ( item ) ;
80132 this . heapifyUp ( ) ;
81133 }
82134
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+
83151 heapifyUp ( ) {
84152 // Take last element (last in array or the bottom left in a tree) in
85153 // a heap container and lift him up until we find the parent element
@@ -120,10 +188,16 @@ export default class MinHeap {
120188 }
121189 }
122190
191+ /**
192+ * @return {boolean }
193+ */
123194 isEmpty ( ) {
124195 return ! this . heapContainer . length ;
125196 }
126197
198+ /**
199+ * @return {string }
200+ */
127201 toString ( ) {
128202 return this . heapContainer . toString ( ) ;
129203 }
0 commit comments