• 用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;
    };
    

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

  • 相关阅读:
    Python代写使用矩阵分解法找到类似的音乐
    (转)语义分析透镜洞察婚庆微博用户行为数据
    (转)虎扑论坛基因探秘:社群用户行为数据洞察
    bzoj 4922: [Lydsy1706月赛]Karp-de-Chant Number 贪心+dp
    bzoj 1110: [POI2007]砝码Odw 贪心
    bzoj 3721: PA2014 Final Bazarek 贪心
    bzoj 2563: 阿狸和桃子的游戏 贪心
    bzoj 3999: [TJOI2015]旅游 LCT
    bzoj 4240: 有趣的家庭菜园 树状数组+贪心
    CF369E Valera and Queries kdtree
  • 原文地址:https://www.cnblogs.com/alwu007/p/5412890.html
Copyright © 2020-2023  润新知