Comments (2)
// 封装优先级队列
function PriorityQueue() {
// 在PriorityQueue中重新创建一个类,和java中的内部类很相似
function QueueElement(element, priority) {
this.element = element;
this.priority = priority;
}
// 封装属性,用数组来存储队列
this.items = [];
// 入队
PriorityQueue.prototype.enQueue = function (element, priority) {
// 1.创建对象
var queueElement = new QueueElement(element, priority);
// 2.判断队列是否为空
if(this.items.length == 0)
this.items.push(queueElement);
else {
var flag = false;
for(var i = 0; i< this.items.length; i++){
if(queueElement.priority < this.items[i].priority){
this.items.splice(i,0,queueElement);
flag = true;
break;
}
}
if(!flag)
this.items.push(queueElement);
}
}
// 2.出队
PriorityQueue.prototype.deQueue = function () {
return this.items.shift();
}
// 3.查看队头元素
PriorityQueue.prototype.front = function() {
return this.items[0];
}
// 4.判断队列是否为空
PriorityQueue.prototype.isEmpty = function() {
return this.items.length == 0;
}
// 5.查看队列中元素的个数
PriorityQueue.prototype.size = function() {
return this.items.length;
}
// 6.将队列元素按字符串格式输出
PriorityQueue.prototype.toString = function() {
var result = "";
for(var i = 0; i < this.items.length; i++)
result += this.items[i].element + " ";
return result;
}
}
from daily-question.
基于最大堆实现优先队列
class MaxHeap {
constructor(arr = []) {
this.heap = [] // 用数组表示堆结构
arr.forEach(item => this.add(item))
}
add(value) { // O(logK) 插入节点值: 放入数组末尾并上浮到合适位置
this.heap.push(value)
this.shiftUp(this.heap.length - 1)
}
pop() { // O(logK) 提取最大值/堆顶: 提取 heap[0] 并用 heap[-1] 进行代替,然后从顶部开始下沉到合适位置
const max = this.heap[0]
this.swap(0, this.size() - 1)
this.heap.pop()
this.shiftDown(0)
return max
}
peek() { // 获取最值/堆顶
return this.heap[0]
}
size() { // 获取当前堆大小
return this.heap.length
}
// ↓私有属性↓
swap(index1, index2) { // 交换节点位置
const temp = this.heap[index1]
this.heap[index1] = this.heap[index2]
this.heap[index2] = temp
}
parentIndex(index) { // 获取父节点的位置 (index - 1) / 2 向下取整
return (index - 1) >> 1
}
leftChildIndex(index) { // 获取左子节点
return index * 2 + 1
}
rightChildIndex(index) { // 获取右子节点
return index * 2 + 2
}
shiftUp(index) { // 上浮节点,当前值小于父节点值时停止,使当前堆保持最大堆的性质
let parentIndex = this.parentIndex(index)
while (index > 0 && this.heap[parentIndex] < this.heap[index]) {
this.swap(index, parentIndex)
parentIndex = this.parentIndex(index = parentIndex)
}
}
shiftDown(index) { // 下沉节点,当前值大于子节点值时停止,使当前堆保持最大堆的性质
const leftIndex = this.leftChildIndex(index)
const rightIndex = this.rightChildIndex(index)
// 先比较左子节点值,当前值小于左子节点,则交换,并递归进行下沉
if (this.heap[index] < this.heap[leftIndex]) {
this.swap(leftIndex, index)
this.shiftDown(leftIndex)
}
if (this.heap[index] < this.heap[rightIndex]) {
this.swap(rightIndex, index)
this.shiftDown(rightIndex)
}
}
}
// ==TEST==
const priorityQueue = new MaxHeap([2, 5, 3])
console.log(priorityQueue.peek()) // 5
priorityQueue.add(7)
console.log(priorityQueue.peek()) // 7
priorityQueue.pop()
priorityQueue.add(1)
console.log(priorityQueue.peek()) // 5
from daily-question.
Related Issues (20)
- 【Q736】前端如何对分支环境进行部署 HOT 2
- 【Q737】如何取得一个数字的小数部分与整数部分 HOT 2
- 【Q738】websocket 和短轮询有什么区别 HOT 3
- 【Q739】webpack 中是如何处理 new URL 资源的
- 【Q740】vite 中是如何处理 new URL 资源的
- 【Q741】我们上传图片为 Blob/File 对象时,是如何向服务器端传送数据的 HOT 1
- [bug] B站的链接贴错了 HOT 2
- 引用仓库错了 HOT 2
- `<script type="module">` HOT 1
- 多阶段构建并不需要 docker-compose HOT 1
- 【Q742】大文件上传,如何获取到读取进度? HOT 1
- 代码
- [feature request]面试题添加难度排序以及一键生成一份面试题的工具 HOT 2
- 【Q747】如何实现一个 omit/omitBy 函数 HOT 6
- 【Q748】在 babel 编译为低版本 ES 时,为何能够编译可选链之类语法,但无法编译 API HOT 1
- 【Q743】实现 batchFn 函数,可以批量执行函数 HOT 4
- 【开源自荐】推荐一个每日更新的前端面试题库 HOT 1
- 【Q474】在 react 中,以下父子组件的 useEffect/useLayoutEffect 顺序如何 HOT 1
- 【Q745】webpack 的打包流程是什么样的 HOT 1
- 【Q744】数据库中更新一条记录时,如何自动更新其 updated_at 字段
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from daily-question.