npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@algorithm.ts/priority-queue

v2.0.14

Published

Priority queue.

Readme

A typescript implementation of Priority Queue, the internal data structure is a max heap.

Priority Queue is a special queue structure, the first element of the queue always returns to the maximum value in the queue, and the amortized time complexity of the enqueue and dequeue operations are both $O(\log N)$.

Install

  • npm

    npm install --save @algorithm.ts/priority-queue
  • yarn

    yarn add @algorithm.ts/priority-queue
  • deno

    import { createPriorityQueue } from 'https://raw.githubusercontent.com/guanghechen/algorithm.ts/main/packages/priority-queue/src/index.ts'

Usage

  • IPriorityQueue

    Member | Return | Description :----------------------------------------------------------------------------:|:-------------:|:--------------------------------------- init(elements?: T[], startPos?: number, endPos?: number) | void | Initialize priority queue with initial elements. enqueue(val: T) | void | Drop a element into the priority queue. enqueues(elements: T[], startIndex?: number, endIndex?: number) | void | Drop multiple elements into the priority queue. dequeue() | T|undefined | Popup the top element. splice(filter, newElements?: T[], startIndex?: number, endIndex?: number) | void | Remove existed elements which is not passed the filter, then enqueues the additional elements. replaceTop(newElement: T) | void | Replace top element with new one. (If the queue is empty, then the newElement will be enqueued.) top() | T|undefined | Get the top element. size() | number | Return the number of elements of the priority queue. isEmpty() | boolean | Check if the priority queue is empty. collect() | T[] | Return all of the elements of the priority queue.

  • createPriorityQueue

    export function createPriorityQueue<T>(
      cmp: (x: T, y: T) => -1 | 0 | 1 | number,
    ): IPriorityQueue<T>
    • createPriorityQueue<number>((x, y) => x - y): The top element has a maximum value.
    • createPriorityQueue<number>((x, y) => y - x): The top element has a minimum value.

Example

  • Basic

    import { createPriorityQueue } = '@algorithm.ts/priority-queue'
    
    const Q = createPriorityQueue<number>((x, y) => x - y)
    
    Q.enqueue(3)
    Q.enqueue(7)
    Q.enqueue(1)
    Q.enqueue(-5)
    
    Q.size()      // => 4
    Q.isEmpty()   // => false
    
    Q.dequeue()   // => 7
    Q.dequeue()   // => 3
    Q.top()       // => 1
    Q.top()       // => 1
    Q.dequeue()   // => 1
    Q.top()       // => -5
    Q.dequeue()   // => -5
    Q.top()       // => undefined
    Q.dequeue()   // => undefined
    
    Q.isEmpty()   // => true
  • A solution for 剑指offer#63 https://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1

    import { createPriorityQueue } from '@algorithm.ts/priority-queue'
    
    const lowerQ = createPriorityQueue<number>((x, y) => x - y)
    const upperQ = createPriorityQueue<number>((x, y) => y - x)
    
    export function Insert(num: number): void {
      if (lowerQ.size() === upperQ.size()) {
        upperQ.enqueue(num)
        lowerQ.enqueue(upperQ.dequeue()!)
      } else {
        lowerQ.enqueue(num)
        upperQ.enqueue(lowerQ.dequeue()!)
      }
    }
    
    export function GetMedian(): number {
      return lowerQ.size() === upperQ.size()
        ? (lowerQ.top()! + upperQ.top()!) / 2
        : lowerQ.top()!
    }

Related