优先级队列(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的更多相关文章

  1. 《Java数据结构与算法》笔记-CH4-6优先级队列

    /** * 优先级队列 * 效率:插入O(n),删除O(1).第12章介绍如何通过堆来改进insert时间 */ class PriorityQueue { private int maxSize; ...

  2. 一个C优先级队列实现

    刚下班没事干,实现了一个简单的优先级队列 #include <stdlib.h>#include <stdio.h> typedef void (*pqueue_setinde ...

  3. java实现 数据结构:链表、 栈、 队列、优先级队列、哈希表

    java实现 数据结构:链表. 栈. 队列.优先级队列.哈希表   数据结构javavector工作importlist 最近在准备找工作的事情,就复习了一下java.翻了一下书和网上的教材,发现虽然 ...

  4. 体验Rabbitmq强大的【优先级队列】之轻松面对现实业务场景

    说到队列的话,大家一定不会陌生,但是扯到优先级队列的话,还是有一部分同学是不清楚的,可能是不知道怎么去实现吧,其实呢,,,这东西已 经烂大街了...很简单,用“堆”去实现的,在我们系统中有一个订单催付 ...

  5. Java中的队列Queue,优先级队列PriorityQueue

    队列Queue 在java5中新增加了java.util.Queue接口,用以支持队列的常见操作.该接口扩展了java.util.Collection接口. Queue使用时要尽量避免Collecti ...

  6. 如何基于RabbitMQ实现优先级队列

    概述 由于种种原因,RabbitMQ到目前为止,官方还没有实现优先级队列,只实现了Consumer的优先级处理. 但是,迫于种种原因,应用层面上又需要优先级队列,因此需求来了:如何为RabbitMQ加 ...

  7. ACM/ICPC 之 优先级队列+设置IO缓存区(TSH OJ-Schedule(任务调度))

    一个裸的优先级队列(最大堆)题,但也有其他普通队列的做法.这道题我做了两天,结果发现是输入输出太过频繁,一直只能A掉55%的数据,其他都是TLE,如果将输入输出的数据放入缓存区,然后满区输出,可以将I ...

  8. java中PriorityQueue优先级队列使用方法

    优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最高优先权的元素. PriorityQueue是从JDK1.5开始提供的新的数据结构接口. 如果不提供Comparator的话,优先 ...

  9. stl的优先级队列

    #include <iostream> #include <vector> #include <queue> using namespace std; class ...

随机推荐

  1. SQL 维护用得到的监控语句

    使用DMV来分析SQL Server启动以来累计使用CPU资源最多的语句.例如下面的语句就可以列出前50名 s2.dbid, ( , ( ( ) )) AS sql_statement, execut ...

  2. 断命windows上卸载node并重装

    抠门儿世界500强给前端开发人员用windows windows不支持n模块没法自动升级 不记得何时安装的旧版本node连个uninstaller都找不到 绕道安装nvm path也自动加进去了丫命令 ...

  3. windows下DOS命令中查看被占用端口的进程

    今天在用tomcat 运行项目时报错:   java.net.BindException: Address already in use: JVM_Bind这个错误   刚开始有点怀疑是javaw.e ...

  4. [译]GotW #1: Variable Initialization 续

    Answer 2. 下面每行代码都做了什么? 在Q2中,我们创建了一个vector<int>且传了参数10和20到构造函数中,第一种情况下(10,20),第二种情况是{10, 20}. 它 ...

  5. 一种Android换肤机制的实现

    http://eastmoneyandroid.github.io/2016/01/22/android-reskin/

  6. [LeetCode#218] The Skyline Problem

    Problem: A city's skyline is the outer contour of the silhouette formed by all the buildings in that ...

  7. WordPress Bradesco Gateway插件‘falha.php’跨站脚本漏洞

    漏洞名称: WordPress Bradesco Gateway插件‘falha.php’跨站脚本漏洞 CNNVD编号: CNNVD-201309-451 发布时间: 2013-09-26 更新时间: ...

  8. Android Loader详解四:回调及完整例子

    onLoadFinished 这个方法是在前面已创建的装载器已经完成其加载过程后被调用.这个方法保证会在应用到装载器上的数据被释放之前被调用.在此方法中,你必须删除所有对旧数据的使用(因为它将很快会被 ...

  9. HDU-3790 最短路径问题

    最短路径问题 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  10. QT5.1在Windows下 出现QApplication: No such file or directory 问题的解决办法

    QT5.0.1在Windows下 出现QApplication: No such file or directory 问题的解决办法 分类: 编程语言学习 软件使用 QT编程学习2013-03-07 ...