开发者

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 a TreeMap
  • HashSet is implemented with a HashMap
  • Collections.sort uses modified mergesort
  • Map<K,V> is not a Collection<?>
  • 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜