Skip to content

Conversation

@luckyb13
Copy link
Contributor

Added Priority Queue Implementation (priorityQueue.js)

Overview
This PR introduces a reusable Priority Queue data structure implemented using a binary heap. It supports a custom comparator, enabling both min-heap and max-heap usage in real applications such as scheduling, graph algorithms (Dijkstra, A*), and task prioritization.

Key Features

  • Binary heap implementation for efficient operations
  • Custom comparator function support (default: min-heap)
  • Public methods: push, pop, peek, size, isEmpty, clear
  • Fully self-contained with no dependencies
  • Node and browser compatible (ES Module)

Complexity

  • push: O(log n)
  • pop: O(log n)
  • peek, size, isEmpty: O(1)
  • Space: O(n)

Usage Example

import { PriorityQueue } from "./priorityQueue.js";

const pq = new PriorityQueue();
[5, 1, 4, 2, 3].forEach(x => pq.push(x));

while (!pq.isEmpty()) {
console.log(pq.pop()); // 1, 2, 3, 4, 5
}

Validation

  • Verified ordering correctness with both small and large inputs
  • Verified comparator customization for max-heap behavior
  • Verified API behavior and error-free execution

Notes for Reviewers

  • Clear function naming and private helper methods
  • No existing functionality modified
  • Code formatted consistently with repository style

Hacktoberfest Request
This is a meaningful contribution adding a widely-needed data structure. Please add the label:

hacktoberfest-accepted

Thank you.

@ghostmkg ghostmkg merged commit 47f684e into ghostmkg:main Oct 31, 2025
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants