用NodeJs实现优先级队列PQueue
优先级队列(PriorityQueue)是个很有用的数据结构,很多编程语言都有实现。NodeJs是一个比较新潮的服务器语言,貌似还没有提供相关类。这些天有用到优先级队列,因为时间很充足,闲来无事,就自己实现了一下。代码如下:
- /**
- * script: pqueue.js
- * description: 优先级队列类
- * authors: alwu007@sina.cn
- * date: 2016-04-19
- */
- var util = require('util');
- /**
- * 优先级队列类
- * @param cmp_func 优先级比较函数,必需,参考数组排序参数
- */
- var PQueue = exports.PQueue = function(cmp_func) {
- //记录数组
- this._records = [];
- //优先级比较方法
- this._cmp_func = cmp_func;
- };
- //堆向上调整
- PQueue.prototype._heapUpAdjust = function(index) {
- var records = this._records;
- var record = records[index];
- var cmp_func = this._cmp_func;
- while (index > 0) {
- var parent_index = Math.floor((index - 1) / 2);
- var parent_record = records[parent_index];
- if (cmp_func(record, parent_record) < 0) {
- records[index] = parent_record;
- index = parent_index;
- } else {
- break;
- }
- }
- records[index] = record;
- };
- //堆向下调整
- PQueue.prototype._heapDownAdjust = function(index) {
- var records = this._records;
- var record = records[index];
- var cmp_func = this._cmp_func;
- var length = records.length;
- var child_index = 2 * index + 1;
- while (child_index < length) {
- if (child_index + 1 < length && cmp_func(records[child_index], records[child_index + 1]) > 0) {
- child_index ++;
- }
- var child_record = records[child_index];
- if (cmp_func(record, child_record) > 0) {
- records[index] = child_record;
- index = child_index;
- child_index = 2 * index + 1;
- } else {
- break;
- }
- }
- records[index] = record;
- };
- //销毁
- PQueue.prototype.destroy = function() {
- this._records = null;
- this._cmp_func = null;
- };
- //将记录插入队列
- PQueue.prototype.enQueue = function(record) {
- var records = this._records;
- records.push(record);
- this._heapUpAdjust(records.length - 1);
- };
- //删除并返回队头记录
- PQueue.prototype.deQueue = function() {
- var records = this._records;
- if (!records.length)
- return undefined;
- var record = records[0];
- if (records.length == 1) {
- records.length = 0;
- } else {
- records[0] = records.pop();
- this._heapDownAdjust(0);
- }
- return record;
- };
- //获取队头记录
- PQueue.prototype.getHead = function() {
- return this._records[0];
- };
- //获取队列长度
- PQueue.prototype.getLength = function() {
- return this._records.length;
- };
- //判断队列是否为空
- PQueue.prototype.isEmpty = function() {
- return this._records.length == 0;
- };
- //清空队列
- PQueue.prototype.clear = function() {
- this._records.length = 0;
- };
我觉得,相对于其他排序算法而言,用堆实现优先级队列,入队时间波动较小,比较平稳。
用NodeJs实现优先级队列PQueue的更多相关文章
- 《Java数据结构与算法》笔记-CH4-6优先级队列
/** * 优先级队列 * 效率:插入O(n),删除O(1).第12章介绍如何通过堆来改进insert时间 */ class PriorityQueue { private int maxSize; ...
- 一个C优先级队列实现
刚下班没事干,实现了一个简单的优先级队列 #include <stdlib.h>#include <stdio.h> typedef void (*pqueue_setinde ...
- java实现 数据结构:链表、 栈、 队列、优先级队列、哈希表
java实现 数据结构:链表. 栈. 队列.优先级队列.哈希表 数据结构javavector工作importlist 最近在准备找工作的事情,就复习了一下java.翻了一下书和网上的教材,发现虽然 ...
- 体验Rabbitmq强大的【优先级队列】之轻松面对现实业务场景
说到队列的话,大家一定不会陌生,但是扯到优先级队列的话,还是有一部分同学是不清楚的,可能是不知道怎么去实现吧,其实呢,,,这东西已 经烂大街了...很简单,用“堆”去实现的,在我们系统中有一个订单催付 ...
- Java中的队列Queue,优先级队列PriorityQueue
队列Queue 在java5中新增加了java.util.Queue接口,用以支持队列的常见操作.该接口扩展了java.util.Collection接口. Queue使用时要尽量避免Collecti ...
- 如何基于RabbitMQ实现优先级队列
概述 由于种种原因,RabbitMQ到目前为止,官方还没有实现优先级队列,只实现了Consumer的优先级处理. 但是,迫于种种原因,应用层面上又需要优先级队列,因此需求来了:如何为RabbitMQ加 ...
- ACM/ICPC 之 优先级队列+设置IO缓存区(TSH OJ-Schedule(任务调度))
一个裸的优先级队列(最大堆)题,但也有其他普通队列的做法.这道题我做了两天,结果发现是输入输出太过频繁,一直只能A掉55%的数据,其他都是TLE,如果将输入输出的数据放入缓存区,然后满区输出,可以将I ...
- java中PriorityQueue优先级队列使用方法
优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最高优先权的元素. PriorityQueue是从JDK1.5开始提供的新的数据结构接口. 如果不提供Comparator的话,优先 ...
- stl的优先级队列
#include <iostream> #include <vector> #include <queue> using namespace std; class ...
随机推荐
- SCU3502 The Almost Lucky Number
Description A lucky number is a number whose decimal representation contains only the digits \(4\) a ...
- [原博客] BZOJ 2725 : [Violet 6]故乡的梦
这个题在bzoj上好像是个权限题,想做的可以去Vani的博客下载测试数据.这里有题面. 简单叙述一下题意:给你一个n个点.m条边的带权无向图,S点和T点,询问Q次删一条给定的边的S-T最短路. 其中 ...
- Android 使用XmlSerializer生成xml文件
在Android开发中,我们时常要用到xml文件. xml作为一种数据载体,在数据传输中发挥着重要的作用,而且它可读性比较强. 下面给出在Android开发中使用XmlSerializer类生成一个简 ...
- 读书笔记-----Java并发编程实战(二)对象的共享
public class NoVisibility{ private static boolean ready; private static int number; private static c ...
- LM393,LM741可以用作电压跟随器吗?
应该不能,比较器一般为OC门,输出要上拉VCC,在跟随状态下为深度负反馈,恐怕不能正常工作,会振荡的,不过你可以试下嘛.
- 【HDOJ】3234 Exclusive-OR
并查集.对于对元素赋值操作,更改为I p n v.令val[n]=0(任何数与0异或仍为原值).考虑fa[x] = fx, fa[y] = fy.如果使得fa[fx] = fy, 那么val[fx] ...
- Android Loader详解三:重启与回调
重启装载器 当你使用initLoader()时,如果指定ID的装载器已经存在,则它使用这个装载器.如果不存在呢,它将创建一个新的.但是有时你却是想丢弃旧的然后开始新的数据. 要想丢弃旧数据,你应使用r ...
- bzoj 3531 [Sdoi2014]旅行(树链剖分,线段树)
3531: [Sdoi2014]旅行 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 876 Solved: 446[Submit][Status][ ...
- Bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 广搜,深搜
1687: [Usaco2005 Open]Navigating the City 城市交通 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 122 So ...
- MySQL WorkBench中文教程
在网上找到了一份MySQL WorkBench的教程,点此可以下载Work Bench教程(原文),为了便于学习和交流,请朋友帮忙翻译成了中文,点此可以下载Work Bench教程(中文翻译版). 具 ...