mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Refactor Heaps.
This commit is contained in:
parent
10e633f075
commit
031c5da556
@ -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!');
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
12
src/data-structures/heap/__test__/Heap.test.js
Normal file
12
src/data-structures/heap/__test__/Heap.test.js
Normal 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();
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user