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

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

  • 相关阅读:
    java Activiti 工作流引擎 SSM 框架模块设计方案
    自定义表单 Flowable 工作流 Springboot vue.js 前后分离 跨域 有代码生成器
    数据库设计的十个最佳实践
    activiti 汉化 stencilset.json 文件内容
    JAVA oa 办公系统模块 设计方案
    java 考试系统 在线学习 视频直播 人脸识别 springboot框架 前后分离 PC和手机端
    集成 nacos注册中心配置使用
    “感恩节 ”怼记
    仓颉编程语言的一点期望
    关于System.out.println()与System.out.print("\n")的区别
  • 原文地址:https://www.cnblogs.com/alwu007/p/5412890.html
Copyright © 2011-2022 走看看