From 031c5da5566c0ae56d70002e4a89e1f7fa5de182 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Thu, 16 Aug 2018 21:03:32 +0300 Subject: [PATCH] Refactor Heaps. --- src/data-structures/heap/Heap.js | 21 +++++++++++++------ src/data-structures/heap/MaxHeap.js | 11 ++-------- src/data-structures/heap/MinHeap.js | 9 +------- .../heap/__test__/Heap.test.js | 12 +++++++++++ 4 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 src/data-structures/heap/__test__/Heap.test.js diff --git a/src/data-structures/heap/Heap.js b/src/data-structures/heap/Heap.js index cce76a17..c98822bd 100644 --- a/src/data-structures/heap/Heap.js +++ b/src/data-structures/heap/Heap.js @@ -1,15 +1,18 @@ import Comparator from '../../utils/comparator/Comparator'; /** - * Parent class for heaps - * @class + * Parent class for Min and Max Heaps. */ -class Heap { +export default class Heap { /** * @constructs Heap * @param {Function} [comparatorFunction] */ constructor(comparatorFunction) { + if (new.target === Heap) { + throw new TypeError('Cannot construct Heap instance directly'); + } + // Array representation of the heap. this.heapContainer = []; this.compare = new Comparator(comparatorFunction); @@ -131,7 +134,7 @@ class Heap { /** * @param {*} item - * @return {MinHeap} + * @return {Heap} */ add(item) { this.heapContainer.push(item); @@ -170,6 +173,12 @@ class Heap { toString() { return this.heapContainer.toString(); } -} -export default Heap; + heapifyUp() { + throw new Error('You have to implement this method!'); + } + + heapifyDown() { + throw new Error('You have to implement this method!'); + } +} diff --git a/src/data-structures/heap/MaxHeap.js b/src/data-structures/heap/MaxHeap.js index abff4f36..4282ed4d 100644 --- a/src/data-structures/heap/MaxHeap.js +++ b/src/data-structures/heap/MaxHeap.js @@ -1,11 +1,6 @@ import Heap from './Heap'; -/** - * Creates a new MaxHeap - * @class - * @augments Heap - */ -class MaxHeap extends Heap { +export default class MaxHeap extends Heap { /** * @param {*} item * @param {Comparator} [customFindingComparator] @@ -74,7 +69,7 @@ class MaxHeap extends Heap { * @param {number} [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. let currentIndex = customStartIndex || 0; let nextIndex = null; @@ -100,5 +95,3 @@ class MaxHeap extends Heap { } } } - -export default MaxHeap; diff --git a/src/data-structures/heap/MinHeap.js b/src/data-structures/heap/MinHeap.js index 9eed46fb..ebdac399 100644 --- a/src/data-structures/heap/MinHeap.js +++ b/src/data-structures/heap/MinHeap.js @@ -1,11 +1,6 @@ import Heap from './Heap'; -/** - * Creates a new MinHeap - * @class - * @augments Heap - */ -class MinHeap extends Heap { +export default class MinHeap extends Heap { /** * @param {*} item * @param {Comparator} [customFindingComparator] @@ -98,5 +93,3 @@ class MinHeap extends Heap { } } } - -export default MinHeap; diff --git a/src/data-structures/heap/__test__/Heap.test.js b/src/data-structures/heap/__test__/Heap.test.js new file mode 100644 index 00000000..55e3e24b --- /dev/null +++ b/src/data-structures/heap/__test__/Heap.test.js @@ -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(); + }); +});