1
1
/**
2
2
* 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
5
5
* 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,
7
7
* hence the root of the tree always has the smallest priority of all nodes.
8
- *
8
+ *
9
9
* This implementation of the Minimum Binary Heap allows for nodes to contain both a key and a priority.
10
10
* 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
13
13
* from root-to-leaf, left-to-right.
14
14
* Therefore, the parent-child node relationship is such that
15
15
* * the children nodes positions relative to their parent are: (parentPos * 2 + 1) and (parentPos * 2 + 2)
18
18
* More information and visuals on Binary Heaps can be found here: https://www.geeksforgeeks.org/binary-heap/
19
19
*/
20
20
21
-
22
21
// Priority Queue Helper functions
23
- function getParentPosition ( position ) {
22
+ function getParentPosition ( position ) {
24
23
// Get the parent node of the current node
25
24
return Math . floor ( ( position - 1 ) / 2 )
26
25
}
27
- function getChildrenPosition ( position ) {
26
+ function getChildrenPosition ( position ) {
28
27
// Get the children nodes of the current node
29
28
return [ 2 * position + 1 , 2 * position + 2 ]
30
29
}
31
30
32
31
class KeyPriorityQueue {
33
32
// Priority Queue class using Minimum Binary Heap
34
- constructor ( ) {
33
+ constructor ( ) {
35
34
this . _heap = [ ]
36
35
this . keys = { }
37
36
}
38
37
39
38
/**
40
39
* Checks if the heap is empty
41
- * @returns boolean
40
+ * @returns boolean
42
41
*/
43
- isEmpty ( ) {
42
+ isEmpty ( ) {
44
43
return this . _heap . length === 0
45
44
}
46
45
47
46
/**
48
47
* Adds an element to the queue
49
- * @param {number } key
50
- * @param {number } priority
48
+ * @param {number } key
49
+ * @param {number } priority
51
50
*/
52
- push ( key , priority ) {
51
+ push ( key , priority ) {
53
52
this . _heap . push ( [ key , priority ] )
54
53
this . keys [ key ] = this . _heap . length - 1
55
54
this . _shiftUp ( this . keys [ key ] )
@@ -59,7 +58,7 @@ class KeyPriorityQueue {
59
58
* Removes the element with least priority
60
59
* @returns the key of the element with least priority
61
60
*/
62
- pop ( ) {
61
+ pop ( ) {
63
62
this . _swap ( 0 , this . _heap . length - 1 )
64
63
const [ key ] = this . _heap . pop ( )
65
64
delete this . keys [ key ]
@@ -69,10 +68,10 @@ class KeyPriorityQueue {
69
68
70
69
/**
71
70
* Checks whether a given key is present in the queue
72
- * @param {number } key
71
+ * @param {number } key
73
72
* @returns boolean
74
73
*/
75
- contains ( key ) {
74
+ contains ( key ) {
76
75
return ( key in this . keys )
77
76
}
78
77
@@ -81,7 +80,7 @@ class KeyPriorityQueue {
81
80
* @param {number } key the element to change
82
81
* @param {number } priority new priority of the element
83
82
*/
84
- update ( key , priority ) {
83
+ update ( key , priority ) {
85
84
const currPos = this . keys [ key ]
86
85
this . _heap [ currPos ] [ 1 ] = priority
87
86
const parentPos = getParentPosition ( currPos )
@@ -100,15 +99,11 @@ class KeyPriorityQueue {
100
99
101
100
_getPriorityOrInfinite ( position ) {
102
101
// 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
109
104
}
110
105
111
- _shiftUp ( position ) {
106
+ _shiftUp ( position ) {
112
107
// Helper function to shift up a node to proper position (equivalent to bubbleUp)
113
108
let currPos = position
114
109
let parentPos = getParentPosition ( currPos )
@@ -121,20 +116,19 @@ class KeyPriorityQueue {
121
116
parentPos = getParentPosition ( currPos )
122
117
currPriority = this . _getPriorityOrInfinite ( currPos )
123
118
parentPriority = this . _getPriorityOrInfinite ( parentPos )
124
-
125
119
}
126
120
this . keys [ this . _heap [ currPos ] [ 0 ] ] = currPos
127
121
}
128
122
129
- _shiftDown ( position ) {
123
+ _shiftDown ( position ) {
130
124
// Helper function to shift down a node to proper position (equivalent to bubbleDown)
131
125
let currPos = position
132
126
let [ child1Pos , child2Pos ] = getChildrenPosition ( currPos )
133
127
let child1Priority = this . _getPriorityOrInfinite ( child1Pos )
134
128
let child2Priority = this . _getPriorityOrInfinite ( child2Pos )
135
129
let currPriority = this . _getPriorityOrInfinite ( currPos )
136
130
137
- if ( currPriority == Infinity ) {
131
+ if ( currPriority === Infinity ) {
138
132
return
139
133
}
140
134
@@ -153,12 +147,12 @@ class KeyPriorityQueue {
153
147
}
154
148
}
155
149
156
- _swap ( position1 , position2 ) {
150
+ _swap ( position1 , position2 ) {
157
151
// Helper function to swap 2 nodes
158
152
[ this . _heap [ position1 ] , this . _heap [ position2 ] ] = [ this . _heap [ position2 ] , this . _heap [ position1 ] ]
159
153
this . keys [ this . _heap [ position1 ] [ 0 ] ] = position1
160
154
this . keys [ this . _heap [ position2 ] [ 0 ] ] = position2
161
155
}
162
156
}
163
157
164
- export { KeyPriorityQueue }
158
+ export { KeyPriorityQueue }
0 commit comments