File tree Expand file tree Collapse file tree 4 files changed +30
-23
lines changed
Expand file tree Collapse file tree 4 files changed +30
-23
lines changed Original file line number Diff line number Diff line change 11import Comparator from '../../utils/comparator/Comparator' ;
22
33/**
4- * Parent class for heaps
5- * @class
4+ * Parent class for Min and Max Heaps.
65 */
7- class Heap {
6+ export default class Heap {
87 /**
98 * @constructs Heap
109 * @param {Function } [comparatorFunction]
1110 */
1211 constructor ( comparatorFunction ) {
12+ if ( new . target === Heap ) {
13+ throw new TypeError ( 'Cannot construct Heap instance directly' ) ;
14+ }
15+
1316 // Array representation of the heap.
1417 this . heapContainer = [ ] ;
1518 this . compare = new Comparator ( comparatorFunction ) ;
@@ -131,7 +134,7 @@ class Heap {
131134
132135 /**
133136 * @param {* } item
134- * @return {MinHeap }
137+ * @return {Heap }
135138 */
136139 add ( item ) {
137140 this . heapContainer . push ( item ) ;
@@ -170,6 +173,12 @@ class Heap {
170173 toString ( ) {
171174 return this . heapContainer . toString ( ) ;
172175 }
173- }
174176
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 11import Heap from './Heap' ;
22
3- /**
4- * Creates a new MaxHeap
5- * @class
6- * @augments Heap
7- */
8- class MaxHeap extends Heap {
3+ export default class MaxHeap extends Heap {
94 /**
105 * @param {* } item
116 * @param {Comparator } [customFindingComparator]
@@ -74,7 +69,7 @@ class MaxHeap extends Heap {
7469 * @param {number } [customStartIndex]
7570 */
7671 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
7873 // of children. Do the same for next children after swap.
7974 let currentIndex = customStartIndex || 0 ;
8075 let nextIndex = null ;
@@ -100,5 +95,3 @@ class MaxHeap extends Heap {
10095 }
10196 }
10297}
103-
104- export default MaxHeap ;
Original file line number Diff line number Diff line change 11import Heap from './Heap' ;
22
3- /**
4- * Creates a new MinHeap
5- * @class
6- * @augments Heap
7- */
8- class MinHeap extends Heap {
3+ export default class MinHeap extends Heap {
94 /**
105 * @param {* } item
116 * @param {Comparator } [customFindingComparator]
@@ -98,5 +93,3 @@ class MinHeap extends Heap {
9893 }
9994 }
10095}
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