Refactor Heaps.

This commit is contained in:
Oleksii Trekhleb 2018-08-16 21:03:32 +03:00
parent 10e633f075
commit 031c5da556
4 changed files with 30 additions and 23 deletions

View File

@ -1,15 +1,18 @@
import Comparator from '../../utils/comparator/Comparator'; import Comparator from '../../utils/comparator/Comparator';
/** /**
* Parent class for heaps * Parent class for Min and Max Heaps.
* @class
*/ */
class Heap { export default class Heap {
/** /**
* @constructs Heap * @constructs Heap
* @param {Function} [comparatorFunction] * @param {Function} [comparatorFunction]
*/ */
constructor(comparatorFunction) { constructor(comparatorFunction) {
if (new.target === Heap) {
throw new TypeError('Cannot construct Heap instance directly');
}
// Array representation of the heap. // Array representation of the heap.
this.heapContainer = []; this.heapContainer = [];
this.compare = new Comparator(comparatorFunction); this.compare = new Comparator(comparatorFunction);
@ -131,7 +134,7 @@ class Heap {
/** /**
* @param {*} item * @param {*} item
* @return {MinHeap} * @return {Heap}
*/ */
add(item) { add(item) {
this.heapContainer.push(item); this.heapContainer.push(item);
@ -170,6 +173,12 @@ class Heap {
toString() { toString() {
return this.heapContainer.toString(); return this.heapContainer.toString();
} }
heapifyUp() {
throw new Error('You have to implement this method!');
} }
export default Heap; heapifyDown() {
throw new Error('You have to implement this method!');
}
}

View File

@ -1,11 +1,6 @@
import Heap from './Heap'; import Heap from './Heap';
/** export default class MaxHeap extends Heap {
* Creates a new MaxHeap
* @class
* @augments Heap
*/
class MaxHeap extends Heap {
/** /**
* @param {*} item * @param {*} item
* @param {Comparator} [customFindingComparator] * @param {Comparator} [customFindingComparator]
@ -74,7 +69,7 @@ class MaxHeap extends Heap {
* @param {number} [customStartIndex] * @param {number} [customStartIndex]
*/ */
heapifyDown(customStartIndex) { heapifyDown(customStartIndex) {
// Compare the root element to its children and swap root with the smallest // Compare the root element to its children and swap root with the biggest
// of children. Do the same for next children after swap. // of children. Do the same for next children after swap.
let currentIndex = customStartIndex || 0; let currentIndex = customStartIndex || 0;
let nextIndex = null; let nextIndex = null;
@ -100,5 +95,3 @@ class MaxHeap extends Heap {
} }
} }
} }
export default MaxHeap;

View File

@ -1,11 +1,6 @@
import Heap from './Heap'; import Heap from './Heap';
/** export default class MinHeap extends Heap {
* Creates a new MinHeap
* @class
* @augments Heap
*/
class MinHeap extends Heap {
/** /**
* @param {*} item * @param {*} item
* @param {Comparator} [customFindingComparator] * @param {Comparator} [customFindingComparator]
@ -98,5 +93,3 @@ class MinHeap extends Heap {
} }
} }
} }
export default MinHeap;

View File

@ -0,0 +1,12 @@
import Heap from '../Heap';
describe('Heap', () => {
it('should not allow to create instance of the Heap directly', () => {
const instantiateHeap = () => {
const heap = new Heap();
heap.add(5);
};
expect(instantiateHeap).toThrow();
});
});