3

I really want to deepen my understanding of exactly how a TreeSet, particularly the no-argument version that does not use a comparator, maintains the elements that it contains in order. I cannot find a satisfactory explanation anywhere; they are either too basic or way too advanced for me. From my research, it seems that TreeSets actually store their elements in a TreeMap and TreeMaps are actually Red-Black trees. I feel pretty confident of my understanding about how elements are added to red-black trees.

I assume that somewhere in the java API there must be an algorithm or method that performs the insertion of elements into a Red-Black tree. My first question is where in the Java API is that algorithm located?

Also, how exactly is this algorithm invoked? I guess it is invoked by the add(E e) method in the TreeSet class, right? Can someone provide more details on the exact chain of events that occur when adding an element to a treeset.

Lastly, just as an experiment, I gave an object a compareTo() method but DID NOT implement the Comparable interface. When trying to add these objects to a TreeSet, an exception is thrown. I want to understand why an exception is thrown even though the object has a compareTo() method. I guess that there is a method somewhere in the insertion algorithm that requires all objects implement Comparable, is this correct? The stackTrace of the thrown exception points to TreeMap.compare() method. I guess this is the method that requires all objects that are added to a TreeSet implement the Comparable interface but I can not see this TreeMap.compare() method in the API. How can I find more information about this TreeMap.compare() method in the API?

Thank you very much in advance for your help.

Wes1324
  • 1,077
  • 8
  • 13
  • 2
    "why an exception is thrown even though the object has a compareTo() method. " Java does not use [duck typing](https://en.wikipedia.org/wiki/Duck_typing). The mere presence of a method called `compareTo()` is not enough: the class must implement `Comparable` and correctly override `compareTo()` to be usable. – Andy Turner Sep 30 '16 at 20:44
  • `TreeMap.compare` is a private implementation detail not exposed as part of the API. – Louis Wasserman Sep 30 '16 at 21:31

2 Answers2

3
  1. The no-arg version of TreeSet/TreeMap does indeed use a Comparator, specifically the natural order comparator (Comparator.naturalOrder() in Java 8). What this means is that the key type must be a Comparable<?>, because that makes it clear that instances of the class can be compared to each other.

  2. Java is a strongly-typed language, meaning that the types of variables take precedence over what methods it contains. If someone decides to rename the compareTo method in your class a few years down the line not realising it's used in a Comparable context (which is perfectly plausible if this is a large system), that will lead to pain and suffering. The implements Comparable<?> declaration makes this intention clear from the outset.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Joe C
  • 15,324
  • 8
  • 38
  • 50
0

Please see TreeMap source code. Specifically, jump to line 2039. This is the source code that corresponds to version 8 update 40 of Java.

There you can contemplate all the red-black mechanics. Basically, it's more or less as you suggested: write methods trigger rebalancing of the nodes of the tree.

Regarding compareTo method, elements of TreeSet/TreeMap must implement the Comparable interface. Implementing the compareTo method is not enough for this, the elements must be an explicit subtype of Comparable.

fps
  • 33,623
  • 8
  • 55
  • 110