Options
All
  • Public
  • Public/Protected
  • All
Menu

Class PriorityQueue<T>

PriorityQueue (binary heap implementation)

In computer science, a priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a "priority" associated with it. In a priority queue, an element with high priority is served before an element with low priority. In some implementations, if two elements have the same priority, they are served according to the order in which they were enqueued, while in other implementations, ordering of elements with the same priority is undefined.

Priority Queue

min heap source: howtodoinjava.com

Complexity

Algorithm Average Worst case
enqueue O(log n) O(log n)
dequeue O(1) O(1)
peek O(1) O(1)
- - -
Space O(n) O(n)

Reference

import { PriorityQueue } from '@pencroff/ts-algorithms/dist/structure/priority-queue';
const q = new PriorityQueue([['A', 1], ['B', 2], ['C', 3]])

Type parameters

  • T

Hierarchy

  • PriorityQueue

Index

Constructors

Properties

Accessors

Methods

Constructors

constructor

Properties

Private _heap

_heap: BinaryHeap<[T, number]>

Private asMaxPriority

asMaxPriority: boolean

Accessors

length

  • get length(): number

Methods

__@iterator

clear

  • clear(): void

dequeue

enqueue

  • enqueue(value: T, priority?: number): void
  • Enqueue element to Q

    Parameters

    • value: T
    • Default value priority: number = 0

      Complexity: O(log n)

    Returns void

isEmpty

  • isEmpty(): boolean

peek