什么时候应该在PriorityQueue上使用TreeMap,反之亦然? [英] When should I use a TreeMap over a PriorityQueue and vice versa?

查看:159
本文介绍了什么时候应该在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,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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