Are they AVL trees, red-black trees, or something else?
In this implementation, objects are sorted and stored in ascending order according to their natural order. The TreeSet uses a self-balancing binary search tree, more specifically a Red-Black tree.
In Java, a TreeMap is a red-black tree, which is a self-balancing binary search tree.
TreeSet implements SortedSet in Java. TreeMap implements Map Interface in Java. 2. TreeSet stored a single object in java. TreeMap stores two Object one Key and one value.
TreeMap is used to keep mappings between key and values in sorted order while TreeSet is used to keep just one element in sorted order. TreeSet also doesn't allow duplicates but TreeMap does allow duplicate values. If you find any other significant difference between TreeMap and TreeSet then please post as a comment.
Red-black trees as described in the first line of the javadoc.
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.
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 mergesortMap<K,V>
is not a Collection<?>
ArrayList
doesn't specify exact growth policy (unlike, say, Vector
)java.util.Arrays.sort(Object[])
use 2 kinds of sorting algorithms?If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With