Skip to content

Commit 33a1f14

Browse files
fix: minor coding style changes
1 parent 16fa8a1 commit 33a1f14

File tree

2 files changed

+116
-120
lines changed

2 files changed

+116
-120
lines changed

Data-Structures/Heap/KeyPriorityQueue.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@
1919
*/
2020

2121
// Priority Queue Helper functions
22-
function getParentPosition (position) {
23-
// Get the parent node of the current node
24-
return Math.floor((position - 1) / 2)
25-
}
26-
function getChildrenPosition (position) {
27-
// Get the children nodes of the current node
28-
return [2 * position + 1, 2 * position + 2]
29-
}
22+
const getParentPosition = position => Math.floor((position - 1) / 2)
23+
const getChildrenPositions = position => [2 * position + 1, 2 * position + 2]
3024

3125
class KeyPriorityQueue {
3226
// Priority Queue class using Minimum Binary Heap
@@ -86,7 +80,7 @@ class KeyPriorityQueue {
8680
const parentPos = getParentPosition(currPos)
8781
const currPriority = this._getPriorityOrInfinite(currPos)
8882
const parentPriority = this._getPriorityOrInfinite(parentPos)
89-
const [child1Pos, child2Pos] = getChildrenPosition(currPos)
83+
const [child1Pos, child2Pos] = getChildrenPositions(currPos)
9084
const child1Priority = this._getPriorityOrInfinite(child1Pos)
9185
const child2Priority = this._getPriorityOrInfinite(child2Pos)
9286

@@ -123,7 +117,7 @@ class KeyPriorityQueue {
123117
_shiftDown (position) {
124118
// Helper function to shift down a node to proper position (equivalent to bubbleDown)
125119
let currPos = position
126-
let [child1Pos, child2Pos] = getChildrenPosition(currPos)
120+
let [child1Pos, child2Pos] = getChildrenPositions(currPos)
127121
let child1Priority = this._getPriorityOrInfinite(child1Pos)
128122
let child2Priority = this._getPriorityOrInfinite(child2Pos)
129123
let currPriority = this._getPriorityOrInfinite(currPos)
@@ -140,7 +134,7 @@ class KeyPriorityQueue {
140134
this._swap(child2Pos, currPos)
141135
currPos = child2Pos
142136
}
143-
[child1Pos, child2Pos] = getChildrenPosition(currPos)
137+
[child1Pos, child2Pos] = getChildrenPositions(currPos)
144138
child1Priority = this._getPriorityOrInfinite(child1Pos)
145139
child2Priority = this._getPriorityOrInfinite(child2Pos)
146140
currPriority = this._getPriorityOrInfinite(currPos)
Lines changed: 111 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,126 @@
11
import { KeyPriorityQueue } from '../KeyPriorityQueue.js'
22

3-
describe('Method isEmpty', () => {
4-
test('Check heap is empty', () => {
5-
const queue = new KeyPriorityQueue()
6-
const res = queue.isEmpty()
7-
expect(res).toEqual(true)
8-
})
3+
describe('Key Priority Queue', () => {
4+
describe('Method isEmpty', () => {
5+
test('Check heap is empty', () => {
6+
const queue = new KeyPriorityQueue()
7+
const res = queue.isEmpty()
8+
expect(res).toEqual(true)
9+
})
910

10-
test('Check heap is not empty', () => {
11-
const queue = new KeyPriorityQueue()
12-
queue.push(0, 2)
13-
const res = queue.isEmpty()
14-
expect(res).toEqual(false)
11+
test('Check heap is not empty', () => {
12+
const queue = new KeyPriorityQueue()
13+
queue.push(0, 2)
14+
const res = queue.isEmpty()
15+
expect(res).toEqual(false)
16+
})
1517
})
16-
})
1718

18-
describe('Methods push and pop', () => {
19-
test('Test Case 1', () => {
20-
// create queue
21-
const queue = new KeyPriorityQueue()
22-
queue.push(0, 3)
23-
queue.push(1, 7)
24-
queue.push(2, 9)
25-
queue.push(3, 13)
26-
// create expected queue
27-
const expectedQueue = new KeyPriorityQueue()
28-
expectedQueue.push(1, 7)
29-
expectedQueue.push(3, 13)
30-
expectedQueue.push(2, 9)
31-
// result from popping element from the queue
32-
queue.pop()
33-
expect(queue).toEqual(expectedQueue)
34-
})
19+
describe('Methods push and pop', () => {
20+
test('Test Case 1', () => {
21+
// create queue
22+
const queue = new KeyPriorityQueue()
23+
queue.push(0, 3)
24+
queue.push(1, 7)
25+
queue.push(2, 9)
26+
queue.push(3, 13)
27+
// create expected queue
28+
const expectedQueue = new KeyPriorityQueue()
29+
expectedQueue.push(1, 7)
30+
expectedQueue.push(3, 13)
31+
expectedQueue.push(2, 9)
32+
// result from popping element from the queue
33+
queue.pop()
34+
expect(queue).toEqual(expectedQueue)
35+
})
3536

36-
test('Test Case 2', () => {
37-
// create queue
38-
const queue = new KeyPriorityQueue()
39-
queue.push(0, 3)
40-
queue.push(1, 9)
41-
queue.push(2, 7)
42-
queue.push(3, 13)
43-
// create expected queue
44-
const expectedQueue = new KeyPriorityQueue()
45-
expectedQueue.push(2, 7)
46-
expectedQueue.push(1, 9)
47-
expectedQueue.push(3, 13)
48-
// result from popping element from the queue
49-
queue.pop()
50-
expect(queue).toEqual(expectedQueue)
51-
})
37+
test('Test Case 2', () => {
38+
// create queue
39+
const queue = new KeyPriorityQueue()
40+
queue.push(0, 3)
41+
queue.push(1, 9)
42+
queue.push(2, 7)
43+
queue.push(3, 13)
44+
// create expected queue
45+
const expectedQueue = new KeyPriorityQueue()
46+
expectedQueue.push(2, 7)
47+
expectedQueue.push(1, 9)
48+
expectedQueue.push(3, 13)
49+
// result from popping element from the queue
50+
queue.pop()
51+
expect(queue).toEqual(expectedQueue)
52+
})
5253

53-
test('Test Case 3', () => {
54-
// create queue
55-
const queue = new KeyPriorityQueue()
56-
queue.push(0, 3)
57-
queue.push(1, 7)
58-
queue.push(2, 9)
59-
queue.push(3, 12)
60-
queue.push(4, 13)
61-
// create expected queue
62-
const expectedQueue = new KeyPriorityQueue()
63-
expectedQueue.push(1, 7)
64-
expectedQueue.push(3, 12)
65-
expectedQueue.push(2, 9)
66-
expectedQueue.push(4, 13)
67-
// result from popping element from the queue
68-
queue.pop()
69-
expect(queue).toEqual(expectedQueue)
54+
test('Test Case 3', () => {
55+
// create queue
56+
const queue = new KeyPriorityQueue()
57+
queue.push(0, 3)
58+
queue.push(1, 7)
59+
queue.push(2, 9)
60+
queue.push(3, 12)
61+
queue.push(4, 13)
62+
// create expected queue
63+
const expectedQueue = new KeyPriorityQueue()
64+
expectedQueue.push(1, 7)
65+
expectedQueue.push(3, 12)
66+
expectedQueue.push(2, 9)
67+
expectedQueue.push(4, 13)
68+
// result from popping element from the queue
69+
queue.pop()
70+
expect(queue).toEqual(expectedQueue)
71+
})
7072
})
71-
})
7273

73-
describe('Method contains', () => {
74-
test('Check heap does not contain element', () => {
75-
const queue = new KeyPriorityQueue()
76-
const res = queue.contains(0)
77-
expect(res).toEqual(false)
78-
})
74+
describe('Method contains', () => {
75+
test('Check heap does not contain element', () => {
76+
const queue = new KeyPriorityQueue()
77+
const res = queue.contains(0)
78+
expect(res).toEqual(false)
79+
})
7980

80-
test('Check heap contains element', () => {
81-
const queue = new KeyPriorityQueue()
82-
queue.push(0, 2)
83-
const res = queue.contains(0)
84-
expect(res).toEqual(true)
81+
test('Check heap contains element', () => {
82+
const queue = new KeyPriorityQueue()
83+
queue.push(0, 2)
84+
const res = queue.contains(0)
85+
expect(res).toEqual(true)
86+
})
8587
})
86-
})
8788

88-
describe('Method update', () => {
89-
test('Update without change in position', () => {
90-
// create queue
91-
const queue = new KeyPriorityQueue()
92-
queue.push(0, 3)
93-
queue.push(1, 5)
94-
queue.push(2, 7)
95-
queue.push(3, 11)
96-
// create expected queue
97-
const expectedQueue = new KeyPriorityQueue()
98-
expectedQueue.push(0, 2)
99-
expectedQueue.push(1, 5)
100-
expectedQueue.push(2, 7)
101-
expectedQueue.push(3, 11)
102-
// result from updating to similar priority
103-
queue.update(0, 2)
104-
expect(queue).toEqual(expectedQueue)
105-
})
89+
describe('Method update', () => {
90+
test('Update without change in position', () => {
91+
// create queue
92+
const queue = new KeyPriorityQueue()
93+
queue.push(0, 3)
94+
queue.push(1, 5)
95+
queue.push(2, 7)
96+
queue.push(3, 11)
97+
// create expected queue
98+
const expectedQueue = new KeyPriorityQueue()
99+
expectedQueue.push(0, 2)
100+
expectedQueue.push(1, 5)
101+
expectedQueue.push(2, 7)
102+
expectedQueue.push(3, 11)
103+
// result from updating to similar priority
104+
queue.update(0, 2)
105+
expect(queue).toEqual(expectedQueue)
106+
})
106107

107-
test('Update with change in position', () => {
108-
// create queue
109-
const queue = new KeyPriorityQueue()
110-
queue.push(0, 3)
111-
queue.push(1, 5)
112-
queue.push(2, 7)
113-
queue.push(3, 11)
114-
// create expected queue
115-
const expectedQueue = new KeyPriorityQueue()
116-
expectedQueue.push(1, 5)
117-
expectedQueue.push(3, 11)
118-
expectedQueue.push(2, 7)
119-
expectedQueue.push(0, 9)
120-
// result from updating to similar priority
121-
queue.update(0, 9)
122-
expect(queue).toEqual(expectedQueue)
108+
test('Update with change in position', () => {
109+
// create queue
110+
const queue = new KeyPriorityQueue()
111+
queue.push(0, 3)
112+
queue.push(1, 5)
113+
queue.push(2, 7)
114+
queue.push(3, 11)
115+
// create expected queue
116+
const expectedQueue = new KeyPriorityQueue()
117+
expectedQueue.push(1, 5)
118+
expectedQueue.push(3, 11)
119+
expectedQueue.push(2, 7)
120+
expectedQueue.push(0, 9)
121+
// result from updating to similar priority
122+
queue.update(0, 9)
123+
expect(queue).toEqual(expectedQueue)
124+
})
123125
})
124126
})

0 commit comments

Comments
 (0)