Skip to content

Commit b738c80

Browse files
authored
fix, test: Fix MinHeap sinkDown implementation, increase test coverage (TheAlgorithms#135)
1 parent 839efc7 commit b738c80

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

data_structures/heap/max_heap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class MaxHeap<T> extends Heap<T> {
3232
leftChildIndex: number,
3333
rightChildIndex: number
3434
): number {
35-
return (this.heap[leftChildIndex] || -Infinity) >
35+
return this.heap[leftChildIndex] >
3636
(this.heap[rightChildIndex] || -Infinity)
3737
? leftChildIndex
3838
: rightChildIndex;

data_structures/heap/min_heap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export class MinHeap<T> extends Heap<T> {
3232
leftChildIndex: number,
3333
rightChildIndex: number
3434
): number {
35-
return (this.heap[leftChildIndex] || -Infinity) <
36-
(this.heap[rightChildIndex] || -Infinity)
35+
return this.heap[leftChildIndex] <
36+
(this.heap[rightChildIndex] || Infinity)
3737
? leftChildIndex
3838
: rightChildIndex;
3939
}

data_structures/heap/test/max_heap.test.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { MaxHeap } from "../max_heap";
22

33
describe("MaxHeap", () => {
44
let heap: MaxHeap<number>;
5+
const elements: number[] = [
6+
12, 4, 43, 42, 9, 7, 39, 16, 55, 1, 51, 34, 81, 18,
7+
];
58

6-
beforeAll(() => {
7-
const elements: number[] = [
8-
12, 4, 43, 42, 9, 7, 39, 16, 55, 1, 51, 34, 81, 18,
9-
];
9+
beforeEach(() => {
1010
heap = new MaxHeap(elements);
1111
});
1212

@@ -26,4 +26,26 @@ describe("MaxHeap", () => {
2626
heap.insert(61);
2727
heap.check();
2828
});
29+
30+
const extract_all = (heap: MaxHeap<number>) => {
31+
[...elements].sort((a, b) => b - a).forEach((element: number) => {
32+
expect(heap.extract()).toEqual(element);
33+
});
34+
heap.check();
35+
expect(heap.size()).toEqual(0);
36+
}
37+
38+
it("should remove and return the max elements in order", () => {
39+
extract_all(heap);
40+
});
41+
42+
it("should insert all, then remove and return the max elements in order", () => {
43+
heap = new MaxHeap();
44+
elements.forEach((element: number) => {
45+
heap.insert(element);
46+
});
47+
heap.check();
48+
expect(heap.size()).toEqual(elements.length);
49+
extract_all(heap);
50+
});
2951
});

data_structures/heap/test/min_heap.test.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { MinHeap } from "../min_heap";
22

33
describe("MinHeap", () => {
44
let heap: MinHeap<number>;
5+
const elements: number[] = [
6+
12, 4, 43, 42, 9, 7, 39, 16, 55, 1, 51, 34, 81, 18,
7+
];
58

6-
beforeAll(() => {
7-
const elements: number[] = [
8-
12, 4, 43, 42, 9, 7, 39, 16, 55, 1, 51, 34, 81, 18,
9-
];
9+
beforeEach(() => {
1010
heap = new MinHeap(elements);
1111
});
1212

@@ -26,4 +26,26 @@ describe("MinHeap", () => {
2626
heap.insert(24);
2727
heap.check();
2828
});
29+
30+
const extract_all = (heap: MinHeap<number>) => {
31+
[...elements].sort((a, b) => a - b).forEach((element: number) => {
32+
expect(heap.extract()).toEqual(element);
33+
});
34+
heap.check();
35+
expect(heap.size()).toEqual(0);
36+
}
37+
38+
it("should remove and return the min elements in order", () => {
39+
extract_all(heap);
40+
});
41+
42+
it("should insert all, then remove and return the min elements in order", () => {
43+
heap = new MinHeap();
44+
elements.forEach((element: number) => {
45+
heap.insert(element);
46+
});
47+
heap.check();
48+
expect(heap.size()).toEqual(elements.length);
49+
extract_all(heap);
50+
});
2951
});

0 commit comments

Comments
 (0)