Skip to content

chore: combine min/max heap. allow custom compare function. make some functions private. #138

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 16, 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
61 changes: 46 additions & 15 deletions data_structures/heap/heap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
/**
* A heap is a complete binary tree
* In a complete binary tree each level is filled before lower levels are added
* Each level is filled from left to right
*
* In a (min|max) heap the value of every node is (less|greater) than that if its children
*
* The heap if often implemented using an array structure.
* In the array implementation, the relationship between a parent index and its two children
* are ((parentindex * 2) + 1) and ((parentindex * 2) + 2)
*
*/

export abstract class Heap<T> {
protected heap: T[];
private heap: T[];
// A comparison function. Returns true if a should be the parent of b.
private compare: (a: any, b: any) => boolean;

constructor(elements: T[] = []) {
constructor(elements: T[] = [], compare: (a: T, b: T) => boolean) {
this.heap = [];
this.compare = compare;
for (let element of elements) {
this.insert(element);
}
Expand All @@ -14,19 +30,22 @@ export abstract class Heap<T> {
* In a minHeap the value at parentIndex should be smaller than the value at childIndex
*
*/
protected abstract isRightlyPlaced(
childIndex: number,
parentIndex: number
): boolean;
private isRightlyPlaced( childIndex: number, parentIndex: number) {
return this.compare(this.heap[parentIndex], this.heap[childIndex]);
}

/**
* In a maxHeap the index with the larger value is returned
* In a minHeap the index with the smaller value is returned
*/
protected abstract getChildIndexToSwap(
leftChildIndex: number,
rightChildIndex: number
): number;
private getChildIndexToSwap(leftChildIndex: number, rightChildIndex: number): number {
if (rightChildIndex >= this.size()) {
return leftChildIndex;
}
return this.compare(this.heap[leftChildIndex], this.heap[rightChildIndex])
? leftChildIndex
: rightChildIndex;
}

public insert(value: T): void {
this.heap.push(value);
Expand All @@ -49,7 +68,7 @@ export abstract class Heap<T> {
return this.size() === 0;
}

protected bubbleUp(): void {
private bubbleUp(): void {
let index = this.size() - 1;
let parentIndex;

Expand All @@ -64,7 +83,7 @@ export abstract class Heap<T> {
}
}

protected sinkDown(): void {
private sinkDown(): void {
let index = 0;
let leftChildIndex = this.getLeftChildIndex(index);
let rightChildIndex = this.getRightChildIndex(index);
Expand All @@ -86,19 +105,19 @@ export abstract class Heap<T> {
}
}

protected getLeftChildIndex(index: number): number {
private getLeftChildIndex(index: number): number {
return index * 2 + 1;
}

protected getRightChildIndex(index: number): number {
private getRightChildIndex(index: number): number {
return index * 2 + 2;
}

public check(): void {
return this._check();
}

protected _check(index: number = 0): void {
private _check(index: number = 0): void {
if (!this.heap[index]) return;
const leftChildIndex = this.getLeftChildIndex(index);
const rightChildIndex = this.getRightChildIndex(index);
Expand All @@ -119,3 +138,15 @@ export abstract class Heap<T> {
this._check(rightChildIndex);
}
}

export class MinHeap<T> extends Heap<T> {
constructor(elements: T[] = [], compare = (a: T, b: T) => { return a < b }) {
super(elements, compare);
}
}

export class MaxHeap<T> extends Heap<T> {
constructor(elements: T[] = [], compare = (a: T, b: T) => { return a > b }) {
super(elements, compare);
}
}
40 changes: 0 additions & 40 deletions data_structures/heap/max_heap.ts

This file was deleted.

40 changes: 0 additions & 40 deletions data_structures/heap/min_heap.ts

This file was deleted.

2 changes: 1 addition & 1 deletion data_structures/heap/test/max_heap.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MaxHeap } from "../max_heap";
import { MaxHeap } from "../heap";

describe("MaxHeap", () => {
let heap: MaxHeap<number>;
Expand Down
2 changes: 1 addition & 1 deletion data_structures/heap/test/min_heap.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MinHeap } from "../min_heap";
import { MinHeap } from "../heap";

describe("MinHeap", () => {
let heap: MinHeap<number>;
Expand Down