Giter VIP home page Giter VIP logo

priorityqueuejs's Introduction

priorityqueue.js

Build Status

A simple priority queue data structure for Node.js.

Installation

npm install priorityqueuejs

Example

var PriorityQueue = require('priorityqueuejs');

var queue = new PriorityQueue(function(a, b) {
  return a.cash - b.cash;
});

queue.enq({ cash: 250, name: 'Valentina' });
queue.enq({ cash: 300, name: 'Jano' });
queue.enq({ cash: 150, name: 'Fran' });
queue.size(); // 3
queue.peek(); // { cash: 300, name: 'Jano' }
queue.deq(); // { cash: 300, name: 'Jano' }
queue.size(); // 2

API

PriorityQueue()

Initializes a new empty PriorityQueue wich uses .DEFAULT_COMPARATOR() as the comparator function for its elements.

PriorityQueue(comparator)

Initializes a new empty PriorityQueue with uses the given comparator(a, b) function as the comparator for its elements.

The comparator function must return a positive number when a > b, 0 when a == b and a negative number when a < b.

PriorityQueue.DEFAULT_COMPARATOR(a, b)

Compares two Number or String objects.

PriorityQueue#deq()

Dequeues the top element of the priority queue. Throws an Error when the queue is empty.

PriorityQueue#enq(element)

Enqueues the element at the priority queue and returns its new size.

PriorityQueue#forEach(fn)

Executes fn on each element. Just be careful to not modify the priorities, since the queue won't reorder itself.

PriorityQueue#isEmpty()

Returns whether the priority queue is empty or not.

PriorityQueue#peek()

Peeks at the top element of the priority queue. Throws an Error when the queue is empty.

PriorityQueue#size()

Returns the size of the priority queue.

Testing

npm install
npm test

Licence

MIT

priorityqueuejs's People

Contributors

janogonzalez avatar kessler avatar sridharrajs avatar suryakanigolla avatar xgbuils avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.