Skip to content

Commit 16fa8a1

Browse files
fix: format files TheAlgorithms#1298
1 parent 3f3eacd commit 16fa8a1

File tree

4 files changed

+50
-60
lines changed

4 files changed

+50
-60
lines changed

Data-Structures/Heap/KeyPriorityQueue.js

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/**
22
* KeyPriorityQueue is a priority queue based on a Minimum Binary Heap.
3-
*
4-
* Minimum Binary Heaps are binary trees which are filled level by level
3+
*
4+
* Minimum Binary Heaps are binary trees which are filled level by level
55
* and then from left to right inside a depth level.
6-
* Their main property is that any parent node has a smaller or equal priority to all of its children,
6+
* Their main property is that any parent node has a smaller or equal priority to all of its children,
77
* hence the root of the tree always has the smallest priority of all nodes.
8-
*
8+
*
99
* This implementation of the Minimum Binary Heap allows for nodes to contain both a key and a priority.
1010
* WARNING: keys must be Integers as they are used as array indices.
11-
*
12-
* In this implementation, the heap is represented by an array with nodes ordered
11+
*
12+
* In this implementation, the heap is represented by an array with nodes ordered
1313
* from root-to-leaf, left-to-right.
1414
* Therefore, the parent-child node relationship is such that
1515
* * the children nodes positions relative to their parent are: (parentPos * 2 + 1) and (parentPos * 2 + 2)
@@ -18,38 +18,37 @@
1818
* More information and visuals on Binary Heaps can be found here: https://www.geeksforgeeks.org/binary-heap/
1919
*/
2020

21-
2221
// Priority Queue Helper functions
23-
function getParentPosition(position) {
22+
function getParentPosition (position) {
2423
// Get the parent node of the current node
2524
return Math.floor((position - 1) / 2)
2625
}
27-
function getChildrenPosition(position) {
26+
function getChildrenPosition (position) {
2827
// Get the children nodes of the current node
2928
return [2 * position + 1, 2 * position + 2]
3029
}
3130

3231
class KeyPriorityQueue {
3332
// Priority Queue class using Minimum Binary Heap
34-
constructor() {
33+
constructor () {
3534
this._heap = []
3635
this.keys = {}
3736
}
3837

3938
/**
4039
* Checks if the heap is empty
41-
* @returns boolean
40+
* @returns boolean
4241
*/
43-
isEmpty() {
42+
isEmpty () {
4443
return this._heap.length === 0
4544
}
4645

4746
/**
4847
* Adds an element to the queue
49-
* @param {number} key
50-
* @param {number} priority
48+
* @param {number} key
49+
* @param {number} priority
5150
*/
52-
push(key, priority) {
51+
push (key, priority) {
5352
this._heap.push([key, priority])
5453
this.keys[key] = this._heap.length - 1
5554
this._shiftUp(this.keys[key])
@@ -59,7 +58,7 @@ class KeyPriorityQueue {
5958
* Removes the element with least priority
6059
* @returns the key of the element with least priority
6160
*/
62-
pop() {
61+
pop () {
6362
this._swap(0, this._heap.length - 1)
6463
const [key] = this._heap.pop()
6564
delete this.keys[key]
@@ -69,10 +68,10 @@ class KeyPriorityQueue {
6968

7069
/**
7170
* Checks whether a given key is present in the queue
72-
* @param {number} key
71+
* @param {number} key
7372
* @returns boolean
7473
*/
75-
contains(key) {
74+
contains (key) {
7675
return (key in this.keys)
7776
}
7877

@@ -81,7 +80,7 @@ class KeyPriorityQueue {
8180
* @param {number} key the element to change
8281
* @param {number} priority new priority of the element
8382
*/
84-
update(key, priority) {
83+
update (key, priority) {
8584
const currPos = this.keys[key]
8685
this._heap[currPos][1] = priority
8786
const parentPos = getParentPosition(currPos)
@@ -100,15 +99,11 @@ class KeyPriorityQueue {
10099

101100
_getPriorityOrInfinite (position) {
102101
// Helper function, returns priority of the node, or Infinite if no node corresponds to this position
103-
if (position >= 0 && position < this._heap.length) {
104-
return this._heap[position][1]
105-
}
106-
else {
107-
return Infinity
108-
}
102+
if (position >= 0 && position < this._heap.length) return this._heap[position][1]
103+
else return Infinity
109104
}
110105

111-
_shiftUp(position) {
106+
_shiftUp (position) {
112107
// Helper function to shift up a node to proper position (equivalent to bubbleUp)
113108
let currPos = position
114109
let parentPos = getParentPosition(currPos)
@@ -121,20 +116,19 @@ class KeyPriorityQueue {
121116
parentPos = getParentPosition(currPos)
122117
currPriority = this._getPriorityOrInfinite(currPos)
123118
parentPriority = this._getPriorityOrInfinite(parentPos)
124-
125119
}
126120
this.keys[this._heap[currPos][0]] = currPos
127121
}
128122

129-
_shiftDown(position) {
123+
_shiftDown (position) {
130124
// Helper function to shift down a node to proper position (equivalent to bubbleDown)
131125
let currPos = position
132126
let [child1Pos, child2Pos] = getChildrenPosition(currPos)
133127
let child1Priority = this._getPriorityOrInfinite(child1Pos)
134128
let child2Priority = this._getPriorityOrInfinite(child2Pos)
135129
let currPriority = this._getPriorityOrInfinite(currPos)
136130

137-
if (currPriority == Infinity) {
131+
if (currPriority === Infinity) {
138132
return
139133
}
140134

@@ -153,12 +147,12 @@ class KeyPriorityQueue {
153147
}
154148
}
155149

156-
_swap(position1, position2) {
150+
_swap (position1, position2) {
157151
// Helper function to swap 2 nodes
158152
[this._heap[position1], this._heap[position2]] = [this._heap[position2], this._heap[position1]]
159153
this.keys[this._heap[position1][0]] = position1
160154
this.keys[this._heap[position2][0]] = position2
161155
}
162156
}
163157

164-
export { KeyPriorityQueue }
158+
export { KeyPriorityQueue }

Data-Structures/Heap/test/KeyPriorityQueue.test.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import { KeyPriorityQueue } from '../KeyPriorityQueue.js'
22

33
describe('Method isEmpty', () => {
4-
54
test('Check heap is empty', () => {
65
const queue = new KeyPriorityQueue()
7-
let res = queue.isEmpty()
6+
const res = queue.isEmpty()
87
expect(res).toEqual(true)
98
})
109

1110
test('Check heap is not empty', () => {
1211
const queue = new KeyPriorityQueue()
1312
queue.push(0, 2)
14-
let res = queue.isEmpty()
13+
const res = queue.isEmpty()
1514
expect(res).toEqual(false)
1615
})
1716
})
1817

1918
describe('Methods push and pop', () => {
20-
2119
test('Test Case 1', () => {
2220
// create queue
2321
const queue = new KeyPriorityQueue()
@@ -34,7 +32,7 @@ describe('Methods push and pop', () => {
3432
queue.pop()
3533
expect(queue).toEqual(expectedQueue)
3634
})
37-
35+
3836
test('Test Case 2', () => {
3937
// create queue
4038
const queue = new KeyPriorityQueue()
@@ -51,7 +49,7 @@ describe('Methods push and pop', () => {
5149
queue.pop()
5250
expect(queue).toEqual(expectedQueue)
5351
})
54-
52+
5553
test('Test Case 3', () => {
5654
// create queue
5755
const queue = new KeyPriorityQueue()
@@ -73,23 +71,21 @@ describe('Methods push and pop', () => {
7371
})
7472

7573
describe('Method contains', () => {
76-
7774
test('Check heap does not contain element', () => {
7875
const queue = new KeyPriorityQueue()
79-
let res = queue.contains(0)
76+
const res = queue.contains(0)
8077
expect(res).toEqual(false)
8178
})
8279

8380
test('Check heap contains element', () => {
8481
const queue = new KeyPriorityQueue()
8582
queue.push(0, 2)
86-
let res = queue.contains(0)
83+
const res = queue.contains(0)
8784
expect(res).toEqual(true)
8885
})
8986
})
9087

9188
describe('Method update', () => {
92-
9389
test('Update without change in position', () => {
9490
// create queue
9591
const queue = new KeyPriorityQueue()
@@ -125,4 +121,4 @@ describe('Method update', () => {
125121
queue.update(0, 9)
126122
expect(queue).toEqual(expectedQueue)
127123
})
128-
})
124+
})

Graphs/PrimMST.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { KeyPriorityQueue } from "../Data-Structures/Heap/KeyPriorityQueue"
1+
import { KeyPriorityQueue } from '../Data-Structures/Heap/KeyPriorityQueue'
22
class GraphWeightedUndirectedAdjacencyList {
33
// Weighted Undirected Graph class
44
constructor () {

Graphs/test/PrimMST.test.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { GraphWeightedUndirectedAdjacencyList } from '../PrimMST.js'
22

33
test('Test Case PrimMST 1', () => {
4-
// create graph to compute MST on
5-
const graph = new GraphWeightedUndirectedAdjacencyList()
6-
graph.addEdge(1, 2, 1)
7-
graph.addEdge(2, 3, 2)
8-
graph.addEdge(3, 4, 1)
9-
graph.addEdge(3, 5, 100) // Removed in MST
10-
graph.addEdge(4, 5, 5)
11-
// create expected graph
12-
const expectedGraph = new GraphWeightedUndirectedAdjacencyList()
13-
expectedGraph.addEdge(1, 2, 1)
14-
expectedGraph.addEdge(2, 3, 2)
15-
expectedGraph.addEdge(3, 4, 1)
16-
expectedGraph.addEdge(4, 5, 5)
17-
// result from MST
18-
const res = graph.PrimMST(1)
19-
expect(res).toEqual(expectedGraph)
20-
})
4+
// create graph to compute MST on
5+
const graph = new GraphWeightedUndirectedAdjacencyList()
6+
graph.addEdge(1, 2, 1)
7+
graph.addEdge(2, 3, 2)
8+
graph.addEdge(3, 4, 1)
9+
graph.addEdge(3, 5, 100) // Removed in MST
10+
graph.addEdge(4, 5, 5)
11+
// create expected graph
12+
const expectedGraph = new GraphWeightedUndirectedAdjacencyList()
13+
expectedGraph.addEdge(1, 2, 1)
14+
expectedGraph.addEdge(2, 3, 2)
15+
expectedGraph.addEdge(3, 4, 1)
16+
expectedGraph.addEdge(4, 5, 5)
17+
// result from MST
18+
const res = graph.PrimMST(1)
19+
expect(res).toEqual(expectedGraph)
20+
})

0 commit comments

Comments
 (0)