用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 ...
随机推荐
- Kaggle Competition Past Solutions
Kaggle Competition Past Solutions We learn more from code, and from great code. Not necessarily alwa ...
- 关于键盘冲突那点事(3键冲突/7键冲突/PS2/USB的各种原理)
转自关于键盘冲突那点事(3键冲突/7键冲突/PS2/USB的各种原理) 最近闲得无聊,正好看到有人发帖提问,于是就来详细说说所谓键位冲突和无冲突的各种原理--基本上这也是个老生常谈的话题了,但相关的技 ...
- IntelliJ IDEA Subversion的使用方式
接着一直点下一步基本上就可以了
- Android ListView中 每一项都有不同的布局
实现代码 Adapter的代码 其中:ViewHolder分别是三个不同的布局,也就是ListView中每一项的布局 TYPE_1...是三种类型. 在使用不同布局的时候,getItemViewTyp ...
- win7下登陆中国银行网上银行IE浏览器版本过高问题解决
2013-11-23 2013-11-23登录中国银行出现以下提示. 操作系统为windows7 64位旗舰版,搜狗浏览器版本为4.1.1.7598.想不到突然出现如下提示.打电话去问,告知使用IE7 ...
- [C# 网络编程系列]专题三:自定义Web服务器
转自:http://www.cnblogs.com/zhili/archive/2012/08/23/2652460.html 前言: 经过前面的专题中对网络层协议和HTTP协议的简单介绍相信大家对网 ...
- HDOJ --- 2196 Computer
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- linux下出现+ ls --color=auto -l --color=auto...++ echo -ne '\033]0;root@imon-2:~'等
[root@imon-2 ~]# cd /root/ + cd /root/ ++ echo -ne '\033]0;root@imon-2:~' [root@imon-2 ~]# ll + ls - ...
- selenium webdriver 环境搭建--java
selenium java环境的安装可以分为三个部分:jdk.eclipse和selenium. jdk jdk(java development kit)是sun公司针对java开发人员的产品,是整 ...
- 对使命召唤OL游戏中队友能相互救治的动作设定的感慨
很偶然的在网吧看到有人在玩一个枪战游戏,场景特别真实特别吸引人,后来留意到是使命召唤OL.我使用QQ帐号(是腾讯代理)玩了一次,觉得游戏做的确实精致,子弹打击效果和人物被子弹击中的效果特别真实,大家可 ...