SOURCE

function PriorityQueue(type) {
    // 方便计算,将第一位置空
    this.list = [{}]
    this.type = type || 'min'
}

PriorityQueue.prototype.size = function () {
    return this.list.length - 1
}

PriorityQueue.prototype.empty = function () {
    return this.list.length === 1
}

PriorityQueue.prototype.push = function (data) {
    this.list.push(data)
    this._update()
}

// 调整数
PriorityQueue.prototype._update = function () {
    let newPos = this.list.length - 1
    let parent = Math.floor(newPos / 2)
    let isChange = true
    while (isChange) {
        isChange = false
        console.log('parent:' + parent)
        if (
            (this.type === 'min' && this.list[parent] > this.list[newPos]) 
            || 
            (this.type === 'max' && this.list[parent] < this.list[newPos])
        ) {
            let temp = this.list[parent]
            this.list[parent] = this.list[newPos]
            this.list[newPos] = temp
            isChange = true
            newPost = parent
            parent = Math.floor(newPos / 2)
        }
    }
  console.log(this.list)
}

let queue = new PriorityQueue()
queue.push(2)
queue.push(7)
queue.push(3)
queue.push(15)
queue.push(1)
console 命令行工具 X clear

                    
>
console