Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6543639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:22:15+00:00 2026-05-25T11:22:15+00:00

I need a priority queue, I guess in the framework, CHMutableArrayHeap and CHBinaryHeap can

  • 0

I need a priority queue, I guess in the framework, CHMutableArrayHeap and CHBinaryHeap can do the job, right?

However, if i send objects with identical priority to the queue, both CHMutableArrayHeap and CHBinaryHeap cannot maintain their adding orders.

for example, I have objects obj1 to obj10, their priorities are identical. After I add those 10 objects to the queue one by one from 1 to 10, the positions of them are not the same as the adding order, obj4 may come in front of obj1.

So, Quinn, what do you suggest if I want a priority queue keeping the adding order if priority is the same?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:22:16+00:00Added an answer on May 25, 2026 at 11:22 am

    (Sorry, I just barely saw this question.)

    The nature of a heap is that it doesn’t preserve insertion order at all, so you’d have to (1) build a priority queue atop another data structure or (2) supplement the heap with another data structure that tracks insertion order. However, a heap is generally the most efficient (and preferred) way to implement a priority queue, so I don’t know if it’s a good idea to use something else. The main reason I say this is that you want insertion to be extremely fast, which a heap provides; adding to a sorted array doesn’t.

    One possibility is to maintain a queue (e.g. CHCircularBufferQueue) for each priority level. (I’d imagine you’d want to create a wrapper structure that managed the queues so callers don’t have to deal with them directly.) You could potentially store the queues in a dictionary keyed by an NSNumber containing the priority, but I have no clue what the performance would look like. This obviously wouldn’t scale very efficiently to extremely wide ranges of priorities, and might not for lots of inserts and removes, but it might be workable in small cases. Just an idea.

    Another idea is to make a wrapper class that stores both priority and insertion time, then compares them in order. In this case, you’d probably want to use a sorted set (e.g. binary tree) to store them. However, the added overhead means you won’t get the same performance as with a heap, but I suppose that’s the tradeoff for maintaining insertion order.

    Update: I found these related questions:

    • How to preserve the order of elements of the same priority in a priority queue implemented as binary heap?
    • Priority Queue C
    • Priority queue implementation in C
    • priority queue data structure
    • Implementing Java Priority Queue

    • https://cstheory.stackexchange.com/questions/593/is-there-a-stable-heap

    https://stackoverflow.com/questions/tagged/priority-queue

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to store objects in a heap/priority queue. However, the objects may change
I need a priority queue where I can increase or decrease the priority key.
I need B class to have a min priority queue of AToTime objects. AToTime
I need a priority queue of objects but I keep getting this error: symbol:
I need to use a priority queue in my Python code, and: am looking
I need some kind of priority queue to store pairs <key, value> . Values
I need to build a blocking priority queue and my hunch is that TakeFromAny
Suppose I need to have a class which wraps a priority queue of other
so I am having trouble implementing priority queue. For a priority queue, I need
I have implemented Priority Queue interface for making heap. Can you tell me how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.