Skip to content

Commit c0fe2a3

Browse files
committed
Update Heap.
1 parent 59f61dc commit c0fe2a3

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

Diff for: src/data-structures/heap/MinHeap.js

+74
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,100 @@
11
import Comparator from '../../utils/comparator/Comparator';
22

33
export default class MinHeap {
4+
/**
5+
* @param {Function} [comparatorFunction]
6+
*/
47
constructor(comparatorFunction) {
58
// Array representation of the heap.
69
this.heapContainer = [];
710
this.compare = new Comparator(comparatorFunction);
811
}
912

13+
/**
14+
* @param {number} parentIndex
15+
* @return {number}
16+
*/
1017
static getLeftChildIndex(parentIndex) {
1118
return (2 * parentIndex) + 1;
1219
}
1320

21+
/**
22+
* @param {number} parentIndex
23+
* @return {number}
24+
*/
1425
static getRightChildIndex(parentIndex) {
1526
return (2 * parentIndex) + 2;
1627
}
1728

29+
/**
30+
* @param {number} childIndex
31+
* @return {number}
32+
*/
1833
static getParentIndex(childIndex) {
1934
return Math.floor((childIndex - 1) / 2);
2035
}
2136

37+
/**
38+
* @param {number} childIndex
39+
* @return {boolean}
40+
*/
2241
static hasParent(childIndex) {
2342
return this.getParentIndex(childIndex) >= 0;
2443
}
2544

45+
/**
46+
* @param {number} parentIndex
47+
* @return {boolean}
48+
*/
2649
hasLeftChild(parentIndex) {
2750
return MinHeap.getLeftChildIndex(parentIndex) < this.heapContainer.length;
2851
}
2952

53+
/**
54+
* @param {number} parentIndex
55+
* @return {boolean}
56+
*/
3057
hasRightChild(parentIndex) {
3158
return MinHeap.getRightChildIndex(parentIndex) < this.heapContainer.length;
3259
}
3360

61+
/**
62+
* @param {number} parentIndex
63+
* @return {*}
64+
*/
3465
leftChild(parentIndex) {
3566
return this.heapContainer[MinHeap.getLeftChildIndex(parentIndex)];
3667
}
3768

69+
/**
70+
* @param {number} parentIndex
71+
* @return {*}
72+
*/
3873
rightChild(parentIndex) {
3974
return this.heapContainer[MinHeap.getRightChildIndex(parentIndex)];
4075
}
4176

77+
/**
78+
* @param {number} childIndex
79+
* @return {*}
80+
*/
4281
parent(childIndex) {
4382
return this.heapContainer[MinHeap.getParentIndex(childIndex)];
4483
}
4584

85+
/**
86+
* @param {number} indexOne
87+
* @param {number} indexTwo
88+
*/
4689
swap(indexOne, indexTwo) {
4790
const tmp = this.heapContainer[indexTwo];
4891
this.heapContainer[indexTwo] = this.heapContainer[indexOne];
4992
this.heapContainer[indexOne] = tmp;
5093
}
5194

95+
/**
96+
* @return {*}
97+
*/
5298
peek() {
5399
if (this.heapContainer.length === 0) {
54100
return null;
@@ -57,6 +103,9 @@ export default class MinHeap {
57103
return this.heapContainer[0];
58104
}
59105

106+
/**
107+
* @return {*}
108+
*/
60109
poll() {
61110
if (this.heapContainer.length === 0) {
62111
return null;
@@ -75,11 +124,30 @@ export default class MinHeap {
75124
return item;
76125
}
77126

127+
/**
128+
* @param {*} item
129+
*/
78130
add(item) {
79131
this.heapContainer.push(item);
80132
this.heapifyUp();
81133
}
82134

135+
/**
136+
* @param {*} item
137+
* @return {Number[]}
138+
*/
139+
findItem(item) {
140+
const foundItemIndices = [];
141+
142+
for (let itemIndex = 0; itemIndex < this.heapContainer.length; itemIndex += 1) {
143+
if (this.compare.equal(item, this.heapContainer[itemIndex])) {
144+
foundItemIndices.push(itemIndex);
145+
}
146+
}
147+
148+
return foundItemIndices;
149+
}
150+
83151
heapifyUp() {
84152
// Take last element (last in array or the bottom left in a tree) in
85153
// a heap container and lift him up until we find the parent element
@@ -120,10 +188,16 @@ export default class MinHeap {
120188
}
121189
}
122190

191+
/**
192+
* @return {boolean}
193+
*/
123194
isEmpty() {
124195
return !this.heapContainer.length;
125196
}
126197

198+
/**
199+
* @return {string}
200+
*/
127201
toString() {
128202
return this.heapContainer.toString();
129203
}

Diff for: src/data-structures/heap/__test__/MinHeap.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,20 @@ describe('MinHeap', () => {
8888
expect(minHeap.poll()).toBe(3);
8989
expect(minHeap.toString()).toBe('10,11,12');
9090
});
91+
92+
it('should be possible to find item indices in heap', () => {
93+
const minHeap = new MinHeap();
94+
95+
minHeap.add(3);
96+
minHeap.add(12);
97+
minHeap.add(10);
98+
minHeap.add(11);
99+
minHeap.add(11);
100+
101+
expect(minHeap.toString()).toBe('3,11,10,12,11');
102+
103+
expect(minHeap.findItem(5)).toEqual([]);
104+
expect(minHeap.findItem(3)).toEqual([0]);
105+
expect(minHeap.findItem(11)).toEqual([1, 4]);
106+
});
91107
});

0 commit comments

Comments
 (0)