Skip to content

Commit 8543f05

Browse files
fix: use map for keys and priorities TheAlgorithms#1298
1 parent 33a1f14 commit 8543f05

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

Data-Structures/Heap/KeyPriorityQueue.js

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
* 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.
88
*
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.
1111
*
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
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)
@@ -26,7 +26,7 @@ class KeyPriorityQueue {
2626
// Priority Queue class using Minimum Binary Heap
2727
constructor () {
2828
this._heap = []
29-
this.keys = {}
29+
this.priorities = new Map()
3030
}
3131

3232
/**
@@ -39,13 +39,13 @@ class KeyPriorityQueue {
3939

4040
/**
4141
* Adds an element to the queue
42-
* @param {number} key
42+
* @param {*} key
4343
* @param {number} priority
4444
*/
4545
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)
4949
}
5050

5151
/**
@@ -54,29 +54,33 @@ class KeyPriorityQueue {
5454
*/
5555
pop () {
5656
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)
5959
this._shiftDown(0)
6060
return key
6161
}
6262

6363
/**
6464
* Checks whether a given key is present in the queue
65-
* @param {number} key
65+
* @param {*} key
6666
* @returns boolean
6767
*/
6868
contains (key) {
69-
return (key in this.keys)
69+
return this.priorities.has(key)
7070
}
7171

7272
/**
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
7576
* @param {number} priority new priority of the element
7677
*/
7778
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)
8084
const parentPos = getParentPosition(currPos)
8185
const currPriority = this._getPriorityOrInfinite(currPos)
8286
const parentPriority = this._getPriorityOrInfinite(parentPos)
@@ -93,7 +97,7 @@ class KeyPriorityQueue {
9397

9498
_getPriorityOrInfinite (position) {
9599
// 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])
97101
else return Infinity
98102
}
99103

@@ -111,7 +115,6 @@ class KeyPriorityQueue {
111115
currPriority = this._getPriorityOrInfinite(currPos)
112116
parentPriority = this._getPriorityOrInfinite(parentPos)
113117
}
114-
this.keys[this._heap[currPos][0]] = currPos
115118
}
116119

117120
_shiftDown (position) {
@@ -144,8 +147,6 @@ class KeyPriorityQueue {
144147
_swap (position1, position2) {
145148
// Helper function to swap 2 nodes
146149
[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
149150
}
150151
}
151152

0 commit comments

Comments
 (0)