什么时候应该在PriorityQueue上使用TreeMap,反之亦然?

编程入门 行业动态 更新时间:2024-10-10 21:25:22
本文介绍了什么时候应该在PriorityQueue上使用TreeMap,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

似乎他们都让你检索最小值,这是我对Prim算法所需要的,并强制我删除并重新插入一个键来更新它的值。使用一个优于另一个是否有任何优势,不仅仅是这个例子,但一般来说?

Seems they both let you retrieve the minimum, which is what I need for Prim's algorithm, and force me to remove and reinsert a key to update its value. Is there any advantage of using one over the other, not just for this example, but generally speaking?

推荐答案

一般来说,它使用堆来跟踪最小元素的工作量较少。

Generally speaking, it is less work to track only the minimum element, using a heap.

树更有条理,需要更多计算才能维护该组织。但是如果你需要访问任何键,而不仅仅是最小值,那么堆就不够了,并且树的额外开销是合理的。

A tree is more organized, and it requires more computation to maintain that organization. But if you need to access any key, and not just the minimum, a heap will not suffice, and the extra overhead of the tree is justified.

更多推荐

什么时候应该在PriorityQueue上使用TreeMap,反之亦然?

本文发布于:2023-11-29 04:13:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:什么时候   PriorityQueue   TreeMap

发布评论

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

>www.elefans.com

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