Skip to content

Commit

Permalink
Fix MaxHeap behaviour with overflow and objects.
Browse files Browse the repository at this point in the history
Fix insertionSort overflow and objects. Add tests with objects.
  • Loading branch information
HamsterCoder committed Nov 1, 2023
1 parent fdd0670 commit f62343f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 83 deletions.
85 changes: 5 additions & 80 deletions src/MaxHeap.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,11 @@
export class MaxHeap<T> {
items: T[] = [];
size: number = 0;
lessThan: (a: T, b: T) => boolean;
import { Heap } from "./Heap";

export class MaxHeap<T> extends Heap<T>{
constructor(lessThan: (a: T, b: T) => boolean, items?: T[]) {
this.lessThan = lessThan;
this.heapify(items);
}

heapify(items: T[] = []) {
for (let i = 0; i < items.length; i += 1) {
this.items[i + 1] = items[i];
}

this.size = items.length;

for (let i = Math.floor(this.items.length / 2); i > 0; i -= 1) {
this.balanceDown(i);
}
}

balanceUp(i: number): void {
let item = this.items[i];
let parentIndex = Math.floor(i / 2);

while (this.lessThan(this.items[parentIndex], item) && i > 1) {
this.items[i] = this.items[parentIndex];
this.items[parentIndex] = item;
i = parentIndex;
parentIndex = Math.floor(i / 2);
}
}

add(item: T): void {
// Insert in the last place
this.items[this.size + 1] = item;

// Balance Up
this.balanceUp(this.size + 1);

// Update size
this.size += 1;
}

balanceDown(i: number): void {
let item = this.items[i];
let leftChildIndex = 2 * i;
let rightChildIndex = 2 * i + 1;

while (this.lessThan(item, this.items[leftChildIndex]) || this.lessThan(item, this.items[rightChildIndex])) {
if (this.lessThan(this.items[leftChildIndex], this.items[rightChildIndex])) {
this.items[i] = this.items[rightChildIndex];
this.items[rightChildIndex] = item;
i = rightChildIndex;
} else {
this.items[i] = this.items[leftChildIndex];
this.items[leftChildIndex] = item;
i = leftChildIndex;
}

leftChildIndex = 2 * i;
rightChildIndex = 2 * i + 1;
}
super(lessThan, items);
}

removeMax(): T {
if (this.size === 0) {
throw new Error('MaxHeap: cannot remove max, heap is empty.');
}

const root = this.items[1];

// Promote new root
this.items[1] = this.items[this.size];

// Remove last element
this.items.length = this.size;
this.size -= 1;

// Balance new root
this.balanceDown(1);

return root;
return this.removeRoot();
}
}
}
21 changes: 19 additions & 2 deletions src/insertionSort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@ import { insertionSort } from "./insertionSort";
const testArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

function lessThan(a: number, b: number): boolean {
return a < b;
return a < b;
}

runGenericTests<number>('insertionSort', insertionSort, testArray, lessThan);
runGenericTests<number>(
"insertionSort.numbers",
insertionSort,
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
(a: number, b: number): boolean => {
return a < b;
}
);

runGenericTests<{key: number}>("insertionSort.generic", insertionSort, [
{key: 1},
{key: 2},
{key: 3},
{key: 4},
{key: 5},
], (a: {key: number}, b: {key: number}): boolean => {
return a.key < b.key;
});
2 changes: 1 addition & 1 deletion src/insertionSort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const insertionSort = function<T>(array: T[], lessThan: (a: T, b: T) => b
let curr = sortedArray[i];
let j = i - 1;

while (lessThan(curr, sortedArray[j])) {
while (j > -1 && lessThan(curr, sortedArray[j])) {
sortedArray[j + 1] = sortedArray[j];
sortedArray[j] = curr;
j -= 1;
Expand Down

0 comments on commit f62343f

Please sign in to comment.