Skip to content

Commit

Permalink
feat(CV-0-1): wip, adding structures, features, and README to repos
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-jonathan committed Dec 4, 2023
1 parent cb5955e commit 846fc37
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
10 changes: 9 additions & 1 deletion __tests__/structures/Heap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ import {
} from 'vitest'

import {
buildMaxHeap,
heapSort,
buildMaxHeap,
// buildMinHeap,
} from '@/index'

describe('Heap', () => {
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
42 changes: 34 additions & 8 deletions src/structures/Heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,36 @@ export const heapSwapAt = <T>(nodes: T[], a: number, b: number): void => {
nodes[b] = temp
}

export const heapMaxHeapify = <T>(nodes: T[], size: number, index: number): void | never => {
export const heapMaxHeapify = <T>(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 = <T>(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) {
Expand All @@ -83,16 +102,23 @@ export const heapMaxHeapify = <T>(nodes: T[], size: number, index: number): void

export const buildMaxHeap = <T>(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 = <T>(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 = <T>(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)
}
}

0 comments on commit 846fc37

Please sign in to comment.