Skip to content

Commit 031c5da

Browse files
committedAug 16, 2018
Refactor Heaps.
1 parent 10e633f commit 031c5da

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed
 

‎src/data-structures/heap/Heap.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import 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+
}

‎src/data-structures/heap/MaxHeap.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import 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;

‎src/data-structures/heap/MinHeap.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import 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 numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
});

0 commit comments

Comments
 (0)
Please sign in to comment.