mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-11 07:06:13 +08:00
feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
This commit is contained in:
@ -1,18 +1,18 @@
|
||||
/* 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.
|
||||
*/
|
||||
* 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) {
|
||||
constructor(c) {
|
||||
this.heap = []
|
||||
this.capacity = c
|
||||
this.size = 0
|
||||
@ -20,7 +20,7 @@ class MinPriorityQueue {
|
||||
|
||||
// inserts the key at the end and rearranges it
|
||||
// so that the binary heap is in appropriate order
|
||||
insert (key) {
|
||||
insert(key) {
|
||||
if (this.isFull()) return
|
||||
this.heap[this.size + 1] = key
|
||||
let k = this.size + 1
|
||||
@ -36,33 +36,36 @@ class MinPriorityQueue {
|
||||
}
|
||||
|
||||
// returns the highest priority value
|
||||
peek () {
|
||||
peek() {
|
||||
return this.heap[1]
|
||||
}
|
||||
|
||||
// returns boolean value whether the heap is empty or not
|
||||
isEmpty () {
|
||||
isEmpty() {
|
||||
return this.size === 0
|
||||
}
|
||||
|
||||
// returns boolean value whether the heap is full or not
|
||||
isFull () {
|
||||
isFull() {
|
||||
return this.size === this.capacity
|
||||
}
|
||||
|
||||
// prints the heap
|
||||
print (output = value => console.log(value)) {
|
||||
print(output = (value) => console.log(value)) {
|
||||
output(this.heap.slice(1))
|
||||
}
|
||||
|
||||
// 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 () {
|
||||
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]]
|
||||
;[this.heap[1], this.heap[this.size]] = [
|
||||
this.heap[this.size],
|
||||
this.heap[1]
|
||||
]
|
||||
heapSort.push(this.heap.pop())
|
||||
this.size--
|
||||
this.sink()
|
||||
@ -74,7 +77,7 @@ class MinPriorityQueue {
|
||||
}
|
||||
|
||||
// this function reorders the heap after every delete function
|
||||
sink () {
|
||||
sink() {
|
||||
let k = 1
|
||||
while (2 * k <= this.size || 2 * k + 1 <= this.size) {
|
||||
let minIndex
|
||||
@ -92,8 +95,7 @@ class MinPriorityQueue {
|
||||
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
|
||||
minIndex = this.heap[2 * k] < this.heap[2 * k + 1] ? 2 * k : 2 * k + 1
|
||||
} else {
|
||||
minIndex = k
|
||||
}
|
||||
@ -107,7 +109,7 @@ class MinPriorityQueue {
|
||||
|
||||
// deletes the highest priority value from the heap. The last
|
||||
// element goes to ahead to first position and reorder heap
|
||||
delete () {
|
||||
delete() {
|
||||
// checks empty and one element array conditions
|
||||
if (this.isEmpty()) return
|
||||
if (this.size === 1) {
|
||||
|
Reference in New Issue
Block a user