What kind of tree is used in Java's TreeSet and TreeMap?
Are they AVL trees, re开发者_开发百科d-black trees, or something else?
Red-black trees as described in the first line of the javadoc.
- Tree Map
- Tree Set
From the java.util.TreeMap<K,V>
documentation:
A Red-Black tree based
NavigableMap
implementation.
For questions like these, you should always first consult the documentation. The API shouldn't describe ALL of the inner-workings of a class
, but elementary informations such as general data structures and algorithms used are usually documented.
Other Java Collections Framework trivias
These are all little trivias that are also clearly documented:
TreeSet
is implemented with aTreeMap
HashSet
is implemented with aHashMap
Collections.sort
uses modified mergesortMap<K,V>
is not aCollection<?>
ArrayList
doesn't specify exact growth policy (unlike, say,Vector
)
Related questions
- Why does
java.util.Arrays.sort(Object[])
use 2 kinds of sorting algorithms? - Why does the Java Collections Framework offer two different ways to sort?
- Why doesn't Java Map extends Collection?
The first sentence of the TreeMap Javadoc states:
A Red-Black tree based
NavigableMap
implementation.
It is a red-black tree in the Oracle desktop Java implementation, but an AVL-tree in Android.
TreeSet is based on TreeMap. And they uses red-black tree, red-black tree is a kind of AVL.
精彩评论