JS实现优先队列的三种方式
前言
基于数组和基于二叉堆两种数据结构实现了优先队列。其中,基于数组的方式,分为两种。
一种是入队O(n),出队O(1);一种是入队O(1),出队O(n)。
基于数组的实现
入队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...
被 1 篇内容引用
推荐阅读
Qwik中文文档新鲜出炉啦,欢迎访问~
相信大家在各个技术群和公众号,已经多多少少听说了由Angular作者带头开发的Qwik框架。我觉得它的概念很好,国外也挺火的,其仓库star数每天增长100左右。
飞叶_前端阅读 610
手把手教你写一份优质的前端技术简历
不知不觉一年一度的秋招又来了,你收获了哪些大厂的面试邀约,又拿了多少offer呢?你身边是不是有挺多人技术比你差,但是却拿到了很多大厂的offer呢?其实,要想面试拿offer,首先要过得了简历那一关。如果一份简...
tonychen赞 152阅读 17.7k评论 5
正则表达式实例
收集在业务中经常使用的正则表达式实例,方便以后进行查找,减少工作量。常用正则表达式实例1. 校验基本日期格式 {代码...} {代码...} 2. 校验密码强度密码的强度必须是包含大小写字母和数字的组合,不能使用特殊...
寒青赞 56阅读 8.4k评论 11
JavaScript有用的代码片段和trick
平时工作过程中可以用到的实用代码集棉。判断对象否为空 {代码...} 浮点数取整 {代码...} 注意:前三种方法只适用于32个位整数,对于负数的处理上和Math.floor是不同的。 {代码...} 生成6位数字验证码 {代码...} ...
jenemy赞 48阅读 6.9k评论 12
从零搭建 Node.js 企业级 Web 服务器(十五):总结与展望
总结截止到本章 “从零搭建 Node.js 企业级 Web 服务器” 主题共计 16 章内容就更新完毕了,回顾第零章曾写道:搭建一个 Node.js 企业级 Web 服务器并非难事,只是必须做好几个关键事项这几件必须做好的关键事项就...
乌柏木赞 75阅读 7k评论 16
再也不学AJAX了!(二)使用AJAX ① XMLHttpRequest
「再也不学 AJAX 了」是一个以 AJAX 为主题的系列文章,希望读者通过阅读本系列文章,能够对 AJAX 技术有更加深入的认识和理解,从此能够再也不用专门学习 AJAX。本篇文章为该系列的第二篇,最近更新于 2023 年 1...
libinfs赞 42阅读 6.8k评论 12
从零搭建 Node.js 企业级 Web 服务器(一):接口与分层
分层规范从本章起,正式进入企业级 Web 服务器核心内容。通常,一块完整的业务逻辑是由视图层、控制层、服务层、模型层共同定义与实现的,如下图:从上至下,抽象层次逐渐加深。从下至上,业务细节逐渐清晰。视图...
乌柏木赞 45阅读 8.5k评论 6
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。