6
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
- * This implementation of the Minimum Binary Heap allows for nodes to contain both a key and a priority.
10
- * WARNING: keys must be Integers as they are used as array indices .
9
+ * This implementation of the Minimum Binary Heap allows for nodes to be associated to both a key,
10
+ * which can be any datatype, and a priority .
11
11
*
12
- * In this implementation, the heap is represented by an array with nodes ordered
12
+ * 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)
@@ -26,7 +26,7 @@ class KeyPriorityQueue {
26
26
// Priority Queue class using Minimum Binary Heap
27
27
constructor ( ) {
28
28
this . _heap = [ ]
29
- this . keys = { }
29
+ this . priorities = new Map ( )
30
30
}
31
31
32
32
/**
@@ -39,13 +39,13 @@ class KeyPriorityQueue {
39
39
40
40
/**
41
41
* Adds an element to the queue
42
- * @param {number } key
42
+ * @param {* } key
43
43
* @param {number } priority
44
44
*/
45
45
push ( key , priority ) {
46
- this . _heap . push ( [ key , priority ] )
47
- this . keys [ key ] = this . _heap . length - 1
48
- this . _shiftUp ( this . keys [ key ] )
46
+ this . _heap . push ( key )
47
+ this . priorities . set ( key , priority )
48
+ this . _shiftUp ( this . _heap . length - 1 )
49
49
}
50
50
51
51
/**
@@ -54,29 +54,33 @@ class KeyPriorityQueue {
54
54
*/
55
55
pop ( ) {
56
56
this . _swap ( 0 , this . _heap . length - 1 )
57
- const [ key ] = this . _heap . pop ( )
58
- delete this . keys [ key ]
57
+ const key = this . _heap . pop ( )
58
+ this . priorities . delete ( key )
59
59
this . _shiftDown ( 0 )
60
60
return key
61
61
}
62
62
63
63
/**
64
64
* Checks whether a given key is present in the queue
65
- * @param {number } key
65
+ * @param {* } key
66
66
* @returns boolean
67
67
*/
68
68
contains ( key ) {
69
- return ( key in this . keys )
69
+ return this . priorities . has ( key )
70
70
}
71
71
72
72
/**
73
- * Updates the priority of the given element
74
- * @param {number } key the element to change
73
+ * Updates the priority of the given element.
74
+ * Adds the element if it is not in the queue.
75
+ * @param {* } key the element to change
75
76
* @param {number } priority new priority of the element
76
77
*/
77
78
update ( key , priority ) {
78
- const currPos = this . keys [ key ]
79
- this . _heap [ currPos ] [ 1 ] = priority
79
+ const currPos = this . _heap . indexOf ( key )
80
+ // if the key does not exist yet, add it
81
+ if ( currPos === - 1 ) return this . push ( key , priority )
82
+ // else update priority
83
+ this . priorities . set ( key , priority )
80
84
const parentPos = getParentPosition ( currPos )
81
85
const currPriority = this . _getPriorityOrInfinite ( currPos )
82
86
const parentPriority = this . _getPriorityOrInfinite ( parentPos )
@@ -93,7 +97,7 @@ class KeyPriorityQueue {
93
97
94
98
_getPriorityOrInfinite ( position ) {
95
99
// Helper function, returns priority of the node, or Infinite if no node corresponds to this position
96
- if ( position >= 0 && position < this . _heap . length ) return this . _heap [ position ] [ 1 ]
100
+ if ( position >= 0 && position < this . _heap . length ) return this . priorities . get ( this . _heap [ position ] )
97
101
else return Infinity
98
102
}
99
103
@@ -111,7 +115,6 @@ class KeyPriorityQueue {
111
115
currPriority = this . _getPriorityOrInfinite ( currPos )
112
116
parentPriority = this . _getPriorityOrInfinite ( parentPos )
113
117
}
114
- this . keys [ this . _heap [ currPos ] [ 0 ] ] = currPos
115
118
}
116
119
117
120
_shiftDown ( position ) {
@@ -144,8 +147,6 @@ class KeyPriorityQueue {
144
147
_swap ( position1 , position2 ) {
145
148
// Helper function to swap 2 nodes
146
149
[ this . _heap [ position1 ] , this . _heap [ position2 ] ] = [ this . _heap [ position2 ] , this . _heap [ position1 ] ]
147
- this . keys [ this . _heap [ position1 ] [ 0 ] ] = position1
148
- this . keys [ this . _heap [ position2 ] [ 0 ] ] = position2
149
150
}
150
151
}
151
152
0 commit comments