优先级队列(PriorityQueue)是个很有用的数据结构,很多编程语言都有实现。NodeJs是一个比较新潮的服务器语言,貌似还没有提供相关类。这些天有用到优先级队列,因为时间很充足,闲来无事,就自己实现了一下。代码如下:

  1. /**
  2. * script: pqueue.js
  3. * description: 优先级队列类
  4. * authors: alwu007@sina.cn
  5. * date: 2016-04-19
  6. */
  7.  
  8. var util = require('util');
  9.  
  10. /**
  11. * 优先级队列类
  12. * @param cmp_func 优先级比较函数,必需,参考数组排序参数
  13. */
  14. var PQueue = exports.PQueue = function(cmp_func) {
  15. //记录数组
  16. this._records = [];
  17. //优先级比较方法
  18. this._cmp_func = cmp_func;
  19. };
  20.  
  21. //堆向上调整
  22. PQueue.prototype._heapUpAdjust = function(index) {
  23. var records = this._records;
  24. var record = records[index];
  25. var cmp_func = this._cmp_func;
  26. while (index > 0) {
  27. var parent_index = Math.floor((index - 1) / 2);
  28. var parent_record = records[parent_index];
  29. if (cmp_func(record, parent_record) < 0) {
  30. records[index] = parent_record;
  31. index = parent_index;
  32. } else {
  33. break;
  34. }
  35. }
  36. records[index] = record;
  37. };
  38.  
  39. //堆向下调整
  40. PQueue.prototype._heapDownAdjust = function(index) {
  41. var records = this._records;
  42. var record = records[index];
  43. var cmp_func = this._cmp_func;
  44. var length = records.length;
  45. var child_index = 2 * index + 1;
  46. while (child_index < length) {
  47. if (child_index + 1 < length && cmp_func(records[child_index], records[child_index + 1]) > 0) {
  48. child_index ++;
  49. }
  50. var child_record = records[child_index];
  51. if (cmp_func(record, child_record) > 0) {
  52. records[index] = child_record;
  53. index = child_index;
  54. child_index = 2 * index + 1;
  55. } else {
  56. break;
  57. }
  58. }
  59. records[index] = record;
  60. };
  61.  
  62. //销毁
  63. PQueue.prototype.destroy = function() {
  64. this._records = null;
  65. this._cmp_func = null;
  66. };
  67.  
  68. //将记录插入队列
  69. PQueue.prototype.enQueue = function(record) {
  70. var records = this._records;
  71. records.push(record);
  72. this._heapUpAdjust(records.length - 1);
  73. };
  74.  
  75. //删除并返回队头记录
  76. PQueue.prototype.deQueue = function() {
  77. var records = this._records;
  78. if (!records.length)
  79. return undefined;
  80. var record = records[0];
  81. if (records.length == 1) {
  82. records.length = 0;
  83. } else {
  84. records[0] = records.pop();
  85. this._heapDownAdjust(0);
  86. }
  87. return record;
  88. };
  89.  
  90. //获取队头记录
  91. PQueue.prototype.getHead = function() {
  92. return this._records[0];
  93. };
  94.  
  95. //获取队列长度
  96. PQueue.prototype.getLength = function() {
  97. return this._records.length;
  98. };
  99.  
  100. //判断队列是否为空
  101. PQueue.prototype.isEmpty = function() {
  102. return this._records.length == 0;
  103. };
  104.  
  105. //清空队列
  106. PQueue.prototype.clear = function() {
  107. this._records.length = 0;
  108. };

我觉得,相对于其他排序算法而言,用堆实现优先级队列,入队时间波动较小,比较平稳。

用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. SCU3502 The Almost Lucky Number

    Description A lucky number is a number whose decimal representation contains only the digits \(4\) a ...

  2. [原博客] BZOJ 2725 : [Violet 6]故乡的梦

    这个题在bzoj上好像是个权限题,想做的可以去Vani的博客下载测试数据.这里有题面. 简单叙述一下题意:给你一个n个点.m条边的带权无向图,S点和T点,询问Q次删一条给定的边的S-T最短路. 其中  ...

  3. Android 使用XmlSerializer生成xml文件

    在Android开发中,我们时常要用到xml文件. xml作为一种数据载体,在数据传输中发挥着重要的作用,而且它可读性比较强. 下面给出在Android开发中使用XmlSerializer类生成一个简 ...

  4. 读书笔记-----Java并发编程实战(二)对象的共享

    public class NoVisibility{ private static boolean ready; private static int number; private static c ...

  5. LM393,LM741可以用作电压跟随器吗?

    应该不能,比较器一般为OC门,输出要上拉VCC,在跟随状态下为深度负反馈,恐怕不能正常工作,会振荡的,不过你可以试下嘛.

  6. 【HDOJ】3234 Exclusive-OR

    并查集.对于对元素赋值操作,更改为I p n v.令val[n]=0(任何数与0异或仍为原值).考虑fa[x] = fx, fa[y] = fy.如果使得fa[fx] = fy, 那么val[fx] ...

  7. Android Loader详解三:重启与回调

    重启装载器 当你使用initLoader()时,如果指定ID的装载器已经存在,则它使用这个装载器.如果不存在呢,它将创建一个新的.但是有时你却是想丢弃旧的然后开始新的数据. 要想丢弃旧数据,你应使用r ...

  8. bzoj 3531 [Sdoi2014]旅行(树链剖分,线段树)

    3531: [Sdoi2014]旅行 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 876  Solved: 446[Submit][Status][ ...

  9. Bzoj 1687: [Usaco2005 Open]Navigating the City 城市交通 广搜,深搜

    1687: [Usaco2005 Open]Navigating the City 城市交通 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 122  So ...

  10. MySQL WorkBench中文教程

    在网上找到了一份MySQL WorkBench的教程,点此可以下载Work Bench教程(原文),为了便于学习和交流,请朋友帮忙翻译成了中文,点此可以下载Work Bench教程(中文翻译版). 具 ...