Skip to content

Commit 1891b73

Browse files
committed
[Heap]
1 parent 5ee6666 commit 1891b73

File tree

6 files changed

+71
-53
lines changed

6 files changed

+71
-53
lines changed

examples/PacktDataStructuresAlgorithms.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/PacktDataStructuresAlgorithms.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/chapter10/01-UsingMinHeap.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
const { MinHeap } = PacktDataStructuresAlgorithms;
22

3-
const heap = new MinHeap();
3+
let heap = new MinHeap();
44

55
heap.insert(2);
66
heap.insert(3);
77
heap.insert(4);
88
heap.insert(5);
99

10-
heap.insert(1);
10+
heap.insert(2);
1111

1212
console.log(heap.getAsArray());
1313

1414
console.log('Heap size: ', heap.size()); // 5
1515
console.log('Heap is empty: ', heap.isEmpty()); // false
1616
console.log('Heap min value: ', heap.findMinimum()); // 1
1717

18-
heap.insert(6);
19-
heap.insert(7);
18+
heap = new MinHeap();
19+
for (let i = 1; i < 10; i++) {
20+
heap.insert(i);
21+
}
2022

2123
console.log(heap.getAsArray());
2224

2325
console.log('Extract minimum: ', heap.extract());
24-
console.log(heap.getAsArray());
26+
console.log(heap.getAsArray()); // [2, 4, 3, 8, 5, 6, 7, 9]
2527

src/js/data-structures/heap.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ export class MinHeap {
7878
if (this.size() === 1) {
7979
return this.heap.shift();
8080
}
81-
const removedValue = this.heap.shift();
81+
const removedValue = this.heap[0];
82+
this.heap[0] = this.heap.pop();
8283
this.siftDown(0);
8384
return removedValue;
8485
}

src/ts/data-structures/heap.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export class MinHeap<T> {
66
constructor(protected compareFn: ICompareFunction<T> = defaultCompare) {}
77

88
private getLeftIndex(index: number) {
9-
return (2 * index) + 1;
9+
return 2 * index + 1;
1010
}
1111

1212
private getRightIndex(index: number) {
13-
return (2 * index) + 2;
13+
return 2 * index + 2;
1414
}
1515

1616
private getParentIndex(index: number) {
@@ -71,7 +71,10 @@ export class MinHeap<T> {
7171

7272
private siftUp(index: number): void {
7373
let parent = this.getParentIndex(index);
74-
while (index > 0 && this.compareFn(this.heap[parent], this.heap[index]) === Compare.BIGGER_THAN) {
74+
while (
75+
index > 0 &&
76+
this.compareFn(this.heap[parent], this.heap[index]) === Compare.BIGGER_THAN
77+
) {
7578
swap(this.heap, parent, index);
7679
index = parent;
7780
parent = this.getParentIndex(index);
@@ -85,7 +88,8 @@ export class MinHeap<T> {
8588
if (this.size() === 1) {
8689
return this.heap.shift();
8790
}
88-
const removedValue = this.heap.shift();
91+
const removedValue = this.heap[0];
92+
this.heap[0] = this.heap.pop();
8993
this.siftDown(0);
9094
return removedValue;
9195
}

test/ts/data-structures/heap.spec.ts

+52-41
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,67 @@ describe('Heap', () => {
1111
heap = new MinHeap<number>();
1212
});
1313

14-
it('starts empty', () => {
15-
14+
it('starts empty MinHeap', () => {
15+
expect(heap.size()).to.equal(0);
16+
expect(heap.isEmpty()).to.equal(true);
1617
});
1718

18-
it('inserts elements in the Heap', () => {
19-
20-
let min = 2;
21-
22-
heap.insert(2);
23-
expect(heap.findMinimum()).to.equal(min);
24-
heap.insert(3);
25-
expect(heap.findMinimum()).to.equal(min);
26-
heap.insert(4);
27-
expect(heap.findMinimum()).to.equal(min);
28-
heap.insert(5);
29-
expect(heap.findMinimum()).to.equal(min);
30-
31-
heap.insert(1);
32-
min = 1;
33-
expect(heap.findMinimum()).to.equal(min);
34-
35-
heap.insert(6);
36-
heap.insert(9);
37-
heap.insert(10);
38-
heap.insert(14);
39-
40-
heap.extract();
41-
heap.extract();
42-
heap.extract();
43-
heap.extract();
44-
45-
heap.isEmpty();
19+
it('inserts values in the MinHeap', () => {
20+
const resultArray = [];
21+
for (let i = 1; i < 10; i++) {
22+
resultArray.push(i);
23+
heap.insert(i);
24+
expect(heap.getAsArray()).to.deep.equal(resultArray);
25+
}
26+
});
4627

47-
const maxHeap = new MaxHeap<number>();
48-
maxHeap.insert(3);
49-
maxHeap.insert(2);
50-
maxHeap.insert(1);
51-
maxHeap.insert(4);
28+
it('finds the min value from the MinHeap', () => {
29+
const resultArray = [];
30+
for (let i = 10; i >= 1; i--) {
31+
resultArray.push(i);
32+
heap.insert(i);
33+
expect(heap.findMinimum()).to.equal(i);
34+
}
35+
});
5236

53-
maxHeap.extract();
54-
maxHeap.extract();
55-
maxHeap.extract();
56-
maxHeap.extract();
37+
it('performs heapify in the MinHeap', () => {
38+
const resultArray = [];
39+
for (let i = 10; i >= 1; i--) {
40+
resultArray.push(i);
41+
}
42+
expect(heap.heapify(resultArray)).to.deep.equal(resultArray);
43+
});
5744

58-
maxHeap.isEmpty();
45+
it('extracts the min value from the MinHeap', () => {
46+
let resultArray = [];
47+
for (let i = 1; i < 10; i++) {
48+
resultArray.push(i);
49+
heap.insert(i);
50+
expect(heap.getAsArray()).to.deep.equal(resultArray);
51+
}
52+
53+
resultArray = [
54+
[],
55+
[2, 4, 3, 8, 5, 6, 7, 9],
56+
[3, 4, 6, 8, 5, 9, 7],
57+
[4, 5, 6, 8, 7, 9],
58+
[5, 7 , 6, 8, 9],
59+
[6, 7, 9, 8],
60+
[7, 8, 9],
61+
[8, 9],
62+
[9],
63+
[]
64+
];
65+
66+
for (let i = 1; i < 10; i++) {
67+
expect(heap.extract()).to.equal(i);
68+
expect(heap.getAsArray()).to.deep.equal(resultArray[i]);
69+
}
5970
});
6071

6172
it('Heap Sort', () => {
6273
const array = [3, 2, 5, 6, 1, 7, 8, 9];
6374

64-
heapSort(array);
75+
expect(heapSort(array)).to.deep.equal([1, 2, 3, 5, 6, 7, 8, 9]);
6576
});
6677
});

0 commit comments

Comments
 (0)