Skip to content

fix, test: Fix MinHeap sinkDown implementation, increase test coverage #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion data_structures/heap/max_heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class MaxHeap<T> extends Heap<T> {
leftChildIndex: number,
rightChildIndex: number
): number {
return (this.heap[leftChildIndex] || -Infinity) >
return this.heap[leftChildIndex] >
(this.heap[rightChildIndex] || -Infinity)
? leftChildIndex
: rightChildIndex;
Expand Down
4 changes: 2 additions & 2 deletions data_structures/heap/min_heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export class MinHeap<T> extends Heap<T> {
leftChildIndex: number,
rightChildIndex: number
): number {
return (this.heap[leftChildIndex] || -Infinity) <
(this.heap[rightChildIndex] || -Infinity)
return this.heap[leftChildIndex] <
(this.heap[rightChildIndex] || Infinity)
? leftChildIndex
: rightChildIndex;
}
Expand Down
30 changes: 26 additions & 4 deletions data_structures/heap/test/max_heap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { MaxHeap } from "../max_heap";

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

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

Expand All @@ -26,4 +26,26 @@ describe("MaxHeap", () => {
heap.insert(61);
heap.check();
});

const extract_all = (heap: MaxHeap<number>) => {
[...elements].sort((a, b) => b - a).forEach((element: number) => {
expect(heap.extract()).toEqual(element);
});
heap.check();
expect(heap.size()).toEqual(0);
}

it("should remove and return the max elements in order", () => {
extract_all(heap);
});

it("should insert all, then remove and return the max elements in order", () => {
heap = new MaxHeap();
elements.forEach((element: number) => {
heap.insert(element);
});
heap.check();
expect(heap.size()).toEqual(elements.length);
extract_all(heap);
});
});
30 changes: 26 additions & 4 deletions data_structures/heap/test/min_heap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { MinHeap } from "../min_heap";

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

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

Expand All @@ -26,4 +26,26 @@ describe("MinHeap", () => {
heap.insert(24);
heap.check();
});

const extract_all = (heap: MinHeap<number>) => {
[...elements].sort((a, b) => a - b).forEach((element: number) => {
expect(heap.extract()).toEqual(element);
});
heap.check();
expect(heap.size()).toEqual(0);
}

it("should remove and return the min elements in order", () => {
extract_all(heap);
});

it("should insert all, then remove and return the min elements in order", () => {
heap = new MinHeap();
elements.forEach((element: number) => {
heap.insert(element);
});
heap.check();
expect(heap.size()).toEqual(elements.length);
extract_all(heap);
});
});