mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-08 10:24:54 +08:00
Update Heap.
This commit is contained in:
@ -1,54 +1,100 @@
|
|||||||
import Comparator from '../../utils/comparator/Comparator';
|
import Comparator from '../../utils/comparator/Comparator';
|
||||||
|
|
||||||
export default class MinHeap {
|
export default class MinHeap {
|
||||||
|
/**
|
||||||
|
* @param {Function} [comparatorFunction]
|
||||||
|
*/
|
||||||
constructor(comparatorFunction) {
|
constructor(comparatorFunction) {
|
||||||
// Array representation of the heap.
|
// Array representation of the heap.
|
||||||
this.heapContainer = [];
|
this.heapContainer = [];
|
||||||
this.compare = new Comparator(comparatorFunction);
|
this.compare = new Comparator(comparatorFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} parentIndex
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
static getLeftChildIndex(parentIndex) {
|
static getLeftChildIndex(parentIndex) {
|
||||||
return (2 * parentIndex) + 1;
|
return (2 * parentIndex) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} parentIndex
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
static getRightChildIndex(parentIndex) {
|
static getRightChildIndex(parentIndex) {
|
||||||
return (2 * parentIndex) + 2;
|
return (2 * parentIndex) + 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} childIndex
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
static getParentIndex(childIndex) {
|
static getParentIndex(childIndex) {
|
||||||
return Math.floor((childIndex - 1) / 2);
|
return Math.floor((childIndex - 1) / 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} childIndex
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
static hasParent(childIndex) {
|
static hasParent(childIndex) {
|
||||||
return this.getParentIndex(childIndex) >= 0;
|
return this.getParentIndex(childIndex) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} parentIndex
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
hasLeftChild(parentIndex) {
|
hasLeftChild(parentIndex) {
|
||||||
return MinHeap.getLeftChildIndex(parentIndex) < this.heapContainer.length;
|
return MinHeap.getLeftChildIndex(parentIndex) < this.heapContainer.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} parentIndex
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
hasRightChild(parentIndex) {
|
hasRightChild(parentIndex) {
|
||||||
return MinHeap.getRightChildIndex(parentIndex) < this.heapContainer.length;
|
return MinHeap.getRightChildIndex(parentIndex) < this.heapContainer.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} parentIndex
|
||||||
|
* @return {*}
|
||||||
|
*/
|
||||||
leftChild(parentIndex) {
|
leftChild(parentIndex) {
|
||||||
return this.heapContainer[MinHeap.getLeftChildIndex(parentIndex)];
|
return this.heapContainer[MinHeap.getLeftChildIndex(parentIndex)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} parentIndex
|
||||||
|
* @return {*}
|
||||||
|
*/
|
||||||
rightChild(parentIndex) {
|
rightChild(parentIndex) {
|
||||||
return this.heapContainer[MinHeap.getRightChildIndex(parentIndex)];
|
return this.heapContainer[MinHeap.getRightChildIndex(parentIndex)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} childIndex
|
||||||
|
* @return {*}
|
||||||
|
*/
|
||||||
parent(childIndex) {
|
parent(childIndex) {
|
||||||
return this.heapContainer[MinHeap.getParentIndex(childIndex)];
|
return this.heapContainer[MinHeap.getParentIndex(childIndex)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} indexOne
|
||||||
|
* @param {number} indexTwo
|
||||||
|
*/
|
||||||
swap(indexOne, indexTwo) {
|
swap(indexOne, indexTwo) {
|
||||||
const tmp = this.heapContainer[indexTwo];
|
const tmp = this.heapContainer[indexTwo];
|
||||||
this.heapContainer[indexTwo] = this.heapContainer[indexOne];
|
this.heapContainer[indexTwo] = this.heapContainer[indexOne];
|
||||||
this.heapContainer[indexOne] = tmp;
|
this.heapContainer[indexOne] = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {*}
|
||||||
|
*/
|
||||||
peek() {
|
peek() {
|
||||||
if (this.heapContainer.length === 0) {
|
if (this.heapContainer.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
@ -57,6 +103,9 @@ export default class MinHeap {
|
|||||||
return this.heapContainer[0];
|
return this.heapContainer[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {*}
|
||||||
|
*/
|
||||||
poll() {
|
poll() {
|
||||||
if (this.heapContainer.length === 0) {
|
if (this.heapContainer.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
@ -75,11 +124,30 @@ export default class MinHeap {
|
|||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {*} item
|
||||||
|
*/
|
||||||
add(item) {
|
add(item) {
|
||||||
this.heapContainer.push(item);
|
this.heapContainer.push(item);
|
||||||
this.heapifyUp();
|
this.heapifyUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {*} item
|
||||||
|
* @return {Number[]}
|
||||||
|
*/
|
||||||
|
findItem(item) {
|
||||||
|
const foundItemIndices = [];
|
||||||
|
|
||||||
|
for (let itemIndex = 0; itemIndex < this.heapContainer.length; itemIndex += 1) {
|
||||||
|
if (this.compare.equal(item, this.heapContainer[itemIndex])) {
|
||||||
|
foundItemIndices.push(itemIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return foundItemIndices;
|
||||||
|
}
|
||||||
|
|
||||||
heapifyUp() {
|
heapifyUp() {
|
||||||
// Take last element (last in array or the bottom left in a tree) in
|
// Take last element (last in array or the bottom left in a tree) in
|
||||||
// a heap container and lift him up until we find the parent element
|
// a heap container and lift him up until we find the parent element
|
||||||
@ -120,10 +188,16 @@ export default class MinHeap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
isEmpty() {
|
isEmpty() {
|
||||||
return !this.heapContainer.length;
|
return !this.heapContainer.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
toString() {
|
toString() {
|
||||||
return this.heapContainer.toString();
|
return this.heapContainer.toString();
|
||||||
}
|
}
|
||||||
|
@ -88,4 +88,20 @@ describe('MinHeap', () => {
|
|||||||
expect(minHeap.poll()).toBe(3);
|
expect(minHeap.poll()).toBe(3);
|
||||||
expect(minHeap.toString()).toBe('10,11,12');
|
expect(minHeap.toString()).toBe('10,11,12');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should be possible to find item indices in heap', () => {
|
||||||
|
const minHeap = new MinHeap();
|
||||||
|
|
||||||
|
minHeap.add(3);
|
||||||
|
minHeap.add(12);
|
||||||
|
minHeap.add(10);
|
||||||
|
minHeap.add(11);
|
||||||
|
minHeap.add(11);
|
||||||
|
|
||||||
|
expect(minHeap.toString()).toBe('3,11,10,12,11');
|
||||||
|
|
||||||
|
expect(minHeap.findItem(5)).toEqual([]);
|
||||||
|
expect(minHeap.findItem(3)).toEqual([0]);
|
||||||
|
expect(minHeap.findItem(11)).toEqual([1, 4]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user