mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
This commit is contained in:

committed by
GitHub

parent
32781c2aa1
commit
4fb0809666
@ -57,13 +57,22 @@ class MinPriorityQueue {
|
|||||||
output(this.heap.slice(1))
|
output(this.heap.slice(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
// heap sorting can be done by performing
|
// heap reverse can be done by performing swaping the first
|
||||||
// delete function to the number of times of the size of the heap
|
// element with the last, removing the last element to
|
||||||
// it returns reverse sort because it is a min priority queue
|
// new array and calling sink function.
|
||||||
heapSort () {
|
heapReverse () {
|
||||||
for (let i = 1; i < this.capacity; i++) {
|
const heapSort = []
|
||||||
this.delete()
|
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()
|
||||||
}
|
}
|
||||||
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// this function reorders the heap after every delete function
|
// this function reorders the heap after every delete function
|
||||||
@ -98,11 +107,17 @@ class MinPriorityQueue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// deletes the highest priority value from the heap
|
// 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) {
|
||||||
|
this.size--
|
||||||
|
return this.heap.pop()
|
||||||
|
}
|
||||||
const min = this.heap[1]
|
const min = this.heap[1]
|
||||||
this.heap[1] = this.heap[this.size]
|
this.heap[1] = this.heap.pop()
|
||||||
this.heap[this.size] = min
|
|
||||||
this.size--
|
this.size--
|
||||||
this.sink()
|
this.sink()
|
||||||
return min
|
return min
|
||||||
|
@ -3,14 +3,16 @@ import { MinPriorityQueue } from '../MinPriorityQueue'
|
|||||||
describe('MinPriorityQueue', () => {
|
describe('MinPriorityQueue', () => {
|
||||||
const values = [5, 2, 4, 1, 7, 6, 3, 8]
|
const values = [5, 2, 4, 1, 7, 6, 3, 8]
|
||||||
const capacity = values.length
|
const capacity = values.length
|
||||||
|
let queue
|
||||||
|
|
||||||
const Queue = new MinPriorityQueue(capacity)
|
beforeEach(() => {
|
||||||
|
queue = new MinPriorityQueue(capacity)
|
||||||
values.forEach(v => Queue.insert(v))
|
values.forEach(v => queue.insert(v))
|
||||||
|
})
|
||||||
|
|
||||||
it('Check heap ordering', () => {
|
it('Check heap ordering', () => {
|
||||||
const mockFn = jest.fn()
|
const mockFn = jest.fn()
|
||||||
Queue.print(mockFn)
|
queue.print(mockFn)
|
||||||
|
|
||||||
expect(mockFn.mock.calls.length).toBe(1) // Expect one call
|
expect(mockFn.mock.calls.length).toBe(1) // Expect one call
|
||||||
expect(mockFn.mock.calls[0].length).toBe(1) // Expect one argument
|
expect(mockFn.mock.calls[0].length).toBe(1) // Expect one argument
|
||||||
@ -21,10 +23,9 @@ describe('MinPriorityQueue', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('heapSort() expected to reverse the heap ordering', () => {
|
it('heapSort() expected to reverse the heap ordering', () => {
|
||||||
Queue.heapSort()
|
queue.heapReverse()
|
||||||
|
|
||||||
const mockFn = jest.fn()
|
const mockFn = jest.fn()
|
||||||
Queue.print(mockFn)
|
queue.print(mockFn)
|
||||||
|
|
||||||
expect(mockFn.mock.calls.length).toBe(1)
|
expect(mockFn.mock.calls.length).toBe(1)
|
||||||
expect(mockFn.mock.calls[0].length).toBe(1)
|
expect(mockFn.mock.calls[0].length).toBe(1)
|
||||||
@ -33,4 +34,22 @@ describe('MinPriorityQueue', () => {
|
|||||||
expect(heap.length).toBe(capacity)
|
expect(heap.length).toBe(capacity)
|
||||||
expect(heap).toStrictEqual([8, 7, 6, 5, 4, 3, 2, 1])
|
expect(heap).toStrictEqual([8, 7, 6, 5, 4, 3, 2, 1])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('delete() function work properly', () => {
|
||||||
|
it('return undefined if heap is empty', () => {
|
||||||
|
const minqueue = new MinPriorityQueue(capacity)
|
||||||
|
const min = minqueue.delete()
|
||||||
|
expect(min).toBe(undefined)
|
||||||
|
})
|
||||||
|
it('return min value and remove it', () => {
|
||||||
|
const sortedValues = values.sort()
|
||||||
|
let initialSize = queue.size
|
||||||
|
sortedValues.forEach((minValue, index) => {
|
||||||
|
const min = queue.delete()
|
||||||
|
expect(min).toBe(minValue)
|
||||||
|
expect(queue.size).toBe(--initialSize)
|
||||||
|
})
|
||||||
|
expect(queue.size).toBe(0)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user