# JS实现优先队列的三种方式

## 基于数组的实现

### 入队O(1),出队O(n)方式

``````class Pq{
constructor(arr){
if (arr.length){
this.tree = arr;
return;
}
this.tree = [];
}

// 入队
enqueue(val){
this.tree.push(val);
}

// 出队
dequeue(){
let maxIndex = 0;
for (let i = 1; i < this.tree.length; i++){
if (this.tree[i] > this.tree[maxIndex]){
maxIndex = i;
}
}
this.tree.splice(maxIndex, 1);
}

// 取队首
getFirst(){
return this.tree[0];
}
}``````

### 入队O(n),出队O(1)方式

``````class Pq2{
constructor(arr){
if (arr.length){
this.tree = arr;
this.tree.sort((a, b) => {
return b - a;
});
return;
}
this.tree = [];
}

// 入队
enqueue(val){
let t = this.tree;
if (val > t[0]) {t.unshift(val); return;}
if (val <= t[t.length - 1]) { t.push(val); return;}
for (let i = 0; i <= t.length - 2; i++){
if (t[i] >= val && val > t[i + 1]){
t.splice(i + 1, 0, val); // 插入
return;
}
}
}

// 出队
dequeue(){
this.tree.shift();
}

// 取队首
getFirst(){
return this.tree[0];
}
}``````

## 基于二叉堆的实现

``````class PriorityQueue{
// 取父节点索引 ~~((index - 1) / 2)
constructor(arr){
if (arr.length){
this.tree = []
this._build_tree(arr);
return;
}
this.tree = [];
}

// 入队
enqueue(val){
this.tree.push(val);
// 上浮
this._up(this.tree.length - 1);
}

// 出队
dequeue(){
// 取树根元素
this.tree.shift();
// 最后一个元素放树根，进行下沉
let last = this.tree.pop();
this.tree.unshift(last);
// log(n)下沉
this._down(0);
}

// 取队首的值
getFirst(){
return this.tree[0];
}

_build_tree(arr){
let tree = this.tree;
tree.push(arr[0]);
for (let i = 1; i < arr.length; i++){
tree.unshift(arr[i]);
this._down(0);
}
}

// 对index号元素执行下沉操作. 也叫heapify
_down(index){
let tree = this.tree;
// 本身就是叶子节点，无需下沉
let last_no_leaf = ~~((tree.length - 2) / 2);
if (index > last_no_leaf) return;
while(index <= last_no_leaf){
let l = tree[index * 2 + 1];
let r = tree[index * 2 + 2] || tree[index * 2 + 1]; // 有可能没有右儿子
let max = l >= r ? l : r;
let maxIndex = l >= r ? index * 2 + 1: index * 2 + 2
if (tree[index] < max){
[tree[index], tree[maxIndex]] = [tree[maxIndex], tree[index]]
index = index * 2 + 1
}else{
return;
}
}
}

// 对index号元素执行上浮操作
_up(index){
let tree = this.tree;
while(index !== 0){
let p = ~~((index - 1) / 2);
if (tree[index] > tree[p]){
[tree[index], tree[p]] = [tree[p], tree[index]];
// let tmp = index;
// this._down(tmp);
index = p;
} else {
return;
}
}
}
}

let p = new PriorityQueue([4,2,6,8,1,5,7,3,9]);
p.enqueue(13)
p.dequeue();
p.enqueue(12)
console.log(p.tree)``````

## 结尾

JS经典数据结构与算法代码库：https://github.com/cunzaizhuy...

##### 飞叶_前端

Wasm和emscripten技术交流群：939206522

1.4k 声望
138 粉丝
0 条评论