Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions Data-Structures/Heap/MinPriorityQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,22 @@ class MinPriorityQueue {
output(this.heap.slice(1))
}

// heap sorting can be done by performing
// delete function to the number of times of the size of the heap
// it returns reverse sort because it is a min priority queue
heapSort () {
for (let i = 1; i < this.capacity; i++) {
this.delete()
// heap reverse can be done by performing swaping 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()
}
// 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
Expand Down Expand Up @@ -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 () {
// 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]
this.heap[1] = this.heap[this.size]
this.heap[this.size] = min
this.heap[1] = this.heap.pop()
this.size--
this.sink()
return min
Expand Down
33 changes: 26 additions & 7 deletions Data-Structures/Heap/test/MinPriorityQueue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { MinPriorityQueue } from '../MinPriorityQueue'
describe('MinPriorityQueue', () => {
const values = [5, 2, 4, 1, 7, 6, 3, 8]
const capacity = values.length
let queue

const Queue = new MinPriorityQueue(capacity)

values.forEach(v => Queue.insert(v))
beforeEach(() => {
queue = new MinPriorityQueue(capacity)
values.forEach(v => queue.insert(v))
})

it('Check heap ordering', () => {
const mockFn = jest.fn()
Queue.print(mockFn)
queue.print(mockFn)

expect(mockFn.mock.calls.length).toBe(1) // Expect one call
expect(mockFn.mock.calls[0].length).toBe(1) // Expect one argument
Expand All @@ -21,10 +23,9 @@ describe('MinPriorityQueue', () => {
})

it('heapSort() expected to reverse the heap ordering', () => {
Queue.heapSort()

queue.heapReverse()
const mockFn = jest.fn()
Queue.print(mockFn)
queue.print(mockFn)

expect(mockFn.mock.calls.length).toBe(1)
expect(mockFn.mock.calls[0].length).toBe(1)
Expand All @@ -33,4 +34,22 @@ describe('MinPriorityQueue', () => {
expect(heap.length).toBe(capacity)
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)
})
})
})