如何preserve相同的优先级在实现二叉堆优先级队列元素的顺序? [英] How to preserve the order of elements of the same priority in a priority queue implemented as binary heap?

查看:200
本文介绍了如何preserve相同的优先级在实现二叉堆优先级队列元素的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个二元堆,从而重新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.

这篇关于如何preserve相同的优先级在实现二叉堆优先级队列元素的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆