Skip to content

Commit 112ea29

Browse files
authored
chore: combine min/max heap. allow custom compare function. (TheAlgorithms#138)
1 parent 10e98a9 commit 112ea29

File tree

5 files changed

+48
-97
lines changed

5 files changed

+48
-97
lines changed

data_structures/heap/heap.ts

+46-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
/**
2+
* A heap is a complete binary tree
3+
* In a complete binary tree each level is filled before lower levels are added
4+
* Each level is filled from left to right
5+
*
6+
* In a (min|max) heap the value of every node is (less|greater) than that if its children
7+
*
8+
* The heap if often implemented using an array structure.
9+
* In the array implementation, the relationship between a parent index and its two children
10+
* are ((parentindex * 2) + 1) and ((parentindex * 2) + 2)
11+
*
12+
*/
13+
114
export abstract class Heap<T> {
2-
protected heap: T[];
15+
private heap: T[];
16+
// A comparison function. Returns true if a should be the parent of b.
17+
private compare: (a: any, b: any) => boolean;
318

4-
constructor(elements: T[] = []) {
19+
constructor(elements: T[] = [], compare: (a: T, b: T) => boolean) {
520
this.heap = [];
21+
this.compare = compare;
622
for (let element of elements) {
723
this.insert(element);
824
}
@@ -14,19 +30,22 @@ export abstract class Heap<T> {
1430
* In a minHeap the value at parentIndex should be smaller than the value at childIndex
1531
*
1632
*/
17-
protected abstract isRightlyPlaced(
18-
childIndex: number,
19-
parentIndex: number
20-
): boolean;
33+
private isRightlyPlaced( childIndex: number, parentIndex: number) {
34+
return this.compare(this.heap[parentIndex], this.heap[childIndex]);
35+
}
2136

2237
/**
2338
* In a maxHeap the index with the larger value is returned
2439
* In a minHeap the index with the smaller value is returned
2540
*/
26-
protected abstract getChildIndexToSwap(
27-
leftChildIndex: number,
28-
rightChildIndex: number
29-
): number;
41+
private getChildIndexToSwap(leftChildIndex: number, rightChildIndex: number): number {
42+
if (rightChildIndex >= this.size()) {
43+
return leftChildIndex;
44+
}
45+
return this.compare(this.heap[leftChildIndex], this.heap[rightChildIndex])
46+
? leftChildIndex
47+
: rightChildIndex;
48+
}
3049

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

52-
protected bubbleUp(): void {
71+
private bubbleUp(): void {
5372
let index = this.size() - 1;
5473
let parentIndex;
5574

@@ -64,7 +83,7 @@ export abstract class Heap<T> {
6483
}
6584
}
6685

67-
protected sinkDown(): void {
86+
private sinkDown(): void {
6887
let index = 0;
6988
let leftChildIndex = this.getLeftChildIndex(index);
7089
let rightChildIndex = this.getRightChildIndex(index);
@@ -86,19 +105,19 @@ export abstract class Heap<T> {
86105
}
87106
}
88107

89-
protected getLeftChildIndex(index: number): number {
108+
private getLeftChildIndex(index: number): number {
90109
return index * 2 + 1;
91110
}
92111

93-
protected getRightChildIndex(index: number): number {
112+
private getRightChildIndex(index: number): number {
94113
return index * 2 + 2;
95114
}
96115

97116
public check(): void {
98117
return this._check();
99118
}
100119

101-
protected _check(index: number = 0): void {
120+
private _check(index: number = 0): void {
102121
if (!this.heap[index]) return;
103122
const leftChildIndex = this.getLeftChildIndex(index);
104123
const rightChildIndex = this.getRightChildIndex(index);
@@ -119,3 +138,15 @@ export abstract class Heap<T> {
119138
this._check(rightChildIndex);
120139
}
121140
}
141+
142+
export class MinHeap<T> extends Heap<T> {
143+
constructor(elements: T[] = [], compare = (a: T, b: T) => { return a < b }) {
144+
super(elements, compare);
145+
}
146+
}
147+
148+
export class MaxHeap<T> extends Heap<T> {
149+
constructor(elements: T[] = [], compare = (a: T, b: T) => { return a > b }) {
150+
super(elements, compare);
151+
}
152+
}

data_structures/heap/max_heap.ts

-40
This file was deleted.

data_structures/heap/min_heap.ts

-40
This file was deleted.

data_structures/heap/test/max_heap.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MaxHeap } from "../max_heap";
1+
import { MaxHeap } from "../heap";
22

33
describe("MaxHeap", () => {
44
let heap: MaxHeap<number>;

data_structures/heap/test/min_heap.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MinHeap } from "../min_heap";
1+
import { MinHeap } from "../heap";
22

33
describe("MinHeap", () => {
44
let heap: MinHeap<number>;

0 commit comments

Comments
 (0)