Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4fb0809

Browse files
merge: fixes: #774 - add test and fix MinPriorityQueue implementation (#811)
1 parent 32781c2 commit 4fb0809

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

‎Data-Structures/Heap/MinPriorityQueue.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,22 @@ class MinPriorityQueue {
5757
output(this.heap.slice(1))
5858
}
5959

60-
// heap sorting can be done by performing
61-
// delete function to the number of times of the size of the heap
62-
// it returns reverse sort because it is a min priority queue
63-
heapSort () {
64-
for (let i = 1; i < this.capacity; i++) {
65-
this.delete()
60+
// heap reverse can be done by performing swaping the first
61+
// element with the last, removing the last element to
62+
// new array and calling sink function.
63+
heapReverse () {
64+
const heapSort = []
65+
while (this.size > 0) {
66+
// swap first element with last element
67+
[this.heap[1], this.heap[this.size]] = [this.heap[this.size], this.heap[1]]
68+
heapSort.push(this.heap.pop())
69+
this.size--
70+
this.sink()
6671
}
72+
// first value from heap it's empty to respect
73+
// structure with 1 as index of the first element
74+
this.heap = [undefined, ...heapSort.reverse()]
75+
this.size = heapSort.length
6776
}
6877

6978
// this function reorders the heap after every delete function
@@ -98,11 +107,17 @@ class MinPriorityQueue {
98107
}
99108
}
100109

101-
// deletes the highest priority value from the heap
110+
// deletes the highest priority value from the heap. The last
111+
// element goes to ahead to first position and reorder heap
102112
delete () {
113+
// checks empty and one element array conditions
114+
if (this.isEmpty()) return
115+
if (this.size === 1) {
116+
this.size--
117+
return this.heap.pop()
118+
}
103119
const min = this.heap[1]
104-
this.heap[1] = this.heap[this.size]
105-
this.heap[this.size] = min
120+
this.heap[1] = this.heap.pop()
106121
this.size--
107122
this.sink()
108123
return min

‎Data-Structures/Heap/test/MinPriorityQueue.test.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import { MinPriorityQueue } from '../MinPriorityQueue'
33
describe('MinPriorityQueue', () => {
44
const values = [5, 2, 4, 1, 7, 6, 3, 8]
55
const capacity = values.length
6+
let queue
67

7-
const Queue = new MinPriorityQueue(capacity)
8-
9-
values.forEach(v => Queue.insert(v))
8+
beforeEach(() => {
9+
queue = new MinPriorityQueue(capacity)
10+
values.forEach(v => queue.insert(v))
11+
})
1012

1113
it('Check heap ordering', () => {
1214
const mockFn = jest.fn()
13-
Queue.print(mockFn)
15+
queue.print(mockFn)
1416

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

2325
it('heapSort() expected to reverse the heap ordering', () => {
24-
Queue.heapSort()
25-
26+
queue.heapReverse()
2627
const mockFn = jest.fn()
27-
Queue.print(mockFn)
28+
queue.print(mockFn)
2829

2930
expect(mockFn.mock.calls.length).toBe(1)
3031
expect(mockFn.mock.calls[0].length).toBe(1)
@@ -33,4 +34,22 @@ describe('MinPriorityQueue', () => {
3334
expect(heap.length).toBe(capacity)
3435
expect(heap).toStrictEqual([8, 7, 6, 5, 4, 3, 2, 1])
3536
})
37+
38+
describe('delete() function work properly', () => {
39+
it('return undefined if heap is empty', () => {
40+
const minqueue = new MinPriorityQueue(capacity)
41+
const min = minqueue.delete()
42+
expect(min).toBe(undefined)
43+
})
44+
it('return min value and remove it', () => {
45+
const sortedValues = values.sort()
46+
let initialSize = queue.size
47+
sortedValues.forEach((minValue, index) => {
48+
const min = queue.delete()
49+
expect(min).toBe(minValue)
50+
expect(queue.size).toBe(--initialSize)
51+
})
52+
expect(queue.size).toBe(0)
53+
})
54+
})
3655
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /