如何preserve相同的优先级在实现二叉堆优先级队列元素的顺序?优先级、队列、顺序、元素

2023-09-11 04:34:17 作者:死一样的痛过丶

我已经创建了一个二元堆,从而重新presents优先级队列。这只是传统的众所周知的算法。这堆时间表不同的事件(排序关键是时间)的时间序列。

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 ( the sort key is time ).

它支持2操作:插入和删除。堆的每个节点的关键是大于或等于它的每个孩子。然而,使用相同的密钥将事件并不preserve他们增加了,因为以后每次删除或插入的顺序被调用时,堆起来,堆向下突破的程序的顺序。

It supports 2 operation: Insert and Remove. Each node's key of the heap is greater than or equal to each of its children. However, adding events with the same key doesn't preserve the order they were added, because each time after Remove or Insert were called, the heap-up and the heap-down procedures break the order.

我的问题是:我应该在一个经典的算法改为preserve节点具有相同的优先级顺序

My question is: what should be changed in a classical algorithm to preserve the order of the nodes with the same priority?

推荐答案

的一个解决方案是增加插入属性的时间来插入的元件。这可能只是一个简单的计数器增加了新的元素插入堆中各一次。然后,当两个元件是相等的按优先级,比较插入时

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.

 
精彩推荐