Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
What is the best way to implement a PriorityQueue in java?

I am trying to implement my own PriorityQueue class from scratch (not using any existing Java imports or libraries). I know that I want to use a min-heap Data Structure. But I visualize a heap as a form on Binary Search Tree. Should I be using Linked List style Nodes to implement this min-heap, or should I use an an array? What are the benefits or preferred method of either? Or is there a third option available that I could use?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Before answering the question, please see this.

But I visualize a heap as a form on Binary Search Tree.

This is not true. Heap is a form of binary tree but not binary search tree. Please see Difference between binary tree and binary search tree

Now, to answer your question, I would choose some sort of array form. The reason is I need to calculate my children or my parent with index information frequently when I implement a heap. Usually, it happens with below calculation.

Given n is the index of the current node and index starts from 1(for the simplicity)

  • parents index = n/2
  • left child index = 2n
  • right child index = 2n+1

When you do this with LinkedList.get(n), it is O(n). In ArrayList or array, it is O(1).

over 4 years ago · Santiago Trujillo Report

0

The easiest implementation I can think of would use a LinkedList or ArrayList that keeps itself in sorted order based on priority. Then you remove from front or back of list (depending on how you sort array) when it is time to remove someone from the queue.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!