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

编程入门 行业动态 更新时间:2024-10-19 06:23:04
本文介绍了如何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相同的优先级在实现二叉堆优先级队列元素的顺序?

本文发布于:2023-11-29 21:39:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647695.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:优先级   队列   顺序   元素   preserve

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!