优先级队列(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;
};

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

最新文章

  1. Notes:SVG(2)---各种常见图形
  2. WPF 线程 Dispatcher
  3. SpringToolSuite/Eclipse中集成的Tomcat无法add Project时的解决版本
  4. WinForm程序中的类TextBox的自定义控件, 添加失去焦点的功能
  5. C#创建桌面快捷方式 和 开机启动
  6. 解决Autofac MVC 自动注入在 Areas拆分到不同dll下的注入失败问题
  7. centos git版本服务器配置
  8. 在linux下实现UBOOT的TFTP下载功能
  9. DAS,NAS,SAN在数据库存储上的应用
  10. python Redis
  11. P3390 【模板】矩阵快速幂
  12. 谈谈HashMap与HashTable
  13. 高通平台手机开发之LCD
  14. 音视频 学习&amp;开发&amp;测试 资源
  15. 接口自动化测试持续集成--Soapui接口功能测试断言
  16. Python3爬虫系列:理论+实验+爬取妹子图实战
  17. ISG 2018 Web Writeup
  18. mysql 执行语句
  19. Redux学习(3) ----- 结合React使用
  20. cocos creator怎么隐藏组件(setVisible)

热门文章

  1. 关于entity framework
  2. The APR based Apache Tomcat Native library
  3. 带你了解世界最先进的手势识别技术 -- 微软,凌感,Leap...
  4. java中的单例模式与doublecheck
  5. QWidget与HWND的互相转换
  6. Android JNI使用方法
  7. Boostrap 模态框 水平垂直居中问题
  8. bzoj2893
  9. JS判断浏览器是否支持某一个CSS3属性
  10. 你需要知道的10位Java开发牛人