class Node { constructor(value, priority) { this.value = value; this.priority = priority } } class PriorityQueue{ constructor() { this.values = []; } enqueue(value, priority) { let node = new Node(value, priority); this.values.push(node); this.bubbleUp(); } bubbleUp(){ let index = this.values.length - 1; const element = this.values[index]; while(index > 0) { let parentIndex = floor((index-1)/2); let parent = this.values[parentIndex]; if(element.priority >= parent.priority) break; this.values[parentIndex] = element; this.values[index] = parent; index = parentIndex; } } dequeue() { const min = this.values[0]; const end = this.values.pop(); if(this.values.length > 0) { this.values[0] = end; this.bubbleDown(); } return min; } bubbleDown() { let index = 0; const length = this.values.length; const element = this.value[0]; while(true) { let leftChildIndex = 2 * index + 1; let rightChildIndex = 2 * index + 2; let leftChild; let rightChild; let swap = null; if(leftChildIndex < length) { leftChildValue = this.values[leftChildIndex]; if(leftChild.priority < element.priority) { swap = leftChildIndex } } if(rightChildIndex < length) { rightChild = this.values[rightChildIndex]; if( (swap === null && rightChild.priority < element.priority) || (swap !== null && rightChild.priority < leftChildValue.priority) ){ swap = rightChildIndex; } } if(swap === null) break; this.values[index] = this.values[swap]; this.values[swap] = element; index = swap; } } }