File tree 4 files changed +30
-23
lines changed
4 files changed +30
-23
lines changed Original file line number Diff line number Diff line change 1
1
import Comparator from '../../utils/comparator/Comparator' ;
2
2
3
3
/**
4
- * Parent class for heaps
5
- * @class
4
+ * Parent class for Min and Max Heaps.
6
5
*/
7
- class Heap {
6
+ export default class Heap {
8
7
/**
9
8
* @constructs Heap
10
9
* @param {Function } [comparatorFunction]
11
10
*/
12
11
constructor ( comparatorFunction ) {
12
+ if ( new . target === Heap ) {
13
+ throw new TypeError ( 'Cannot construct Heap instance directly' ) ;
14
+ }
15
+
13
16
// Array representation of the heap.
14
17
this . heapContainer = [ ] ;
15
18
this . compare = new Comparator ( comparatorFunction ) ;
@@ -131,7 +134,7 @@ class Heap {
131
134
132
135
/**
133
136
* @param {* } item
134
- * @return {MinHeap }
137
+ * @return {Heap }
135
138
*/
136
139
add ( item ) {
137
140
this . heapContainer . push ( item ) ;
@@ -170,6 +173,12 @@ class Heap {
170
173
toString ( ) {
171
174
return this . heapContainer . toString ( ) ;
172
175
}
173
- }
174
176
175
- export default Heap ;
177
+ heapifyUp ( ) {
178
+ throw new Error ( 'You have to implement this method!' ) ;
179
+ }
180
+
181
+ heapifyDown ( ) {
182
+ throw new Error ( 'You have to implement this method!' ) ;
183
+ }
184
+ }
Original file line number Diff line number Diff line change 1
1
import Heap from './Heap' ;
2
2
3
- /**
4
- * Creates a new MaxHeap
5
- * @class
6
- * @augments Heap
7
- */
8
- class MaxHeap extends Heap {
3
+ export default class MaxHeap extends Heap {
9
4
/**
10
5
* @param {* } item
11
6
* @param {Comparator } [customFindingComparator]
@@ -74,7 +69,7 @@ class MaxHeap extends Heap {
74
69
* @param {number } [customStartIndex]
75
70
*/
76
71
heapifyDown ( customStartIndex ) {
77
- // Compare the root element to its children and swap root with the smallest
72
+ // Compare the root element to its children and swap root with the biggest
78
73
// of children. Do the same for next children after swap.
79
74
let currentIndex = customStartIndex || 0 ;
80
75
let nextIndex = null ;
@@ -100,5 +95,3 @@ class MaxHeap extends Heap {
100
95
}
101
96
}
102
97
}
103
-
104
- export default MaxHeap ;
Original file line number Diff line number Diff line change 1
1
import Heap from './Heap' ;
2
2
3
- /**
4
- * Creates a new MinHeap
5
- * @class
6
- * @augments Heap
7
- */
8
- class MinHeap extends Heap {
3
+ export default class MinHeap extends Heap {
9
4
/**
10
5
* @param {* } item
11
6
* @param {Comparator } [customFindingComparator]
@@ -98,5 +93,3 @@ class MinHeap extends Heap {
98
93
}
99
94
}
100
95
}
101
-
102
- export default MinHeap ;
Original file line number Diff line number Diff line change
1
+ import Heap from '../Heap' ;
2
+
3
+ describe ( 'Heap' , ( ) => {
4
+ it ( 'should not allow to create instance of the Heap directly' , ( ) => {
5
+ const instantiateHeap = ( ) => {
6
+ const heap = new Heap ( ) ;
7
+ heap . add ( 5 ) ;
8
+ } ;
9
+
10
+ expect ( instantiateHeap ) . toThrow ( ) ;
11
+ } ) ;
12
+ } ) ;
You can’t perform that action at this time.
0 commit comments