Coder Social home page Coder Social logo

amj311 / priority_queue Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 95 KB

An abstract Priority Queue class that can be inherited from to work with an data type.

JavaScript 12.17% TypeScript 87.83%
datastructures algorithms minheap maxheap priority-queue

priority_queue's Introduction

Abstract Priority Queue

This simple library provides an abstract Priority Queue class that can be inherited from to work with an data type.

Inheriting

When inheriting from this class you only need to define a method called 'isHigherPriority' which provides the logic for comparing two items from the queue.

For example:

class Dog {
  constructor(
    public name:string,
    public weight:number
  ) {}
}

class DogPile extends PriorityQueue<Dog> {
  protected isHigherPriority(a: Dog, b: Dog): boolean {
    if (a.weight < b.weight) return false;
    return true;
  }
}

Under the hood, the Priority Queue implements a heap structure, using the abstract isHigherPriority method to determine how to sift the heap contents. In this way, classes that inherit from the PriorityQueue can behave like a MinHeap, MaxHeap, or anything else that sorts items based on any amount of complex priority logic!

Using the Queue

The queue provides the following method to interact with the data:

Method Description
insert(item:T) Add an item to the queue
pop() => T Returns the highest priority item and removes it from the queue
peek() => T Returns the highest priority item but leaves it in the queue
toArray() => T[] Returns the internal array of the queue - does NOT guarantee sorted order.
toSortedArray() => T[] Returns an array of all queue elements sorted by priority.

Example usage (continuing from above):

let dogPile = new DogPile();

let ernie = new Dog("Ernie",7);
let humph = new Dog("Humphrey",9);
let tums = new Dog("Mr. Tums",2);

dogPile.insert(humph);
dogPile.insert(ernie);

console.log(dogPile.peek())
// Output: { name: "Ernie", age: 7 }

dogPile.insert(tums);
console.log(dogPile.toSortedArray())
/* Output:
[ { name: "Mr. Tums", age: 2 },
  { name: "Ernie", age: 7 },
  { name: "Humphrey", age: 9 } ] */


console.log(dogPile.pop());
// Output: { name: "Mr. Tums", age: 2 }

console.log(dogPile.toSortedArray())
/* Output: 
[ { name: "Ernie", age: 7 },
  { name: "Humphrey", age: 9 } ] */

priority_queue's People

Contributors

amj311 avatar

Watchers

 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.