java Java的TreeSet和TreeMap用的是什么树?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3580761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What kind of tree is used in Java's TreeSet and TreeMap?
提问by Craig P. Motlin
Are they AVL trees, red-black trees, or something else?
它们是 AVL 树、红黑树还是其他什么树?
回答by Kru
回答by polygenelubricants
From the java.util.TreeMap<K,V>documentation:
A Red-Black treebased
NavigableMapimplementation.
一个红黑树的基础
NavigableMap实施。
For questions like these, you should always first consult the documentation. The API shouldn't describe ALLof the inner-workings of a class, but elementary informations such as general data structures and algorithms used are usually documented.
对于此类问题,您应该始终首先查阅文档。API 不应描述 a 的所有内部工作原理class,但通常记录基本信息,例如使用的通用数据结构和算法。
Other Java Collections Framework trivias
其他 Java 集合框架琐事
These are all little trivias that are also clearly documented:
这些都是小细节,也清楚地记录在案:
TreeSetis implemented with aTreeMapHashSetis implemented with aHashMapCollections.sortuses modified mergesortMap<K,V>is not aCollection<?>ArrayListdoesn't specify exact growth policy (unlike, say,Vector)
TreeSet是用一个TreeMapHashSet是用一个HashMapCollections.sort使用修改后的归并排序Map<K,V>不是一个Collection<?>ArrayList没有指定确切的增长政策(不像,说,Vector)
Related questions
相关问题
回答by WonderCsabo
It is a red-black tree in the Oracle desktop Java implementation, but an AVL-tree in Android.
它在 Oracle 桌面 Java 实现中是红黑树,但在 Android 中是AVL 树。
回答by matt b
The first sentence of the TreeMap Javadocstates:
TreeMap Javadoc的第一句话指出:
A Red-Black tree based
NavigableMapimplementation.
基于红黑树的
NavigableMap实现。
回答by u290629
TreeSet is based on TreeMap. And they uses red-black tree, red-black tree is a kind of AVL.
TreeSet 基于 TreeMap。他们使用红黑树, 红黑树是 AVL 的一种。

