diff --git a/Data-Structures/Heap/MinPriorityQueue.js b/Data-Structures/Heap/MinPriorityQueue.js index 40b014a86..82de29129 100644 --- a/Data-Structures/Heap/MinPriorityQueue.js +++ b/Data-Structures/Heap/MinPriorityQueue.js @@ -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 @@ -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 diff --git a/Data-Structures/Heap/test/MinPriorityQueue.test.js b/Data-Structures/Heap/test/MinPriorityQueue.test.js index b91e70786..e79357e0f 100644 --- a/Data-Structures/Heap/test/MinPriorityQueue.test.js +++ b/Data-Structures/Heap/test/MinPriorityQueue.test.js @@ -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 @@ -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) @@ -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) + }) + }) })