How to preserve the order of elements of the same priority in a priority queue implemented as binary heap?

后端 未结 3 1288
夕颜
夕颜 2021-01-04 00:19

I have created a binary heap, which represents a priority queue. It\'s just classical well known algorithm. This heap schedules a chronological sequence of different events

3条回答
  •  醉梦人生
    2021-01-04 01:04

    One solution is to add time of insertion attribute to the inserted element. That may be just a simple counter incremented each time a new element is inserted into the heap. Then when two elements are equal by priority, compare the time of insertion.

提交回复
热议问题