SIGN IN SIGN UP

Algorithms and Data Structures implemented in JavaScript for beginners, following best practices.

34087 0 0 JavaScript
/* Minimum Priority Queue
* It is a part of heap data structure
* A heap is a specific tree based data structure
* in which all the nodes of tree are in a specific order.
* that is the children are arranged in some
* respect of their parents, can either be greater
* or less than the parent. This makes it a min priority queue
* or max priority queue.
*/
// Functions: insert, delete, peek, isEmpty, print, heapSort, sink
class MinPriorityQueue {
// calls the constructor and initializes the capacity
constructor(c) {
2020-05-03 09:05:12 +02:00
this.heap = []
this.capacity = c
this.size = 0
}
2020-05-03 09:05:12 +02:00
// inserts the key at the end and rearranges it
// so that the binary heap is in appropriate order
insert(key) {
2020-05-03 09:05:12 +02:00
if (this.isFull()) return
this.heap[this.size + 1] = key
let k = this.size + 1
while (k > 1) {
if (this.heap[k] < this.heap[Math.floor(k / 2)]) {
2020-05-03 09:05:12 +02:00
const temp = this.heap[k]
this.heap[k] = this.heap[Math.floor(k / 2)]
this.heap[Math.floor(k / 2)] = temp
}
2020-05-03 09:05:12 +02:00
k = Math.floor(k / 2)
}
2020-05-03 09:05:12 +02:00
this.size++
}
2020-05-03 09:05:12 +02:00
// returns the highest priority value
peek() {
2020-05-03 09:05:12 +02:00
return this.heap[1]
}
2020-05-03 09:05:12 +02:00
// returns boolean value whether the heap is empty or not
isEmpty() {
return this.size === 0
}
2020-05-03 09:05:12 +02:00
// returns boolean value whether the heap is full or not
isFull() {
return this.size === this.capacity
}
2020-05-03 09:05:12 +02:00
// prints the heap
print(output = (value) => console.log(value)) {
output(this.heap.slice(1))
}
merge: Fix spellings (#821) * chore: remove codespell from ci * feat: add codespell workflow * fix: codespell workflow * fix: ignore spellings in directory * chore: fix spellings ./Dynamic-Programming/KadaneAlgo.js:2: contiguos ==> contiguous ./Dynamic-Programming/KadaneAlgo.js:14: posible ==> possible * chore: fix spelling ./Dynamic-Programming/SieveOfEratosthenes.js:4: upto ==> up to * chore: fix spellings ./Dynamic-Programming/MaxNonAdjacentSum.js:22: Exmaple ==> Example * chore: fix spelling ./Project-Euler/test/Problem010.test.js:4: upto ==> up to ./Project-Euler/test/Problem010.test.js:8: upto ==> up to ./Project-Euler/test/Problem010.test.js:12: upto ==> up to * chore: fix spelling ./String/AlphaNumericPalindrome.js:10: recieves ==> receives ./String/AlphaNumericPalindrome.js:10: sting ==> string ./String/AlphaNumericPalindrome.js:46: varaible ==> variable * chore: fix spelling ./String/DiceCoefficient.js:3: stings ==> strings * chore: fix spelling ./String/test/DiceCoefficient.test.js:9: atleast ==> at least * chore: fix spelling ./String/test/MaxWord.test.js:8: ba ==> be * chore: ignore `PermutateString.test.js` * chore: fix spelling ./String/test/CheckVowels.test.js:62: occurances ==> occurrences * chore: ignore `SubsequenceRecursive.js` * chore: fix spelling ./Conversions/TemperatureConversion.js:2: arguement ==> argument * chore: fix spelling ./Conversions/RailwayTimeConversion.js:7: Formate ==> Format ./Conversions/RailwayTimeConversion.js:8: Formate ==> Format * chore: remove Linear Algebra The deleted directory hosted a package which are not accepted by this repository. * Auto-update DIRECTORY.md * chore: fix spelling * chore: fix spellings * merge: Created composite Simpson's integration method. Tests included. (#819) * Created composite Simpson's integration method.Tests included * Minor corrections * Auto-update DIRECTORY.md * Styled with standard.js * chore: remove blank line * chore: remove blank line Co-authored-by: ggkogkou <ggkogkou@ggkogkou.gr> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Rak Laptudirm <raklaptudirm@gmail.com> * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: remove codespell from ci * feat: add codespell workflow * fix: codespell workflow * fix: ignore spellings in directory * chore: fix spellings ./Dynamic-Programming/KadaneAlgo.js:2: contiguos ==> contiguous ./Dynamic-Programming/KadaneAlgo.js:14: posible ==> possible * chore: fix spelling ./Dynamic-Programming/SieveOfEratosthenes.js:4: upto ==> up to * chore: fix spellings ./Dynamic-Programming/MaxNonAdjacentSum.js:22: Exmaple ==> Example * chore: fix spelling ./Project-Euler/test/Problem010.test.js:4: upto ==> up to ./Project-Euler/test/Problem010.test.js:8: upto ==> up to ./Project-Euler/test/Problem010.test.js:12: upto ==> up to * chore: fix spelling ./String/AlphaNumericPalindrome.js:10: recieves ==> receives ./String/AlphaNumericPalindrome.js:10: sting ==> string ./String/AlphaNumericPalindrome.js:46: varaible ==> variable * chore: fix spelling ./String/DiceCoefficient.js:3: stings ==> strings * chore: fix spelling ./String/test/DiceCoefficient.test.js:9: atleast ==> at least * chore: fix spelling ./String/test/MaxWord.test.js:8: ba ==> be * chore: ignore `PermutateString.test.js` * chore: fix spelling ./String/test/CheckVowels.test.js:62: occurances ==> occurrences * chore: ignore `SubsequenceRecursive.js` * chore: fix spelling ./Conversions/TemperatureConversion.js:2: arguement ==> argument * chore: fix spelling ./Conversions/RailwayTimeConversion.js:7: Formate ==> Format ./Conversions/RailwayTimeConversion.js:8: Formate ==> Format * chore: remove Linear Algebra The deleted directory hosted a package which are not accepted by this repository. * Auto-update DIRECTORY.md * chore: fix spelling * chore: fix spellings * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: fix spelling * chore: no need to check filenames Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: ggkogkou <76820848+ggkogkou@users.noreply.github.com> Co-authored-by: ggkogkou <ggkogkou@ggkogkou.gr>
2021-10-28 15:37:43 +05:30
// heap reverse can be done by performing swapping the first
// element with the last, removing the last element to
// new array and calling sink function.
heapReverse() {
const heapSort = []
while (this.size > 0) {
// swap first element with last element
;[this.heap[1], this.heap[this.size]] = [
this.heap[this.size],
this.heap[1]
]
heapSort.push(this.heap.pop())
this.size--
this.sink()
2020-05-03 09:05:12 +02:00
}
// first value from heap it's empty to respect
// structure with 1 as index of the first element
this.heap = [undefined, ...heapSort.reverse()]
this.size = heapSort.length
}
2020-05-03 09:05:12 +02:00
// this function reorders the heap after every delete function
sink() {
2020-05-03 09:05:12 +02:00
let k = 1
while (2 * k <= this.size || 2 * k + 1 <= this.size) {
2020-05-03 09:05:12 +02:00
let minIndex
if (this.heap[2 * k] >= this.heap[k]) {
2020-05-03 09:05:12 +02:00
if (2 * k + 1 <= this.size && this.heap[2 * k + 1] >= this.heap[k]) {
break
} else if (2 * k + 1 > this.size) {
break
}
}
if (2 * k + 1 > this.size) {
2020-05-03 09:05:12 +02:00
minIndex = this.heap[2 * k] < this.heap[k] ? 2 * k : k
} else {
if (
this.heap[k] > this.heap[2 * k] ||
this.heap[k] > this.heap[2 * k + 1]
) {
minIndex = this.heap[2 * k] < this.heap[2 * k + 1] ? 2 * k : 2 * k + 1
} else {
2020-05-03 09:05:12 +02:00
minIndex = k
}
}
2020-05-03 09:05:12 +02:00
const temp = this.heap[k]
this.heap[k] = this.heap[minIndex]
this.heap[minIndex] = temp
k = minIndex
}
}
// deletes the highest priority value from the heap. The last
// element goes to ahead to first position and reorder heap
delete() {
// checks empty and one element array conditions
if (this.isEmpty()) return
if (this.size === 1) {
this.size--
return this.heap.pop()
}
2020-05-03 09:05:12 +02:00
const min = this.heap[1]
this.heap[1] = this.heap.pop()
2020-05-03 09:05:12 +02:00
this.size--
this.sink()
return min
}
}
export { MinPriorityQueue }