1. class PriorityQueue {
    2. constructor(
    3. compare = (a, b) => a[0] < b[0]
    4. ){
    5. this.data = []
    6. this.size = 0
    7. this.compare = compare
    8. }
    9. peek() {
    10. return this.size === 0 ? null : this.data[0]
    11. }
    12. offer(val) {
    13. this.data.push(val)
    14. this._shifUp(this.size++)
    15. }
    16. poll() {
    17. if(this.size === 0) { return null }
    18. this._swap(0, --this.size)
    19. this._shifDown(0)
    20. return this.data.pop()
    21. }
    22. _parent(index) {
    23. return index - 1 >> 1
    24. }
    25. _child(index) {
    26. return (index << 1) + 1
    27. }
    28. _shifDown(index) {
    29. while(this._child(index) < this.size) {
    30. let child = this._child(index)
    31. if(child + 1 < this.size
    32. && this.compare(this.data[child + 1], this.data[child])) {
    33. child = child + 1
    34. }
    35. if(this.compare(this.data[index], this.data[child])){
    36. break
    37. }
    38. this._swap(index, child)
    39. index = child
    40. }
    41. }
    42. _shifUp(index) {
    43. while(this._parent(index) >= 0
    44. && this.compare(this.data[index], this.data[this._parent(index)])) {
    45. this._swap(index, this._parent(index))
    46. index = this._parent(index)
    47. }
    48. }
    49. _swap(a, b) {
    50. [this.data[a], this.data[b]] = [this.data[b], this.data[a]]
    51. }
    52. }