zoukankan      html  css  js  c++  java
  • 用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;
    };
    

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

  • 相关阅读:
    集合(双列)
    集合(单列)
    Struts2.0第三章(文件上传、ajax开发、json、Fastjson、Jackson、注解开发)
    Struts2.0第二章(封装数据获取请求参数、servlet api的获取、ServletActionContext、注入、ognl、valueStack、ActionContext、interceptor拦截器)
    Struts2.0第一章(struts2.0概述,使用步骤,执行流程,各配置文件介绍,Action详解)
    servlet3.0实现文件上传功能
    annotation注解
    ClassLoader类加载器
    Proxy 动态代理(在过滤器中对request使用动态代理解决接受参数乱码问题)
    Filter过滤器(自动登陆、通用的字符集编码的过滤【处理不同请求的乱码问题】、。。。)
  • 原文地址:https://www.cnblogs.com/alwu007/p/5412890.html
Copyright © 2011-2022 走看看