diff --git a/__tests__/structures/Heap.spec.ts b/__tests__/structures/Heap.spec.ts index 4e97367..3e36230 100644 --- a/__tests__/structures/Heap.spec.ts +++ b/__tests__/structures/Heap.spec.ts @@ -37,8 +37,9 @@ import { } from 'vitest' import { - buildMaxHeap, heapSort, + buildMaxHeap, + // buildMinHeap, } from '@/index' describe('Heap', () => { @@ -49,6 +50,13 @@ describe('Heap', () => { expect(nodes).toStrictEqual([ 16, 14, 10, 8, 7, 9, 3, 2, 4, 1 ]) }) + // it('buildMinHeap', () => { + // const nodes = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ] + // buildMinHeap(nodes) + // + // expect(nodes).toStrictEqual([ 1, 4, 2, 3, 9, 7, 8, 10, 14, 16 ]) + // }) + it('heapSort', () => { const nodes = [ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 ] heapSort(nodes) diff --git a/package.json b/package.json index 99a2313..a81eb0b 100644 --- a/package.json +++ b/package.json @@ -45,10 +45,10 @@ "test:coverage": "vitest run --coverage --config vite.config.test.ts --mode ${VITE_MODE:-benchmark}" }, "dependencies": { - "@cosmicmind/foundationjs": "^0.0.1-rc-092723-1" + "@cosmicmind/foundationjs": "^0.0.1-rc-120323-1" }, "devDependencies": { - "@cosmicmind/foundationjs": "^0.0.1-rc-092723-1", + "@cosmicmind/foundationjs": "^0.0.1-rc-120323-1", "@microsoft/eslint-formatter-sarif": "^3.0.0", "@types/node": "^18.17.17", "@typescript-eslint/eslint-plugin": "^5.62.0", diff --git a/src/structures/Heap.ts b/src/structures/Heap.ts index f84efd0..6f3464f 100644 --- a/src/structures/Heap.ts +++ b/src/structures/Heap.ts @@ -62,17 +62,36 @@ export const heapSwapAt = (nodes: T[], a: number, b: number): void => { nodes[b] = temp } -export const heapMaxHeapify = (nodes: T[], size: number, index: number): void | never => { +export const heapMaxHeapify = (nodes: T[], size: number, index= 0): void | never => { assert(0 <= size, 'size must be 0 or greater') assert(0 <= index, 'index must be 0 or greater') - const l = heapLeft(index) - const r = heapRight(index) + const left = heapLeft(index) + const right = heapRight(index) - let largest = l < size && nodes[l] > nodes[index] ? l : index + let largest = left < size && nodes[left] > nodes[index] ? left : index - if (r < size && nodes[r] > nodes[largest]) { - largest = r + if (right < size && nodes[right] > nodes[largest]) { + largest = right + } + + if (index !== largest) { + heapSwapAt(nodes, index, largest) + heapMaxHeapify(nodes, size, largest) + } +} + +export const heapMinHeapify = (nodes: T[], size: number, index= 0): void | never => { + assert(0 <= size, 'size must be 0 or greater') + assert(0 <= index, 'index must be 0 or greater') + + const left = heapLeft(index) + const right = heapRight(index) + + let largest = left < size && nodes[left] > nodes[index] ? left : index + + if (right < size && nodes[right] > nodes[largest]) { + largest = right } if (index !== largest) { @@ -83,16 +102,23 @@ export const heapMaxHeapify = (nodes: T[], size: number, index: number): void export const buildMaxHeap = (nodes: T[]): void | never => { const size = nodes.length - for (let i = Math.floor(size / 2); 0 <= i; --i) { + for (let i = Math.floor(size / 2) - 1; 0 <= i; --i) { heapMaxHeapify(nodes, size, i) } } +export const buildMinHeap = (nodes: T[]): void | never => { + const size = nodes.length + for (let i = Math.floor(size / 2); 0 <= i; --i) { + heapMinHeapify(nodes, size, i) + } +} + export const heapSort = (nodes: T[]): void | never => { buildMaxHeap(nodes) for (let i = nodes.length - 1; 0 < i; --i) { heapSwapAt(nodes, 0, i) - heapMaxHeapify(nodes, i, 0) + heapMaxHeapify(nodes, i) } } \ No newline at end of file