Created
May 30, 2021 02:26
-
-
Save jcar787/775b6e06784a8f0d13211849fb8ab170 to your computer and use it in GitHub Desktop.
Implementation of MinHeap and MaxHeap in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createMinHeap = () => { | |
const items = []; | |
const percolateUp = index => { | |
const parent = Math.floor((index - 1) / 2); | |
if (index > 0 && items[parent] > items[index]) { | |
[items[parent], items[index]] = [items[index], items[parent]]; | |
percolateUp(parent); | |
} | |
}; | |
const insert = item => { | |
items.push(item); | |
percolateUp(items.length - 1); | |
}; | |
const minHeapify = index => { | |
const left = index * 2 + 1; | |
const right = index * 2 + 2; | |
let smallest = index; | |
if (items.length > left && items[smallest] > items[left]) { | |
smallest = left; | |
} | |
if (items.length > right && items[smallest] > items[right]) { | |
smallest = right; | |
} | |
if (smallest !== index) { | |
[items[smallest], items[index]] = [items[index], items[smallest]]; | |
minHeapify(smallest); | |
} | |
}; | |
const removeMin = () => { | |
if (items.length > 1) { | |
const min = items[0]; | |
items[0] = items.pop(); | |
minHeapify(0); | |
return min; | |
} else if (items.length === 1) { | |
return items.pop(); | |
} | |
}; | |
const getSize = () => items.length; | |
const isEmpty = () => getSize() === 0; | |
const getMin = () => !isEmpty() ? items[0] : null; | |
const printHeap = () => console.log(items, items.length); | |
return { | |
getMin, | |
getSize, | |
insert, | |
isEmpty, | |
printHeap, | |
removeMin, | |
}; | |
}; | |
const createMaxHeap = () => { | |
const items = []; | |
const percolateUp = index => { | |
const parent = Math.floor((index - 1) / 2); | |
if (index > 0 && items[parent] < items[index]) { | |
[items[parent], items[index]] = [items[index], items[parent]]; | |
percolateUp(parent); | |
} | |
}; | |
const insert = item => { | |
items.push(item); | |
percolateUp(items.length - 1); | |
}; | |
const maxHeapify = index => { | |
const left = index * 2 + 1; | |
const right = index * 2 + 2; | |
let largest = index; | |
if (items.length > left && items[largest] < items[left]) { | |
largest = left; | |
} | |
if (items.length > right && items[largest] < items[right]) { | |
largest = right; | |
} | |
if (largest !== index) { | |
[items[largest], items[index]] = [items[index], items[largest]]; | |
maxHeapify(largest); | |
} | |
}; | |
const removeMax = () => { | |
if (items.length > 1) { | |
const max = items[0]; | |
items[0] = items.pop(); | |
maxHeapify(0); | |
return max; | |
} else if (items.length === 1) { | |
return items.pop(); | |
} | |
}; | |
const getSize = () => items.length; | |
const isEmpty = () => getSize() === 0; | |
const getMax = () => !isEmpty() ? items[0] : null; | |
const printHeap = () => console.log(items, items.length); | |
return { | |
getMax, | |
getSize, | |
insert, | |
isEmpty, | |
printHeap, | |
removeMax, | |
}; | |
}; | |
module.exports = { | |
createMinHeap, | |
createMaxHeap | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment